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)
```
ppi = 300; png("mygraph.png", width=6*ppi, height=6*ppi, res=ppi)
– Gerhanfig.width=6, fig.height=6, dpi=300
inknitr
– Tineaout.width=504
is probably not enough, since you did not specify the unit, although you probably mean pixelsout.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 – Tinea