Skip to content

Commit 17d1c58

Browse files
committed
Add map2() walk through and examples
1 parent df1d08a commit 17d1c58

File tree

3 files changed

+31
-71
lines changed

3 files changed

+31
-71
lines changed

04-map2.R

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
library(repurrrsive)
2+
gap_split_small <- gap_split[1:10]
3+
countries <- names(gap_split_small)
4+
5+
# For each country create a ggplot of Life Expectancy through time
6+
# with a title
7+
8+
# For one country
9+
ggplot(gap_split_small[[1]], aes(year, lifeExp)) +
10+
geom_line() +
11+
labs(title = countries[[1]])
12+
13+
# For all countries
14+
plots <- map2(gap_split_small, countries,
15+
~ ggplot(.x, aes(year, lifeExp)) +
16+
geom_line() +
17+
labs(title = .y))
18+
19+
plots[[1]]
20+
# Display all plots
21+
walk(plots, print) # this might take awhile
22+
23+
# Save all plots
24+
walk2(.x = plots, .y = countries,
25+
~ ggsave(filename = paste0(.y, ".pdf"), plot = .x))
26+
27+
# Argh! I didn't want all those pictures in this directory,
28+
# remove them all
29+
file.remove(paste0(countries, ".pdf"))
30+
31+

04-other-iteration.R

Lines changed: 0 additions & 71 deletions
This file was deleted.

slides.pdf

14.6 KB
Binary file not shown.

0 commit comments

Comments
 (0)