Figure size in R Markdown

和自甴很熟 提交于 2019-12-04 04:13:40

问题


I tried to be careful and thorough, to read various things on the net on how to format figures in R Markdown. They are plotted correctly, but it seems that their size cannot be controlled.

Firstly, there are basics, like:

```{r Fig1, echo=FALSE, fig.height=5, fig.width=15}
    x1 = rnorm(100)
    x2 = runif(100)
    x3 = rbeta(100, 1, 1,)
    par(mfrow=c(1,3), mar=c(4,4,4,1), oma=c(0.5,0.5,0.5,0))
    qqnorm(x1)
    qqnorm(x2)
    qqnorm(x3)
```

Then, I try a bit more with:

```{r Fig1b, echo=FALSE, fig.height=5, fig.width=15, out.retina=1}

```

And if I try to match size of another, simple figure, differences are quite visible. For example:

```{r Fig2, echo=FALSE, fig.height=5, fig.width=5, retina=1}
    par(mfrow=c(1,1), mar=c(4,4,4,1), oma=c(0.5,0.5,0.5,0))
    qqnorm(x1)
```

I wonder what can be done about it -- i.e., how to make all figures equal in size? In particular, if figures such as Fig1 and Fig1b are shrunk, how to adjust the size of simple figure like in Fig2?

Thanks!


回答1:


It seems to me like you want Fig2 to be the same size as a single panel in Fig1. If you really want them to be the same size, I'd suggest using the same fig.width and same value for mfrow.

```{r Fig1, echo=TRUE, fig.height=5, fig.width=15}
x1 = rnorm(100)
x2 = runif(100)
x3 = rbeta(100, 1, 1,)
par(mfrow=c(1,3), mar=c(4,4,4,1), oma=c(0.5,0.5,0.5,0))
qqnorm(x1)
qqnorm(x2)
qqnorm(x3)
```

```{r Fig2, echo=TRUE, fig.height=5, fig.width=15}
par(mfrow=c(1,3), mar=c(4,4,4,1), oma=c(0.5,0.5,0.5,0)) # same, could omit
plot.new()   # empty plot
qqnorm(x1)
plot.new()   # empty plot
```

And if you mean you want Fig2 to take up the same amount of space on the rendered document as Fig1 then try this where par(op) resets the plotting parameters.

```{r Fig1, echo=TRUE, fig.height=5, fig.width=15}
x1 = rnorm(100)
x2 = runif(100)
x3 = rbeta(100, 1, 1,)
op <- par(mfrow=c(1,3), mar=c(4,4,4,1), oma=c(0.5,0.5,0.5,0))
qqnorm(x1)
qqnorm(x2)
qqnorm(x3)
par(op)
```

```{r Fig2, echo=TRUE, fig.height=5, fig.width=15}
op <- par(mfrow=c(1,1), mar=c(4,4,4,1), oma=c(0.5,0.5,0.5,0))
qqnorm(x1)
par(op)
```



来源:https://stackoverflow.com/questions/34535155/figure-size-in-r-markdown

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!