r - HTML 中的 Knitr 和图形标题

标签 r ggplot2 knitr r-markdown

knitr 将参数 fig.cap 定义为

fig.cap: (NULL; character) figure caption to be used in a figure environment in LaTeX (in \caption{}); if NULL or NA, it will be ignored, otherwise a figure environment will be used for the plots in the chunk (output in \begin{figure} and \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)
```

意思是,每个数字都有在代码块参数 fig.cap = c("Caption 1", "Caption 2") 中定义的正确标题

然而,当它们被放置在 block 选项中时,跟踪字幕是具有挑战性的——尤其是在长时间的情况下。除了为每个图形创建两个单独的 block 并在 block 外插入标题之外,还有其他选择吗?

最佳答案

您可以设置eval.after="fig.cap"以便在 block 运行后评估图形标题。这样,您可以在 block 内定义您的标题。

---
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")
```

关于r - HTML 中的 Knitr 和图形标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35486935/

相关文章:

mysql - 如何关闭 RMySQL 中的结果集?

r - 使用 ggplot2 绘制 fitdist 图

r - 使用geom_text&facet_wrap : Error in unit(x, default.units): 'x'和 'units'的长度必须> 0

html - 允许绘图在 rmarkdown html 中突出 block 文本

r - 在 dplyr::mutate 中对字符向量中列出的多个变量求和

r - 拆分科学名称

r - pandoc 和 rmarkdown : Can't fetch an image one level above a rmarkdown document

latex - 上图使用 knitr 的标题(LaTeX/PDF)

r - "Hmisc"软件包或 namespace 无法加载-没有名为'latticeExtra'的软件包

r - 在 ggplot2 #2 的各个方面注释文本