我有一个.Rmd文件,并尝试通过pandoc函数创建.docx文件。
我想要一个分辨率为504x504像素的图形(即,7x7英寸,72dpi)。不幸的是,默认的72dpi质量太差了,我想将其增加到150dpi,而不改变最终分辨率(因此它在.docx文件中已经具有正确的大小)。如果我保持选项fig.width和fig.height=7,并设置dpi=150,我会得到所需的质量,但最终分辨率会增加,图形会超出.docx边界。我尝试使用out.width和out.height参数,但当我包含这些参数时,最终的.docx中就没有绘制任何内容。
有什么好的想法吗?
示例.Rmd代码:
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)
```
转换成 .docx 文件格式
library(knitr)
library(markdown)
knit("example.Rmd") # produces the md file
pandoc("example.md", format = "docx") #prodces the .docx file
如果我尝试重新调整这张图片的比例,它就无法正常工作。如下所示:
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)
。 - csgillespieknitr
中的fig.width=6, fig.height=6, dpi=300
。 - Yihui Xie