简体   繁体   English

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

[英]How to adjust figure size in pdf rendered directly from R script?

I read from some where on SO that figure size for pdf rendered directly from a R script can be set with knitr::opts_chunk . 我从knitr::opts_chunk可以用knitr::opts_chunk设置直接从R脚本渲染的pdf的图形尺寸。 I want to make a pdf from a R script with several figures and tried to adjust figure size with opts_chunk . 我想从R脚本制作带有多个图形的pdf文件,并尝试使用opts_chunk调整图形大小。 However, it didn't work. 但是,它没有用。 Below is an short example. 下面是一个简短的示例。

This is a script called main.R : 这是一个名为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)

I tried to get desired pdf in rstudio with command ctrl + shift + K. A snapshot of the pdf I got looks like this: 我尝试使用命令ctrl + shift + K在rstudio中获取所需的pdf。我得到的pdf快照如下所示: 在此处输入图片说明

although I wanted a 3x5 figure, this seems to be a 5x3 figure. 尽管我想要3x5的数字,但这似乎是5x3的数字。 How should I specify the figure width and height correctly in R script in order to get desired output? 如何在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"
#' ---
#' 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)
````

This is described here: http://rmarkdown.rstudio.com/articles_report_from_r_script.html . 此处对此进行了描述: http : //rmarkdown.rstudio.com/articles_report_from_r_script.html You put the chunk options in a special comment: 您将块选项放在特殊注释中:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM