【发布时间】:2013-09-23 22:52:56
【问题描述】:
我有一个 .Rmd 文件,我正在尝试通过函数 pandoc 创建一个 .docx 文件。
我想要一个最终分辨率为 504x504 像素的图形(即 7x7 英寸,72dpi)。不幸的是,默认的 72 dpi 质量太差,我想在不改变最终分辨率的情况下将其增加到 150 dpi(因此它在 .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)
```
【问题讨论】:
-
当我保存 png 文件时,我使用类似:
ppi = 300; png("mygraph.png", width=6*ppi, height=6*ppi, res=ppi) -
@csgillespie 相当于
knitr中的fig.width=6, fig.height=6, dpi=300 -
out.width=504可能还不够,因为您没有指定单位,尽管您可能指的是像素out.width='504px';即使这样,我也不确定 pandoc 是否可以很好地在 .docx 中设置图形大小;我没有 MS Word,所以无法验证 -
不幸的是,它不适用于 .docx。它适用于 .html 不过。谢谢!
标签: r knitr pixels pandoc r-markdown