Poor resolution in knitr using Rmd

dbarneche picture dbarneche · Sep 19, 2013 · Viewed 16.8k times · Source

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

Answer

Matthew Walker picture Matthew Walker · Mar 14, 2016

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".