使用Rmd时,knitr的分辨率较低

48

我有一个.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)
```

也许这个问题会为你提供灵感:https://dev59.com/CmUq5IYBdhLWcg3wC8Px - mnel
当我保存PNG文件时,我会使用类似以下的代码:ppi = 300; png("mygraph.png", width=6*ppi, height=6*ppi, res=ppi) - csgillespie
4
@csgillespie 这相当于 knitr 中的 fig.width=6, fig.height=6, dpi=300 - Yihui Xie
3
“out.width=504”可能不足以表达您的意思,因为您没有指定单位,但您可能是想使用像素“out.width='504px'”;即使采用这种方式,我也不确定pandoc是否能够在.docx中设置图像大小;我没有MS Word,所以无法验证。 - Yihui Xie
不幸的是,它不适用于 .docx 文件。但对于 .html 文件它运行良好。谢谢! - dbarneche
顺便说一下,您知道如果在.Rmd文件中添加几行代码,就可以在RStudio中点击“knit doc”吗?http://rmarkdown.rstudio.com/word_document_format.html - Marcin
3个回答

37

很可能自此问题被提出以来,该软件已经有所改进。我找到这个问题是为了寻找如何增加绘图的分辨率。我发现 OP 的原始方法对我来说可以直接使用。

因此,在代码块的参数中设置 dpi=300(因为 dpi=150 没有产生足够明显的差异),可以在不修改 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)
```

然而,设置out.widthout.height会完全删除图像生成,并出现警告“fig.align、out.width、out.height、out.extra不支持Word输出”。


28

保持简单,将所有块的dpi设置为300,并使它们更宽。

首先运行此命令:

```{r setup, include=FALSE}
knitr::opts_chunk$set(dpi=300,fig.width=7)
```

干得好,绝对是正确的态度。 - arielhasidim
做得漂亮,绝对是正确的态度。 - undefined

14

这是一个很好的时机,可以利用knitr内置的动态自定义功能来生成输出。它已经在两个输出目标上进行了测试...

````{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 )
````

如果您不想将用于每个生成的图像,您可以选择不调用set函数,而是在您想要使用它的块的参数中添加opts.label="img_template",或通过明确指定块的参数来覆盖img_template。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接