Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Figure size in R Markdown

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!

like image 346
striatum Avatar asked Dec 30 '15 18:12

striatum


1 Answers

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

Fig 1

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

Fig 2

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

Fig 1

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

enter image description here

like image 76
Stefan Avey Avatar answered Sep 23 '22 00:09

Stefan Avey