使用R markdown,knitr,pandoc,beamer绘制尺寸和分辨率

nac*_*dus 58 r beamer pandoc knitr r-markdown

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

这是.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)
```
Run Code Online (Sandbox Code Playgroud)

保存为test.Rmd 然后使用beamer编译为tex:

knit("test.Rmd")
system("pandoc -s -t beamer --slide-level 1 test.md -o test.tex")
Run Code Online (Sandbox Code Playgroud)

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

我已经阅读了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)
```
Run Code Online (Sandbox Code Playgroud)

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  
Run Code Online (Sandbox Code Playgroud)

Yih*_*Xie 46

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

```{r setup, include=FALSE}
knitr::opts_chunk$set(dev = 'pdf')
```
Run Code Online (Sandbox Code Playgroud)

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

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

  • @nacnudus你最好在再次运行knitr之前删除`figure`目录; 关于LaTeX有很多黑暗的事实,例如它默认优先使用PNG而不是PDF,所以如果有`foo.png`和`foo.pdf`,`\ includegraphics {foo}`将使用`foo.png ` (5认同)

Cro*_*non 19

图形大小以英寸为单位指定,可作为文档输出格式的全局选项包含在内.例如:

---
title: "My Document"
output:
  html_document:
    fig_width: 6
    fig_height: 4
---
Run Code Online (Sandbox Code Playgroud)

并且图形设备中的绘图大小可以在块级别增加:

```{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
```
Run Code Online (Sandbox Code Playgroud)

您还可以使用out.widthout.height参数直接在输出文件中定义绘图的大小:

```{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
```
Run Code Online (Sandbox Code Playgroud)