使用Rmd编织的分辨率很差

dba*_*che 28 r pixels pandoc knitr r-markdown

我有一个.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)
```
Run Code Online (Sandbox Code Playgroud)

转换为.docx

library(knitr)
library(markdown)
knit("example.Rmd")  # produces the md file
pandoc("example.md", format = "docx") #prodces the .docx file
Run Code Online (Sandbox Code Playgroud)

如果我尝试重新缩放图形,它就不起作用.下面:

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)
```
Run Code Online (Sandbox Code Playgroud)

Mat*_*ker 21

最有可能的是,自从提出这个问题以来,该软件已得到改进.我来到这个问题寻找如何提高情节的分辨率.我发现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)
```
Run Code Online (Sandbox Code Playgroud)

但是,完全设置out.widthout.height删除图像的生成,警告"fig.align,out.width,out.height,out.extra不支持Word输出".


The*_*ell 10

这是利用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 )
````
Run Code Online (Sandbox Code Playgroud)

如果您不想对生成的每个图像使用img_template,则可以不调用set函数,而是添加opts.label="img_template"到要使用它的块的参数,或者通过为块明确指定params来覆盖img_template .


mma*_*123 6

保持简单,将所有卡盘的dpi设置为300,然后将其扩大。

运行第一件事:

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