当尝试从Rmd到Word和Docx绘制图形时,图形大小会有所不同,其中docx版本会修剪边缘。有什么办法可以防止这种情况?
这是一个Rmd,在某种程度上很少复制此内容。 (在其他情节中,效果要极端得多,但需要更多代码才能重现)
```{r}
library(gemtc)
example(gemtc)
forest(results)
```
rmarkdown::render("./test.Rmd", output_format="word_document", clean=F)
rmarkdown::render("./test.Rmd", output_format="html_fragment")
请注意在右侧修剪的CrI
。
运行之间的绘图参数似乎有所不同(这是来自不同的绘图):
par(no.readonly = T)
(docx)
## $pin
## [1] 3.76 2.16
##
## $plt
## [1] 0.164 0.916 0.255 0.795
vs。
(html)
## $pin
## [1] 5.76 3.16
##
## $plt
## [1] 0.1171429 0.9400000 0.2040000 0.8360000
##
在同一情节上。在某些情况下,这会导致Word的边缘被极端修剪。
版本信息
R version 3.4.4 (2018-03-15)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 18.04.1 LTS
Matrix products: default
BLAS: /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.7.1
LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.7.1
locale:
[1] LC_CTYPE=C.UTF-8 LC_NUMERIC=C
[3] LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8
[5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8
[7] LC_PAPER=en_US.UTF-8 LC_NAME=C
[9] LC_ADDRESS=C LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] knitr_1.21.1 rmarkdown_1.11 gemtc_0.8-2 coda_0.19-2
loaded via a namespace (and not attached):
[1] Rcpp_1.0.0 lattice_0.20-38 digest_0.6.18 truncnorm_1.0-8
[5] slam_0.1-43 plyr_1.8.4 grid_3.4.4 meta_4.9-3
[9] magrittr_1.5 evaluate_0.12 stringi_1.2.4 tools_3.4.4
[13] stringr_1.3.1 igraph_1.2.2 xfun_0.4 compiler_3.4.4
[17] pkgconfig_2.0.2 Rglpk_0.6-3 htmltools_0.3.6
一个更极端情况的说明性示例(与上述命令相同,在相同的Rmd文件上运行,图来自coda
包)
答案 0 :(得分:10)
截止是由于绘图设备(png)太小。 R Markdown根据输出格式使用不同的图形宽度默认值。使用默认选项will produce 7 inch wide images编织为HTML,而docx输出中的图像仅为5 inch wide per default。对于选择的设备宽度,有问题的图太宽,导致意外切断。
您可以通过将图形宽度设置为5英寸以输出HTML来验证这一点。生成的图像将具有与docx中相同的问题:
---
output:
html_document:
fig_width: 5
---
```{r}
library(gemtc)
example(gemtc)
forest(results)
```
因此,解决方法是全局选择较大的图形宽度
---
output:
word_document:
fig_width: 5.5
---
或分别针对每个地块:
```{r fig.width=5.5}
library(gemtc)
example(gemtc)
forest(results)
```