为什么 rmarkdown::render 中的 docx 和 html 的绘图大小不同?
Why does the plot size differ between docx and html in rmarkdown::render?
当尝试将图形从 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
。
运行s 之间的绘图参数似乎不同(这是来自不同的绘图):
par(no.readonly = T)
(docx)
## $pin
## [1] 3.76 2.16
##
## $plt
## [1] 0.164 0.916 0.255 0.795
对比
(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
包)
HTML 版本(正确)
DOCX 版本(尺寸错误)
截断是由于绘图设备(png)太小。 R Markdown 根据输出格式使用不同的图形宽度默认值。使用默认选项 will produce 7 inch wide images, while images in docx output are only 5 inch wide per default 编织到 HTML。有问题的图对于所选的设备宽度来说太宽,导致意外截断。
您可以通过将 HTML 输出的图形宽度设置为 5 英寸来验证这一点。生成的图片会出现和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)
```
当尝试将图形从 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
。
运行s 之间的绘图参数似乎不同(这是来自不同的绘图):
par(no.readonly = T)
(docx)
## $pin
## [1] 3.76 2.16
##
## $plt
## [1] 0.164 0.916 0.255 0.795
对比
(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
包)
HTML 版本(正确)
截断是由于绘图设备(png)太小。 R Markdown 根据输出格式使用不同的图形宽度默认值。使用默认选项 will produce 7 inch wide images, while images in docx output are only 5 inch wide per default 编织到 HTML。有问题的图对于所选的设备宽度来说太宽,导致意外截断。
您可以通过将 HTML 输出的图形宽度设置为 5 英寸来验证这一点。生成的图片会出现和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)
```