-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSolution_09.R
More file actions
46 lines (35 loc) · 1.39 KB
/
Solution_09.R
File metadata and controls
46 lines (35 loc) · 1.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# Exercise 9
# let's use the previous solution
source("Solution_08.R")
# create a directory to store plots in, if it doesn't exist
if (!dir.exists("plots")) dir.create("plots")
# we now want to save all of the files that were created previously.
# for this, we need a combination of the plots, and filenames.
# we can use the names assinged to the list items of the plots object
names(plots)
# we need to append the names with ".png"
# (hint: use the paste0 function for string concatenation in R)
filenames <- paste0(names(plots), ".png")
# now, we can use the walk2 function, along with the filenames and plots objects
# and the ggsave function.
# we don't need to assign the output of walk2 to a variable: walk is for function
# side effects and only returns the first input
# make sure to set the following arguments with ggsave:
# device = "png"
# path = "plots"
# you could also play about with the height/width by using the following arguments
# width = 20
# height = 12
# units = "cm"
# if you have the Cairo package installed, then I would recommend adding in
# type = "cairo-png"
# dpi = "retina"
# (try running those lines, if they don't work then install.packages("Cairo").
# this will produce much higher quality graphics that are anti-aliased)
walk2(filenames,
plots,
ggsave,
device = "png",
path = "plots",
type = "cairo-png",
dpi = "retina")