Tho*_*del 3 r ggplot2 knitr r-markdown
knitr将参数fig.cap定义为
fig.cap :( NULL; character)在LaTeX的数字环境中使用的数字标题(在\ caption {}中); 如果是NULL或NA,它将被忽略,否则图形环境将用于块中的图(在\ begin {figure}和\ end {figure}中输出)
但是,对于HTML输出,以下工作:
---
title: "Caption Test"
author: "Some Author"
date: "February 18, 2016"
output: html_document
---
```{r}
library(ggplot2)
```
```{r, fig.cap = c("This is caption 1", "This is caption 2")}
## Plot 1
qplot(carat, price, data = diamonds)
## Plot 2
qplot(carat, depth, data = diamonds)
```
Run Code Online (Sandbox Code Playgroud)
意思是,每个数字都会在代码块参数中定义正确的标题 fig.cap = c("Caption 1", "Caption 2")
但是,跟踪字幕是很有挑战性的 - 特别是如果长时间将它们放在块选项中.除了为每个图形创建两个单独的块,并且在块外部插入了标题,还有其他选项吗?
您可以进行设置,eval.after="fig.cap"以便在运行块后评估图形标题.这样,您就可以在块内定义标题.
---
title: "Caption Test"
author: "Some Author"
date: "February 18, 2016"
output: html_document
---
```{r}
library(ggplot2)
library(knitr)
opts_knit$set(eval.after = 'fig.cap')
```
```{r, fig.cap = cap}
## Plot 1
qplot(carat, price, data = diamonds)
cap <- "This is caption 1"
## Plot 2
qplot(carat, depth, data = diamonds)
cap <- c(cap, "This is caption 2")
```
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2964 次 |
| 最近记录: |