Andreas Andreas - 11 months ago 125
Markdown Question

Scaling plots in Rmd via out.width

I am producing a Rmd document that also includes some plots. It should export to both, otd and pdf. Some of the figures should be small enough to fit two of them next to each other in the resulting odt.

I knit the document via

Rscript -e "require(knitr); require(markdown); knit('Test_Markdown.Rmd', '')"

In the Rmd there is a plot that looks like this:

```{r plottestnormal, fig.path = ".", fig.width = 6, fig.height = 6, dpi = 300, dev = "png", dev.args = list(type = "cairo")}
plot(1:10, 1:10)

I would like the plot to be half size -- but scaled!

This gets the size correct, but not the scaling:

```{r plottestsmall, fig.path = ".", fig.width = 3, fig.height = 3, dpi = 600, dev = "png", dev.args = list(type = "cairo")}
plot(1:10, 1:10)

This as I understand from the opiton description the following should do what I mean, but it does not 'work'.

```{r plottestscaled, fig.path = ".", fig.width = 6, fig.height = 6, out.width = 3, out.height = 3, dpi = 300, dev = "png", dev.args = list(type = "cairo")}
plot(1:10, 1:10)

What happens is that this produces an html img tag in the generated *md file:

<img src=".plottestscaled-1.png" title="plot of chunk plottestscaled" alt="plot of chunk plottestscaled" width="3" height="3" />

And that does not show in either pdf nor odt when compiled with

pandoc -f markdown "" -o "Test_Markdown.odt"

pandoc -t latex "" -o "Test_Markdown.pdf"

And I have seen this issue which does not help me further.

What am I missing?


Thanks to @Yihui this now works for LaTeX-> pdf and (presumably -- untested) for html.

Am I right, that
are not supported for

If they are, what might be the reason that they do not work for me?

If they are not, how can I achieve the same effect (scaled plots) in

(I would like to export to
, so any solution would ideally cover (at least) these two simultaneously.)

Here is a complete doc that shows my problem when processed via
rmarkdown::render("Test_Markdown.Rmd", "all")

title: Scale plot in Rmd
latex_engine: xelatex
keep_md: true

```{r plottestscaled, fig.path = "./", fig.width = 6, fig.height = 6, out.width = "3in", out.height = "3in", dpi = 300, dev = "png", dev.args = list(type = "cairo")}
plot(1:10, 1:10)


My 'best' solution so far is, to disable the scaling in the
output and to manually rescale the plots in office. Not very reproducible, though....

```{r plottestscaled, fig.path = "./", fig.width = 6, fig.height = 6, out.width=switch(opts_knit$get(""), latex = "3in", NULL), out.height=switch(opts_knit$get(""), latex = "3in", NULL), dpi = 300, dev = "png", dev.args = list(type = "cairo")}
plot(1:10, 1:10)

Answer Source

Pandoc's Markdown did not to support specifying the size of images until very recent versions, so when you specify the width of a plot, knitr has to fall back to HTML, and HTML code will not work for LaTeX or ODT output (only native Markdown code is supported, except for LaTeX output, in which raw LaTeX code is also supported).

The short answer is use the rmarkdown package instead of running Pandoc by yourself. knitr will be smart enough to switch to LaTeX code when generating figures that have the width attributes. Or even better, just click the Knit button in the RStudio IDE.

You can try to figure out all the technical details behind the scenes if you want (start from knitr::hook_plot_md), but it can be overwhelming if you are not familiar with how knitr and rmarkdown work internally.