使用Rmd编织的分辨率很差

时间:2021-03-04 06:12:56

I have a .Rmd file and I am trying to create a .docx file via the function pandoc.

我有一个.Rmd文件,我试图通过函数pandoc创建一个.docx文件。

I want to have a figure with final resolution of 504x504 pixels (i.e., 7x7inch with 72dpi). Unfortunately, the default 72 dpi is too poor in quality, and I would like to increase it to, say, 150 dpi without altering the final resolution (so it will already have the correct size within the .docx file). If I keep options fig.width and fig.height=7 and set dpi=150, I get the quality I want but the final resolution increases and the figure blows outside the .docx margins. I tried playing with the arguments out.width and out.height but when I include those it just doesn't plot anything in the final .docx.

我想要一个最终分辨率为504x504像素的数字(即7x7英寸,72dpi)。不幸的是,默认的72 dpi质量太差了,我想把它增加到150 dpi而不改变最终分辨率(所以它在.docx文件中已经有了正确的大小)。如果我保留选项fig.width和fig.height = 7并设置dpi = 150,我得到我想要的质量,但最终分辨率增加,数字超出.docx边距。我尝试使用out.width和out.height参数进行播放,但是当我包含它们时,它不会在最终的.docx中绘制任何内容。

Ideas?

想法?

Example .Rmd code:

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

Transforming into .docx

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

If I try to rescale the figure, it just does not work. Below:

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

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

2 个解决方案

#1


13  

It's most likely that since this question was asked, the software has improved. I came to this question looking for how to increase the resolution of plots. I found OP's original approach worked out-of-the-box for me.

最有可能的是,自从提出这个问题以来,该软件已得到改进。我来到这个问题寻找如何提高情节的分辨率。我发现OP的原始方法对我来说是开箱即用的。

So, setting dpi=300 (because dpi=150 did not produce a sufficiently obvious difference) in the chunk's parameters, produced a much higher quality image without modifying the physical size of the images within Word.

因此,在块的参数中设置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)
```

However, setting out.width and out.height removes the production of the image entirely, with the warning "fig.align, out.width, out.height, out.extra are not supported for Word output".

但是,设置out.width和out.height会完全删除图像的生成,并且警告“fig.align,out.width,out.height,out.extra不支持Word输出”。

#2


8  

This is a great time to take advantage of knitr's built-in dynamic customization features for output types. Ths was tested with both output targets...

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

If you don't want to use the img_template for every image produced you can either not call the set function and instead add opts.label="img_template" to the params of the chunks you want to use it with, or override the img_template by specifying the params explicitly for the chunk.

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

#1


13  

It's most likely that since this question was asked, the software has improved. I came to this question looking for how to increase the resolution of plots. I found OP's original approach worked out-of-the-box for me.

最有可能的是,自从提出这个问题以来,该软件已得到改进。我来到这个问题寻找如何提高情节的分辨率。我发现OP的原始方法对我来说是开箱即用的。

So, setting dpi=300 (because dpi=150 did not produce a sufficiently obvious difference) in the chunk's parameters, produced a much higher quality image without modifying the physical size of the images within Word.

因此,在块的参数中设置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)
```

However, setting out.width and out.height removes the production of the image entirely, with the warning "fig.align, out.width, out.height, out.extra are not supported for Word output".

但是,设置out.width和out.height会完全删除图像的生成,并且警告“fig.align,out.width,out.height,out.extra不支持Word输出”。

#2


8  

This is a great time to take advantage of knitr's built-in dynamic customization features for output types. Ths was tested with both output targets...

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

If you don't want to use the img_template for every image produced you can either not call the set function and instead add opts.label="img_template" to the params of the chunks you want to use it with, or override the img_template by specifying the params explicitly for the chunk.

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