Last active
March 19, 2021 21:14
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 | |
Author
SPLOpenData
commented
Mar 16, 2021
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment