(Yet More) Exercises
1. Run the following lines of code. Which of the two plots is saved as mpg-plot.png
? Why?
ggplot(mpg, aes(x = class)) +
geom_bar()
ggplot(mpg, aes(x = cty, y = hwy)) +
geom_point()
ggsave("mpg-plot.png")
It will run the second. In looking at ?ggplot2::ggsave
, we see that it defaults to the most recent plot run.
2. What do you need to change in the code above to save the plot as a PDF instead of a PNG? How could you find out what types of image files would work in ggsave()
?
Changing filename in ggsave()
to end in .pdf
instead of .png
will change the type. Looking at the function documentation, we see there are options for: “eps”, “ps”, “tex” (pictex), “pdf”, “jpeg”, “tiff”, “png”, “bmp”, “svg” and “wmf” (windows only).