Consistent figures in Quarto
July 23, 2024
I’ve been struggling to correctly size my ggplot2 or base plots in Quarto documents. The main problem I encounter is that when I change the width or size of a figure, the text in figures also changes in size, which makes the document look very inconsistent.
This post documents the (awesome) solution I stumbled upon and settled on. There is no explanation, just a series of steps. For discussion, see the links at the bottom of the post.
Step 1: Defaults
Set reasonable document-wide defaults:
knitr::opts_chunk$
Step 2: Aspect ratio
For each figure, adjust the fig-asp code chunk as appropriate. For example:
```{r}
#| fig-asp: .7
plot(hp ~ mpg, data = mtcars)
```
attempt to use zero-length variable name
fig-asp is a “free” parameter. You can adjust it as you please without a second thought.
Step 3: Width
Finally, set the width of your figure. This is a two step process:
- Set
out-width - Set
fig-widthbased on the value ofout-width
First, you decide what percentage of the horizontal space you want the figure to take. This is defined in terms of percentage by the out-width chunk option.
Second, use the out2fig() calculator to figure out what value of fig-width to select:
{
fig.width.default * out.width / out.width.default
}
For example, if you use the same defaults as me and want a figure to take up 95% of horizontal space in your document, set the fig-width chunk option to:
> [1] 8.142857
```{r}
#| fig-asp: .7
#| out-width: 95%
#| fig-width: 8.142857
plot(hp ~ mpg, data = mtcars)
```
attempt to use zero-length variable name
References
Loading source...