Skip to content

Instantly share code, notes, and snippets.

@SPLOpenData
Last active March 19, 2021 21:14
Show Gist options
  • Save SPLOpenData/7446d2f716974644ee90f47d5ed91cca to your computer and use it in GitHub Desktop.
Save SPLOpenData/7446d2f716974644ee90f47d5ed91cca to your computer and use it in GitHub Desktop.
This example R script creates a visualization using ggplot2 with both ggtextures & magick images.
#Eample visualization to demonstrate use of ggplot + ggtextures + magick images``
library(magick)
library(ggplot2)
library(ggtextures) #Not on CRAN. Use: devtools::install_github("clauswilke/ggtextures")
library(dplyr)
#Source data 'https://www.icco.org/wp-content/uploads/Grindings_QBCS-XLVII-No.-1.pdf'
df<-tibble(country= c("Germany", "Cote d'ivoire", "Netherlands", "USA", "Indonesia"),
grindings= c(430, 610, 600, 385, 485),
flag_img= c("de","ci","nl","us","id"),
img = list(
image_read_svg("https://hatscripts.github.io/circle-flags/flags/de.svg"),
image_read_svg("https://hatscripts.github.io/circle-flags/flags/ci.svg"),
image_read_svg("https://hatscripts.github.io/circle-flags/flags/nl.svg"),
image_read_svg("https://hatscripts.github.io/circle-flags/flags/us.svg"),
image_read_svg("https://hatscripts.github.io/circle-flags/flags/id.svg"))) %>%
arrange(-.$grindings)
#Make the plot
p <- ggplot(df, aes(x=reorder(country, grindings), grindings, fill = country, image = img)) +
geom_isotype_col(
img_height = grid::unit(1, "null"), img_width = NULL,
ncol = 1, nrow = 1, hjust = .9, vjust = 0.5) +
coord_flip() +
scale_fill_manual(values = c("#3e1e04","#6a3005","#965015", "#c4923e","#cbac85"))+
scale_y_continuous(limits = c(0, max(df$grindings)+100))+
geom_text(aes(label=grindings),hjust=-.05, size=10) +
xlab("") +ylab("Grindings (measured in 1000s of tons)")+guides(fill = "none")+
theme_minimal()+ggtitle("Grindings of cocoa beans by country") +
theme(plot.title = element_text(size = 20))+
labs(subtitle = paste("2020-21 season estimates for top 5 countries.
Source: https://www.icco.org/wp-content/uploads/Grindings_QBCS-XLVII-No.-1.pdf"))
#The result
p
@SPLOpenData
Copy link
Author

cocoa-grindings

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