-
-
Save apoorvalal/2e07000980d623883ab648b48383eec8 to your computer and use it in GitHub Desktop.
Reference code for grouped plots using purrr's map2
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
library(tidyverse) | |
library(gapminder) | |
graphloc = '~/tmp/r-sandbox/popn_trends/' | |
dir.create(graphloc, showWarnings = F) | |
setwd(graphloc) | |
# split dataset by country, then plot population trends | |
(pop_plots <- gapminder %>% | |
group_by(country) %>% | |
nest() %>% | |
mutate(plot = map2(data, country, ~ggplot(data = .x, aes(x=year, y = pop)) + | |
geom_line() + geom_point() + | |
ggtitle(.y)+labs(subtitle='Population time trend') + | |
lal_plot_theme()), | |
filename = paste0(graphloc, country,'_popn_trend.pdf') | |
) %>% select(plot, filename)) | |
map2(pop_plots$filename, pop_plots$plot, ggsave, device = cairo_pdf) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment