如何调整直接从R脚本渲染的pdf中的图形大小?

时间:2017-03-24 13:57:42

标签: r pdf knitr r-markdown

我从SO的某些地方读到,可以使用knitr::opts_chunk设置直接从R脚本渲染的pdf的数字大小。我想用R脚本制作一个带有几个数字的pdf,并尝试用opts_chunk调整图形大小。但是,它没有用。以下是一个简短的例子。

这是一个名为main.R的脚本:

#' ---
#' title: "test"
#' output:
#'     pdf_document
#' ---

#' ## draw a long figure, not a wide figure
knitr::opts_chunk$set(fig.width = 3, fig.height = 5)
plot(mtcars$mpg, mtcars$drat)

我尝试使用命令ctrl + shift + K在rstudio中获得所需的pdf。我得到的pdf快照如下所示: enter image description here

虽然我想要一个3x5的数字,但这似乎是一个5x3的数字。如何在R脚本中正确指定图形宽度和高度以获得所需的输出?

> sessionInfo()
R version 3.3.1 (2016-06-21)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 16.04.2 LTS

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C               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    LC_PAPER=en_US.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C             LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

loaded via a namespace (and not attached):
 [1] backports_1.0.5 magrittr_1.5    rprojroot_1.2   htmltools_0.3.5 tools_3.3.1     yaml_2.1.14    
 [7] Rcpp_0.12.10    stringi_1.1.2   rmarkdown_1.3   knitr_1.15.1    stringr_1.1.0   digest_0.6.12  
[13] evaluate_0.10  
> 
> windows.options()
Error: could not find function "windows.options"

2 个答案:

答案 0 :(得分:1)

#' ---
#' title: "test"
#' output:
#'     pdf_document
#' ---

#' ## draw a long figure, not a wide figure

```{r scatterplot, fig.width=5, fig.height=3}
plot(mtcars$mpg, mtcars$drat)
````

答案 1 :(得分:1)

这里描述:http://rmarkdown.rstudio.com/articles_report_from_r_script.html。您将块选项放在特殊注释中:

#' ---
#' title: "test"
#' output:
#'     pdf_document
#' ---

#' ## draw a long figure, not a wide figure
#+ fig.width=3, fig.height=5
plot(mtcars$mpg, mtcars$drat)
相关问题