Skip to content

Instantly share code, notes, and snippets.

@jhnwllr
Created October 28, 2021 11:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jhnwllr/dfa8733a0b5e69e310a3acf92a3df6a5 to your computer and use it in GitHub Desktop.
Save jhnwllr/dfa8733a0b5e69e310a3acf92a3df6a5 to your computer and use it in GitHub Desktop.
library(ggplot2)
library(ggtext)
library(extrafont)
font_import()
loadfonts(device = "win")
windowsFonts()
breaks = scales::pretty_breaks(n = 7)(c(0,900e3))
labels = gbifapi::plot_label_maker(breaks,unit_MK = "K",unit_scale = 1e-3)
# geom_col(aes(fill=group,stat="identity")) +
p = ggplot(pd,aes(country,n,group=group,fill=group)) +
geom_bar(position="stack",stat="identity") +
scale_y_continuous(breaks = breaks,label = labels,limits=c(0,900e3),expand=c(0.01,0.1)) +
scale_fill_manual(values = c("bird" = "#509E2F", "mammal" = "#ff93ff", "plant" = "#FDAF02", "other" = "#E8E8E8")) +
coord_flip() +
xlab("") +
ylab("number of 'sensitive' occurrences") +
theme_bw() +
theme(axis.text.y=element_text(face=face,size=10,color="#535362")) +
theme(axis.title.x = element_text(margin = margin(t = 8, r = 0, b = 5, l = 0), size = 11, face="plain")) +
theme(legend.position = "top") +
theme(legend.title = element_blank()) +
guides(fill = guide_legend(nrow = 1, reverse = TRUE)) +
theme(legend.key.size = unit(0.3, 'cm')) +
theme(text=element_text(family="sans")) +
theme(plot.margin = unit(c(0.1,0.5,0.1,0.1), "cm")) +
labs(
caption =
"sensitive = on CITES + certain location + NT,VU,EN,CR<br>
countries considered 'high risk' by WWF in <b>bold</b>") +
theme(
plot.caption.position = "plot",
plot.caption = element_textbox_simple(
size = 8,
lineheight = 1,
padding = margin(1, 1, 1, 1),
margin = margin(5, 5, 5.5, 200),
fill = "#ffffff",
hjust = 1
))
save_dir = "C:/Users/ftw712/Desktop/iucn_project_2021/plots/"
gbifapi::save_ggplot_formats(p,save_dir,"country_barplot",height=4,width=6,formats=c("pdf","jpg","svg"))
@jhnwllr
Copy link
Author

jhnwllr commented Oct 28, 2021

country_barplot

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment