【问题标题】:Plot size and resolution with R markdown, knitr, pandoc, beamer使用 R markdown、knitr、pandoc、beamer 绘制大小和分辨率
【发布时间】:2013-07-31 14:23:34
【问题描述】:

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

这是 .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:

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

在 RStudio 中打开 test.tex 并点击“编译 PDF”。

我读过一辉的documentation,希望我没有错过一些非常明显的东西。

编辑新的代码结合了易辉的建议。

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

【问题讨论】:

  • 我编辑了您的帖子并删除了print(x),因为它既无意义又极具误导性。我希望你不介意。

标签: r knitr pandoc beamer r-markdown


【解决方案1】:

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

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

然后所有的图片都会写成PDF文件,LaTeX会很开心。

您的第二个问题是您将out.width / out.height 中的 HTML 单元与 LaTeX 单元混合在一起。 LaTeX 和 HTML 是非常不同的技术。你不应该期望\maxwidth 在 HTML 中工作,或在 LaTeX 中使用 200px。尤其是想把Markdown转成LaTeX的时候,最好不要设置out.width/out.height(使用fig.width/fig.height,让LaTeX使用原来的大小)。

【讨论】:

  • 这有帮助,但还没有完全实现。第一个情节仍然很大。第二个图很小,但分辨率非常低。我尝试将this similar question 中的代码直接添加到 .tex 文件中(编织后)。这控制了宽度,但它仍然从幻灯片的底部掉下来。对不起print()ing 基本图形。我使用 Ubuntu - 会话信息马上出现。
  • @nacnudus 你最好在再次运行 knitr 之前删除 figure 目录;关于 LaTeX 有很多黑暗的事实,例如默认情况下它更喜欢PNG而不是PDF,所以如果有foo.pngfoo.pdf\includegraphics{foo}将使用foo.png
  • 对我不起作用...情节仍在 pdf 的边界之外
【解决方案2】:

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

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

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

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

您也可以使用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
```

【讨论】:

  • @theforestecologist 在第一个选项之前可能需要一个逗号,例如{r chunk-name, opt=val, opt=val2}。这种方式对我有用。不过,我没有足够的针织知识来编辑它。
  • 带有fig_widthfig_weight 的yml 标头中的设置似乎不适用于output: pdf_document。这个怎么设置?
  • 此解决方案仅适用于 html 输出。
  • out.widthout.height 对我来说是不错的选择。谢谢。
猜你喜欢
  • 2021-08-26
  • 2013-06-14
  • 1970-01-01
  • 2017-03-26
  • 1970-01-01
  • 2016-11-14
  • 2014-03-04
  • 2013-09-23
  • 1970-01-01
相关资源
最近更新 更多