47

I have a .Rmd file and I am trying to create a .docx file via the function pandoc.

I want to have a figure with final resolution of 504x504 pixels (i.e., 7x7inch with 72dpi). Unfortunately, the default 72 dpi is too poor in quality, and I would like to increase it to, say, 150 dpi without altering the final resolution (so it will already have the correct size within the .docx file). If I keep options fig.width and fig.height=7 and set dpi=150, I get the quality I want but the final resolution increases and the figure blows outside the .docx margins. I tried playing with the arguments out.width and out.height but when I include those it just doesn't plot anything in the final .docx.

Ideas?

Example .Rmd code:

My title
-------------------------

*(this report was produced on: `r as.character(Sys.Date())`)*  

That's my plot

```{r echo=FALSE}
    plot(0,0,type="n",xlim=c(0,500), ylim=c(-12,0), las=1)
    color  <-  rainbow(500)
    text(380,-1,"Test",pos=4)
    lseq   <-  seq(-6,-2,length.out=500)
    for(j in seq_along(lseq)) {
        lines(c(400,450), rep(lseq[j], 2), col=color[j])
    }
    polygon(c(400,450,450,400), c(-6,-6,-2,-2), lwd=1.2)
```

Transforming into .docx

library(knitr)
library(markdown)
knit("example.Rmd")  # produces the md file
pandoc("example.md", format = "docx") #prodces the .docx file

If I try to rescale the figure, it just does not work. Below:

My title
-------------------------

*(this report was produced on: `r as.character(Sys.Date())`)*  

That's my plot

```{r echo=FALSE, dpi=150, fig.width=7, fig.height=7, out.width=504, out.height=504}
    plot(0,0,type="n",xlim=c(0,500), ylim=c(-12,0), las=1)
    color  <-  rainbow(500)
    text(380,-1,"Test",pos=4)
    lseq   <-  seq(-6,-2,length.out=500)
    for(j in seq_along(lseq)) {
        lines(c(400,450), rep(lseq[j], 2), col=color[j])
    }
    polygon(c(400,450,450,400), c(-6,-6,-2,-2), lwd=1.2)
```
dbarneche
  • 645
  • 1
  • 5
  • 8
  • perhaps http://stackoverflow.com/questions/14829791/figure-sizes-with-pandoc-conversion-from-markodown-to-docx will provide inspiration – mnel Sep 19 '13 at 01:41
  • When I save png files, I use something like: `ppi = 300; png("mygraph.png", width=6*ppi, height=6*ppi, res=ppi)` – csgillespie Sep 19 '13 at 08:24
  • 4
    @csgillespie that is equivalent to `fig.width=6, fig.height=6, dpi=300` in `knitr` – Yihui Xie Sep 20 '13 at 03:18
  • 3
    `out.width=504` is probably not enough, since you did not specify the unit, although you probably mean pixels `out.width='504px'`; even with that, I'm not sure if pandoc can do a good job setting the figure size in .docx; I do not have MS Word, so I cannot verify it – Yihui Xie Sep 20 '13 at 03:19
  • Unfortunately it does not work for .docx. It works just fine for .html though. Thanks! – dbarneche Sep 24 '13 at 06:38
  • By the way did you know that you can click "knit doc" in RStudio if you add some lines to .Rmd file ? http://rmarkdown.rstudio.com/word_document_format.html – Marcin Aug 20 '14 at 20:24

3 Answers3

36

It's most likely that since this question was asked, the software has improved. I came to this question looking for how to increase the resolution of plots. I found OP's original approach worked out-of-the-box for me.

So, setting dpi=300 (because dpi=150 did not produce a sufficiently obvious difference) in the chunk's parameters, produced a much higher quality image without modifying the physical size of the images within Word.

```{r, echo=FALSE, dpi=300, fig.width=7, fig.height=7}
plot(0,0,type="n",xlim=c(0,500), ylim=c(-12,0), las=1)
color  <-  rainbow(500)
text(380,-1,"Test",pos=4)
lseq   <-  seq(-6,-2,length.out=500)
for(j in seq_along(lseq)) {
    lines(c(400,450), rep(lseq[j], 2), col=color[j])
}
polygon(c(400,450,450,400), c(-6,-6,-2,-2), lwd=1.2)
```

However, setting out.width and out.height removes the production of the image entirely, with the warning "fig.align, out.width, out.height, out.extra are not supported for Word output".

Matthew Walker
  • 2,527
  • 3
  • 24
  • 30
27

Just keep things simple, set all chucks to dpi of 300 and make them wider.

Run this first thing:

```{r setup, include=FALSE}
knitr::opts_chunk$set(dpi=300,fig.width=7)
```
mmann1123
  • 5,031
  • 7
  • 41
  • 49
14

This is a great time to take advantage of knitr's built-in dynamic customization features for output types. Ths was tested with both output targets...

````{r img-setup, include=FALSE, cache=FALSE}
out.format <- knitr::opts_knit$get("out.format")
img_template <- switch( out.format,
                     word = list("img-params"=list(fig.width=6,
                                                   fig.height=6,
                                                   dpi=150)),
                     {
                       # default
                       list("img-params"=list( dpi=150,
                                               fig.width=6,
                                               fig.height=6,
                                               out.width="504px",
                                               out.height="504px"))
                     } )

knitr::opts_template$set( img_template )
````

If you don't want to use the img_template for every image produced you can either not call the set function and instead add opts.label="img_template" to the params of the chunks you want to use it with, or override the img_template by specifying the params explicitly for the chunk.

Thell
  • 5,883
  • 31
  • 55