R Knitr Chunk Options for Figure Height/Width Are Not Working

r knitr chunk options for figure height / width are not working

I'm pretty sure you were using Sweave instead of knitr. If you were using RStudio (you did not say it but I guess so), you need to change the global option (or project option) to knitr:

use knitr in RStudio

Knitr not displaying graph when out.width or out.height set

Instead of using knitr::knit() it is easier to use rmarkdown::render(). You can either call it directly

Rscript -e 'rmarkdown::render("file.Rmd")'

or indirectly using an example script from the littler package

render.r file.Rmd

on the command line.

R markdown ... chunk size to control figure size in knitr - text size problem

I have found a solution to this ...

Instead of using fig.width and fig.height in the chunk header you can use
out.width = "NN%" as shown below

chunk header

Then you get the axis text scaled appropriately with the figure; this
scaling doesn't seem to apply to figure heading - but I can live with that :-)

knitted ggplot output

I have also found that you can combine out.width with fig.height to get more control over the proportions of the plots :

Sample Image

In knitr, figure heights do not obey my opts_chunk$set commands when sizes are changed on the fly

I think it has to do with repeatedly setting opts_chunk though I don't know why it shouldn't work the way you want. Regardless, here is a working example. I won't post the result to save space, but it looks like you want - sizes are respected.

<!DOCTYPE html>
<html>
<head>
<title>TEST</title>
</head>
<body>

<!--begin.rcode Setup, echo = FALSE
opts_chunk$set(echo=FALSE, dpi=96, fig.width=9, fig.height=8, dev="png")
end.rcode-->

<!--begin.rcode Fig1
print("Width 9, height 8")
plot(rnorm(100), col="orange")
end.rcode-->

<hr>

<!--begin.rcode Fig2, fig.width=9, fig.height=12
print("Width 9, height 12")
plot(rnorm(100), col="purple")
end.rcode-->

<hr>

</body>
</html>

Knitr changes Image Size

There is a difference between fig.width and out.width, and likewise between fig.height and out.height. The fig.* control the size of the graphic that is saved, and the out.* control how the image is scaled in the output.

For example, the following .Rmd file will produce the same graphic twice but displayed with different width and height.

---
title: "Example graphic"
---

```{r echo = FALSE, fig.width = 14, fig.height = 9, out.width = "588", out.height = "378"}
library(ggplot2)
ggplot(mpg, aes(x = year, y = cyl)) + geom_point()
```

```{r echo = FALSE, fig.width = 14, fig.height = 9, out.width = "1026", out.height = "528"}
library(ggplot2)
ggplot(mpg, aes(x = year, y = cyl)) + geom_point()
```

Sample Image



Related Topics



Leave a reply



Submit