使用Rmd的Knitr分辨率差 [英] Poor resolution in knitr using Rmd
问题描述
我有一个.Rmd文件,我正在尝试通过pandoc函数创建一个.docx文件.
I have a .Rmd file and I am trying to create a .docx file via the function pandoc.
我想要一个最终分辨率为504x504像素(即7x7英寸,72dpi)的图形.不幸的是,默认的72 dpi质量太差,我想将其提高到150 dpi,而不会更改最终分辨率(因此.docx文件中的尺寸已经正确).如果我保留fig.width和fig.height = 7的选项并设置dpi = 150,则可以得到所需的质量,但最终分辨率会提高,并且数字会超出.docx边距.我尝试使用参数out.width和out.height进行播放,但是当我包含这些参数时,它只是不会在最终的.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.
想法?
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
如果我尝试重新缩放比例,它将无法正常工作.下方:
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)
```
推荐答案
自从提出此问题以来,该软件最有可能得到了改进.我来到这个问题,寻找如何提高地块的分辨率.我发现OP的原始方法对我来说是开箱即用的.
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.
因此,在块的参数中设置dpi=300
(因为dpi=150
不会产生足够明显的差异),可以生成质量更高的图像,而无需修改Word中图像的物理尺寸.
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.
```{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.width
和out.height
会完全消除图像的产生,并警告"Word输出不支持fig.align,out.width,out.height,out.extra".
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".
这篇关于使用Rmd的Knitr分辨率差的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!