在 RStudio 中更改 R 笔记本中的绘图打印纵横比或大小,而不使绘图变为全宽

Har*_*y M 3 r rstudio knitr r-markdown

我想更改在 RStudio 的 R 笔记本中打印的绘图的纵横比或图形大小。

我可以使用fig.asp块参数调整纵横比。例如,在下面的最小示例中,绘图纵横比正确地变为 2。但是,与此同时,绘图也变得非常大,在笔记本中全宽(下面的屏幕截图)。如何更改纵横比而不使绘图在笔记本中变成全宽?指定fig.widthout.width参数没有什么区别。

环境:

R version: 3.6.1 (2019-07-05)
RStudio version: 1.2.1541
platform       x86_64-apple-darwin15.6.0   
arch           x86_64                      
os             darwin15.6.0                
system         x86_64, darwin15.6.0 
Run Code Online (Sandbox Code Playgroud)

最小的例子:

---
title: "R Notebook"
output: html_notebook
---

This is an [R Markdown](http://rmarkdown.rstudio.com) Notebook. When you 
execute code within the notebook, the results appear beneath the code. 

Try executing this chunk by clicking the *Run* button within the chunk or 
by placing your cursor inside it and pressing *Cmd+Shift+Enter*. 

```{r, fig.asp=2, fig.width='200px'}
plot(cars)
```
Run Code Online (Sandbox Code Playgroud)

问题:

在此输入图像描述

chr*_*aho 5

在 RMarkdown 中,您不需要将代码块选项括在引号中。

对于您的示例,只需'从代码块选项行中删除(如下所示:)out.width=200px即可按预期显示绘图。

这段代码:

```{r, fig.asp=2, out.width=200px}
plot(cars)
```
Run Code Online (Sandbox Code Playgroud)

有这样的结果:

<code>out.width=200px</code> 的屏幕截图

fig.width或者,您也可以尝试使用和选项指定图形的固定宽度和高度fig.height。这些也不需要引号 ( ')。

例如,这两个代码块具有非常不同的纵横比:

```{r, fig.width=12, fig.height=4}
plot(cars)
```
Run Code Online (Sandbox Code Playgroud)

```{r, fig.width=4, fig.height=12}
plot(cars)
```
Run Code Online (Sandbox Code Playgroud)

得到这样的结果:

指定 <code>fig.width</code> 和 <code>fig.height</code> 的两个图的屏幕截图

我相信这会有所帮助。