Xinming
Xinming

Reputation: 117

How to zoom or shrink the size of ggplot or simple plot in Rmarkdown output?

I know one way is to specify fig.height and fig.width in the code chunks, but I'd like to keep the default display aspect ratio of the plot.

Is there any option that can control the output size of a plot (like default value 1 stands for the default size and value 2 stands for double size)?

Upvotes: 2

Views: 2701

Answers (1)

Peter
Peter

Reputation: 7770

Use the opts.label option to create lists of options for a chunk. For an example, there are three pairs of fig.height and fig.width defined by opts_template$set and then called in a chunk option. See the example .Rmd file below.

---
title: How to zoom or shrink the size of ggplot or simple plot in Rmarkdown
output: html_document
---

Use option templates, see `?opts_template` for more details.  An example
follows.  There will be three 

```{r setup, include = FALSE, cache = FALSE}
library(ggplot2)
library(knitr)
opts_template$set(figure1 = list(fig.height = 4, fig.width = 4),
                  figure2 = list(fig.height = 2, fig.width = 4),
                  figure3 = list(fig.height = 5, fig.widht = 3)) 
```

The default graphic to show is build here

```{r ourgraphic}
g <- ggplot(mpg) + aes(x = model, y = cty) + geom_boxplot()
```

Use the `opts.label` option to set the figures.  `figure1` is here:
```{r opts.label = "figure1"}
g
```

Now, `figure3`,
```{r opts.label = "figure3"}
g
```

and lastly `figure2`.
```{r opts.label = "figure2"}
g
```

Upvotes: 2

Related Questions