More advice about chunk options
This commit is contained in:
parent
c67d5ed58c
commit
91c93df995
|
@ -515,29 +515,31 @@ 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.
|
||||
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. You can learn more about `ggsave()` in the documentation.
|
||||
|
||||
### 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).
|
||||
The biggest challenge of graphics in RMarkdown is getting your figures the right size and shape. There are five main options that control figure sizing: `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 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 find it most aesthetically pleasing for plots to have a consistent
|
||||
width. To enforce this I set `fig.width = 6` (6") and `fig.asp = 0.618`
|
||||
(the golden ratio) in the defaults. Then in individual chunks, I only
|
||||
adjust `fig.asp`.
|
||||
|
||||
* 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.
|
||||
of the line width). I default to `out.width = "70%"`
|
||||
and `fig.align = 'center'`. That 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.
|
||||
* To put multiple plots in a single row I set the `out.width` to
|
||||
`50%` for two plots, `33%` for 3 plots, or `25%` to 4 plots, and set
|
||||
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`, as
|
||||
discussed 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.
|
||||
If you find that you're having to squint to read the text in your plot, you need to tweak `fig.width`. If `fig.width` is larger than the size the figure is rendered in the final doc, the text will be too small; if `fig.width` is smaller, the text will be too big. 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. To illustrate the principle, the following three plots have `fig.width` of 4, 6, and 8 respectively:
|
||||
|
||||
```{r, include = FALSE}
|
||||
plot <- ggplot(mpg, aes(displ, hwy)) + geom_point()
|
||||
|
@ -552,13 +554,15 @@ plot
|
|||
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.
|
||||
If you want to make sure the font size is the same in all your figures, whenever you set `out.width`, you'll also need to adjust `fig.width` to maintain the same ratio with your default `out.width`. For example, if your default `fig.width` is 6 and `out.width` is 0.7, when you set `out.width = "50%"` you'll need to set `fig.width` to 4.2 (6 * 0.5 / 0.7).
|
||||
|
||||
### Other tips
|
||||
### Other important options
|
||||
|
||||
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.
|
||||
When mingling code and text, like I do in this book, I recommend setting `fig.show = "hold"` so that that plots are shown 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.
|
||||
To add a caption to the plot, use `fig.cap`. In RMarkdown this will change the figure from inline to "floating".
|
||||
|
||||
If you're producing pdf output, the default graphics type is PDF. This a good default because PDFs are high quality vector graphics. However, they can produce very large and 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 more compact.
|
||||
|
||||
## Learning more
|
||||
|
||||
|
|
Loading…
Reference in New Issue