Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Poor resolution in knitr using Rmd

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)
```
like image 800
dbarneche Avatar asked Sep 19 '13 01:09

dbarneche


People also ask

What is the function of knitr in creating markdown document?

knitr is an engine for dynamic report generation with R. It is a package in the programming language R that enables integration of R code into LaTeX, LyX, HTML, Markdown, AsciiDoc, and reStructuredText documents. The purpose of knitr is to allow reproducible research in R through the means of literate programming.

What is a knitr document?

knitr is an R package that integrates computing and reporting. By incorporating code into text documents, the analysis, results and discussion are all in one place.

How do you save plots in rmarkdown?

You want to make sure that you save images you create in your rmarkdown document. To do this automatically, and avoid writing things like ggsave(plot) or dev. off() , just create the plots in rmarkdown, and tell it you want to save the output. In the funky looking code at the top, the YAML, specify keep_md: yes .


3 Answers

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

like image 131
Matthew Walker Avatar answered Sep 27 '22 23:09

Matthew Walker


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)
```
like image 42
mmann1123 Avatar answered Sep 25 '22 23:09

mmann1123


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.

like image 22
Thell Avatar answered Sep 26 '22 23:09

Thell