Poor resolution in knitr using Rmd
Asked Answered
R

3

48

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)
```
Rapids answered 19/9, 2013 at 1:9 Comment(6)
perhaps #14830291 will provide inspirationMa
When I save png files, I use something like: ppi = 300; png("mygraph.png", width=6*ppi, height=6*ppi, res=ppi)Gerhan
@Gerhan that is equivalent to fig.width=6, fig.height=6, dpi=300 in knitrTinea
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 itTinea
Unfortunately it does not work for .docx. It works just fine for .html though. Thanks!Rapids
By the way did you know that you can click "knit doc" in RStudio if you add some lines to .Rmd file ? rmarkdown.rstudio.com/word_document_format.htmlGraeme
G
37

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

Grane answered 14/3, 2016 at 4:27 Comment(0)
L
29

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)
```
Luane answered 10/10, 2018 at 17:31 Comment(1)
Nailed it, definitely the right attitude.Ardeen
L
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.

Lase answered 5/12, 2014 at 17:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.