diff --git a/communicate-plots.Rmd b/communicate-plots.Rmd index 405b075..cf005c1 100644 --- a/communicate-plots.Rmd +++ b/communicate-plots.Rmd @@ -486,6 +486,65 @@ Many people wonder why the default theme has a grey background. This was a delib It's also possible to control individual components of each theme, like the size and colour of the font used for the y axis. This unfortunately is outside the scope of this book, so you'll need to read the ggplot2 book for the full details. You can also create your own themes if you have a corporate style or you're trying to match a journal style. +## Saving your plots + +There are two main ways to get your plots out of R and into your final write-up: `gsave()` and knitr. `ggsave()` will save the most recent plot to disk: + +```{r, fig.show = "none"} +ggplot(mpg, aes(displ, hwy)) + geom_point() +ggsave("my-plot.pdf") +``` +```{r, include = FALSE} +file.remove("my-plot.pdf") +``` + +If you don't specify the `width` and `height` they will be taken from dimensions of the current plotting device. For reproducible code, you'll want to specify them. + +Generally, however, I think you should be assembling your final reports using knitr and rmarkdown, so I want to focus on the important chunk options that you should know about for graphics. + +### Figure sizing + +The most challenging chunk options relate to figure sizing. There are five options that you need to be aware of: `fig.width`, `fig.height`, `fig.asp`, `out.width` and `out.height`. Image sizing is challenging because there are two sizes (the size of the figure created by R and the size in which it is inserted in the output document), and multiple ways of specifying the size (height, width, aspect ratio: pick two out of three). + +I only ever use three of the five options. + +* I generally find it easier to make plots line up in an aesthetically pleasing + way if I set `fig.width` and `fig.asp` rather than using width and height. + I use defaults of `fig.width = 6` and `fig.asp = 0.618` (the golden ratio). + +* I control the output size with `out.width` and set it to a percentage + (of the line width). I default to `out.width = "70%"` + and `fig.align = 'center'`. That seems to give plots room to breath, + without taking up too much space. + +* If I want multiple plots in columns I set (e.g.) + `out.width = "50%", fig.align = "default"`. Depending on what I'm trying + to illustrate (e.g. show data or show plot variations), I'll also tweak + `fig.width`, see below. + +If you find that you're having to squint to read the text, it's typically because you've set the `fig.width` to be much larger than its eventual display. For example, the following three plots have `fig.width` of 4, 6, and 8 respectively. + +```{r, include = FALSE} +plot <- ggplot(mpg, aes(displ, hwy)) + geom_point() +``` +```{r, fig.width = 4, echo = FALSE} +plot +``` +```{r, fig.width = 6, echo = FALSE} +plot +``` +```{r, fig.width = 8, echo = FALSE} +plot +``` + +You'll often need to do a little experimentation to figure out the right ratio between the `fig.width` and the eventual width in your document. + +### Other tips + +When mingling code and text, like I do in this book, I recommend setting `fig.show = "hold"` so that all the plot come after the code. This has the pleasant side effect of forcing you to break up large blocks of code with their explanations. + +If you're producing pdf output, the default graphics type is PDF. This a good default because PDFs are high quality vector graphics. However, that can produce very large and very slow plots if you are displaying thousands of points. In that case, set `dev = "png"` to force the use of PNGs. They are slightly lower quality, but will be much smaller. + ## Learning more The absolute best place to learn more is the ggplot2 book: [_ggplot2: Elegant graphics for data analysis_](https://amzn.com/331924275X). It goes into much more depth about the underlying theory, and has many more examples of how to combine the individual pieces to solve practical problems. Unfortunately the book is not available online for free, although can find the source code at .