Skip to content

Instantly share code, notes, and snippets.

@apoorvalal
Last active August 8, 2018 17:31
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 apoorvalal/2e07000980d623883ab648b48383eec8 to your computer and use it in GitHub Desktop.
Save apoorvalal/2e07000980d623883ab648b48383eec8 to your computer and use it in GitHub Desktop.
Reference code for grouped plots using purrr's map2
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