绘图尺寸和分辨率与R markdown, knitr, pandoc, beamer。

时间:2021-03-04 06:12:50

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

在默认情况下不适合幻灯片,甚至不以任何其他方式打印。

Here's the .Rmd: Edit: it seems you have to use plot() in every chunk. Second plot now prints.

这里是。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)
```

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

保存测试。Rmd然后用beamer编译到tex:

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

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

开放测试。在RStudio中,点击“编译PDF”。

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

我已经读过了Yihui的文档,希望我没有遗漏一些非常明显的东西。

Edit new code incorporating Yihui's suggestions.

编辑新代码,结合Yihui的建议。

```{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()

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  

2 个解决方案

#1


37  

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:

我认为这是一个经常被问到的问题关于在beamer的幻灯片中,由Pandoc和markdown产生的数据。真正的问题是,R Markdown在默认情况下(从knitr)生成PNG图像,并且在默认情况下很难得到PNG图像的大小(我不知道为什么)。不过,要得到正确的PDF格式的图片是相当容易的。一种解决方案是将默认的图形设备重置为PDF,在您的第一个块:

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

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

然后所有的图像都将被写入PDF文件,而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).

你的第二个问题是你把HTML单元和乳胶单位混在一起了。宽/ out.height。乳胶和HTML是非常不同的技术。您不应该期望maxwidth在HTML中工作,或者在LaTeX中使用200px。特别是当你想把Markdown换成LaTeX时,最好不要开始。宽/。高度(使用图宽/图高度,并让乳胶使用原尺寸)。

#2


15  

Figure sizes are specified in inches and can be included as a global option of the document output format. For example:

图形大小以英寸表示,可以作为文档输出格式的全局选项。例如:

---
title: "My Document"
output:
  html_document:
    fig_width: 6
    fig_height: 4
---

And the plot's size in the graphic device can be increased at the chunk level:

图形设备的图形大小可以在块级增加:

```{r, fig.width=14, fig.height=12}          #Expand the plot width to 14 inches

ggplot(aes(x=mycolumn1, y=mycolumn2)) +     #specify the x and y aesthetic
geom_line(size=2) +                         #makes the line thicker
theme_grey(base_size = 25)                  #increases the size of the font
```

You can also use the out.width and out.height arguments to directly define the size of the plot in the output file:

你也可以用out。宽度。高度参数直接定义输出文件中图形的大小:

```{r, out.width="200px", out.height="200px"} #Expand the plot width to 200 pixels

ggplot(aes(x=mycolumn1, y=mycolumn2)) +     #specify the x and y aesthetic
geom_line(size=2) +                         #makes the line thicker
theme_grey(base_size = 25)                  #increases the size of the font
```

#1


37  

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:

我认为这是一个经常被问到的问题关于在beamer的幻灯片中,由Pandoc和markdown产生的数据。真正的问题是,R Markdown在默认情况下(从knitr)生成PNG图像,并且在默认情况下很难得到PNG图像的大小(我不知道为什么)。不过,要得到正确的PDF格式的图片是相当容易的。一种解决方案是将默认的图形设备重置为PDF,在您的第一个块:

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

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

然后所有的图像都将被写入PDF文件,而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).

你的第二个问题是你把HTML单元和乳胶单位混在一起了。宽/ out.height。乳胶和HTML是非常不同的技术。您不应该期望maxwidth在HTML中工作,或者在LaTeX中使用200px。特别是当你想把Markdown换成LaTeX时,最好不要开始。宽/。高度(使用图宽/图高度,并让乳胶使用原尺寸)。

#2


15  

Figure sizes are specified in inches and can be included as a global option of the document output format. For example:

图形大小以英寸表示,可以作为文档输出格式的全局选项。例如:

---
title: "My Document"
output:
  html_document:
    fig_width: 6
    fig_height: 4
---

And the plot's size in the graphic device can be increased at the chunk level:

图形设备的图形大小可以在块级增加:

```{r, fig.width=14, fig.height=12}          #Expand the plot width to 14 inches

ggplot(aes(x=mycolumn1, y=mycolumn2)) +     #specify the x and y aesthetic
geom_line(size=2) +                         #makes the line thicker
theme_grey(base_size = 25)                  #increases the size of the font
```

You can also use the out.width and out.height arguments to directly define the size of the plot in the output file:

你也可以用out。宽度。高度参数直接定义输出文件中图形的大小:

```{r, out.width="200px", out.height="200px"} #Expand the plot width to 200 pixels

ggplot(aes(x=mycolumn1, y=mycolumn2)) +     #specify the x and y aesthetic
geom_line(size=2) +                         #makes the line thicker
theme_grey(base_size = 25)                  #increases the size of the font
```