如何在Rmarkdown输出中缩放或缩小ggplot或简单绘图的大小?

时间:2017-03-11 23:52:06

标签: r plot ggplot2 r-markdown

我知道一种方法是在代码块中指定fig.height和fig.width,但我希望保持绘图的默认显示宽高比。

是否有任何可以控制绘图输出大小的选项(如默认值1代表默认大小,值2代表双倍大小)?

1 个答案:

答案 0 :(得分:2)

使用opts.label选项为块创建选项列表。例如,fig.height定义了三对fig.widthopts_template$set,然后在chunk选项中调用。请参阅下面的示例.Rmd文件。

---
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
```