【问题标题】:knitr/Rmarkdown: Figures side-by-side with a single captionknitr/Rmarkdown:图形并排显示单个标题
【发布时间】:2020-05-01 15:56:34
【问题描述】:

.Rmd 文档中,有几种方法可以并排生成图形。如果数字已经存在,对我来说最简单的方法是使用knitr::include_graphics(c(fig1, fig2, ...)) [Thx: Yihui!]

但是当我这样做时,我找不到添加整体图形标题的方法,这在 LaTeX 中很容易做到。 这是一个例子:

```{r, out.width = "30%", echo=FALSE}
# two figs side by side
include_graphics(c("fig/mathscore-data.png",
                   "fig/mathscore-data-ellipses.png"))
```

输出:

如果我将 fig.cap 添加到块选项中,我会得到两个单独的数字,每个数字都有相同的数字标题。

```{r, out.width = "30%", echo=FALSE, fig.cap="Left: scatterplot of mathscore data; Right: Data ellipses for the two groups."}
# two figs side by side
include_graphics(c("fig/mathscore-data.png",
                   "fig/mathscore-data-ellipses.png"))
```

有没有其他方法可以做我想做的事——两个并排的子图,有一个共同的标题?

【问题讨论】:

  • 您的输出是 HTML 还是 PDF 或其他?你的 YAML 标头是什么?
  • rmarkdown 的好处是你可以(对于 pdf )allwas fallback to latex 所以:
  • /begin{figure} /includegraphics[ ]{ } 等会起作用。

标签: r r-markdown knitr


【解决方案1】:

在 knitr 或纯乳胶中都可以。

knitr 中将块选项设置为:

echo=FALSE,out.width="49%",out.height="49%",fig.show='hold',

fig.align='center'

我使用了 latex 包子标题,

在 YAML 标头的 header-includes 部分中导入。

---
title: "Untitled"
author: "V"
date: "22 4 2020"
output: pdf_document
header-includes:
  - \usepackage{subcaption}
---


# add Two figures with latex

\begin{figure}[h]
\begin{subfigure}{.5\textwidth}
\includegraphics[]{CAT.png}
\end{subfigure}%
\begin{subfigure}{.5\textwidth}
\includegraphics[]{CAT.png}
\end{subfigure}
\caption{This is the main caption - Years 2020 - 2030 - the main caption. }
\end{figure}

#  add Two figures with Knitr 


```{r, echo=FALSE,out.width="49%",out.height="49%",fig.show='hold',
fig.align='center', fig.cap="This is the main caption - Years 2020 - 2030 - the main caption."}
knitr::include_graphics(c("CAT.png","CAT.png"))

【讨论】:

  • Thx @J_F 我缺少的是用于 HTML 输出的 fig.show="hold"
  • 有没有办法在两个数字下面得到一个(a)和(b)或类似的东西?
猜你喜欢
  • 1970-01-01
  • 2015-03-16
  • 1970-01-01
  • 2015-02-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-02
  • 1970-01-01
相关资源
最近更新 更多