绘图大小和分辨率,带有R降价,knitr,pandoc,beamer [英] Plot size and resolution with R markdown, knitr, pandoc, beamer

查看:78
本文介绍了绘图大小和分辨率,带有R降价,knitr,pandoc,beamer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

默认情况下不适合幻灯片,甚至不通过其他任何方式打印.

Doesn't fit on the slide by default, doesn't even print by any other means.

这是.Rmd:似乎您必须在每个块中都使用plot().现在将打印第二个图.

# Plot should show at high resolution

```{r echo=FALSE, comment = ""}
# load some data
require(plyr)
rbi <- ddply(baseball, .(year), summarise,  
  mean_rbi = mean(rbi, na.rm = TRUE))
```

```{r}
# plot
plot(mean_rbi ~ year, type = "l", data = rbi)
```

# Second attempt
```{r, fig.width = 2, fig.height = 2}
plot(mean_rbi ~ year, type = "l", data = rbi)
```

# Third attempt
```{r, out.width = 2, out.height = 2}
plot(mean_rbi ~ year, type = "l", data = rbi)
```

# Fourth attempt
```{r, out.width = '200px', out.height = '200px'}
plot(mean_rbi ~ year, type = "l", data = rbi)
```

# Fifth attempt
```{r, out.width = '\\maxwidth'}
plot(mean_rbi ~ year, type = "l", data = rbi)
```

另存为test.Rmd 然后使用Beamer编译为tex:

Save that as test.Rmd Then compile to tex using beamer:

knit("test.Rmd")
system("pandoc -s -t beamer --slide-level 1 test.md -o test.tex")

在RStudio中打开test.tex,然后单击编译PDF".

Open test.tex in RStudio and click "Compile PDF".

我已经阅读了Yihui的文档,希望我没有错过任何明显的东西.

I've read Yihui's documentation and hope I haven't missed something really obvious.

编辑新代码,其中包含了益辉的建议.

Edit new code incorporating Yihui's suggestions.

```{r setup, include=FALSE}
opts_chunk$set(dev = 'pdf')
```

# Plot should show at high resolution

```{r echo=FALSE, comment = ""}
# load some data
require(plyr)
rbi <- ddply(baseball, .(year), summarise,  
  mean_rbi = mean(rbi, na.rm = TRUE))
```

```{r}
# plot
plot(mean_rbi ~ year, type = "l", data = rbi)
```

# Second attempt
```{r, fig.width = 4, fig.height = 4}
plot(mean_rbi ~ year, type = "l", data = rbi)
```

sessionInfo()

 R version 3.0.1 (16/05/2013)
Platform: x86_64-pc-linux-gnu (64-bit)

Local:
 [1] LC_CTYPE = en_US.UTF-8 LC_NUMERIC = C LC_TIME = C LC_COLLATE = C        
 [5] LC_MONETARY=C        LC_MESSAGES=C        LC_PAPER=C           LC_NAME=C           
 [9] LC_ADDRESS=C         LC_TELEPHONE=C       LC_MEASUREMENT=C     LC_IDENTIFICATION=C 

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

other attached packages:
[1] plyr_1.8       markdown_0.6   knitr_1.2      rCharts_0.3.51 slidify_0.3.52

loaded via a namespace (and not attached):
 [1] RJSONIO_1.0-3   codetools_0.2-8 digest_0.6.3    evaluate_0.4.3  formatR_0.8    
 [6] grid_3.0.1      lattice_0.20-15 stringr_0.6.2   tools_3.0.1     whisker_0.3-2  
[11] yaml_2.1.7  

推荐答案

我认为这是一个关于Pandoc和markdown制作的投影仪幻灯片中的图形行为的常见问题.真正的问题是,R Markdown默认情况下会从knitr生成PNG图像,并且默认情况下很难在LaTeX中获得正确的PNG图像大小(我不知道为什么).但是,正确确定PDF图像的大小是相当容易的.一种解决方案是在第一个块中将默认图形设备重置为PDF:

I think that is a frequently asked question about the behavior of figures in beamer slides produced from Pandoc and markdown. The real problem is, R Markdown produces PNG images by default (from knitr), and it is hard to get the size of PNG images correct in LaTeX by default (I do not know why). It is fairly easy, however, to get the size of PDF images correct. One solution is to reset the default graphical device to PDF in your first chunk:

```{r setup, include=FALSE}
knitr::opts_chunk$set(dev = 'pdf')
```

然后所有图像将被写入PDF文件,而LaTeX将会很高兴.

Then all the images will be written as PDF files, and LaTeX will be happy.

您的第二个问题是在out.width/out.height中将HTML单元与LaTeX单元混合使用. LaTeX和HTML是非常不同的技术.您不应该期望\maxwidth在HTML中运行,或者在200px在LaTeX中运行.尤其是当您要将Markdown转换为LaTeX时,最好不要设置out.width/out.height(使用fig.width/fig.height并让LaTeX使用原始大小).

Your second problem is you are mixing up the HTML units with LaTeX units in out.width / out.height. LaTeX and HTML are very different technologies. You should not expect \maxwidth to work in HTML, or 200px in LaTeX. Especially when you want to convert Markdown to LaTeX, you'd better not set out.width / out.height (use fig.width / fig.height and let LaTeX use the original size).

这篇关于绘图大小和分辨率,带有R降价,knitr,pandoc,beamer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆