【问题标题】:Poor resolution in knitr using Rmd使用 Rmd 的 knitr 分辨率较差
【发布时间】: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


【解决方案1】:

保持简单,将所有卡盘设置为 300 dpi 并使其更宽。

运行第一件事:

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

【讨论】:

    【解决方案2】:

    很可能自从提出这个问题以来,软件已经改进了。我来这个问题是为了寻找如何提高绘图的分辨率。我发现 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 会完全删除图像的生成,并显示警告“Word 输出不支持 fig.align、out.width、out.height、out.extra”。

    【讨论】:

      【解决方案3】:

      这是利用 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 )
      ````
      

      如果您不想为每个生成的图像使用 img_template,您可以不调用 set 函数,而是将 opts.label="img_template" 添加到要使用它的块的参数中,或者通过指定覆盖 img_template显式地为块设置参数。

      【讨论】:

        猜你喜欢
        • 2013-07-31
        • 1970-01-01
        • 2018-01-14
        • 1970-01-01
        • 2020-09-09
        • 1970-01-01
        • 2019-04-22
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多