Subfigures or Subcaptions with knitr?

2020-01-27 03:45发布

问题:

Is it possible to produce subfigures (with associated subcaptions) using knitr? Here is a minimal working example:

\documentclass{article}

\begin{document}

<<echo = FALSE, fig.cap = c("Some numbers.", "Some more numbers."), out.width = "0.5\\textwidth", fig.align = "center">>=

plot(1:10)
plot(30:100)

@

\end{document}

This results in two figures labelled Figure 1 and Figure 2 with captions as defined (respectively). But I want them to be labelled "Figure 1a" and "Figure 1b", as you can do with the subcaption LaTeX package.

I know there is a knitr options "fig.env", but this doesn't solve it (at least not using, for example, "fig.env = 'subfigure'"). There is a similar post here regarding Sweave, but the solution is an inelegant hack: http://texblog.org/2011/12/01/sweave-subfig-controlling-figure-size-and-placement/

回答1:

knitr (>= v1.5) supports subfigures. You can use the chunk option fig.subcap. Here is a minimal example.

\documentclass{article}
\usepackage{subfig}
\begin{document}

<<fig-sub, fig.cap='two plots', fig.subcap=c('one plot', 'the other one'), out.width='.49\\linewidth'>>=
plot(1:10)
plot(rnorm(10), pch=19)
@

\end{document}



回答2:

Updating the answer from Yihui to reflect the changes in Rmarkdown in the past few years, and also the shift away from Rnw files.

As listed within the knitr chunk options, subfigures require a few additional settings to be set in the chunk header:

  • fig.subcap is a list of the captions for subfigures
  • fig.ncol: the number of columns of subfigures
  • out.width: the output width of the figures. You will normally set this 100% divided by the number of sub columns.

Subfigures also require the LaTeX package subfig. The line \usepackage{subfig} must therefore be included within the YAML, or if you are using an external tex template you can add this to that file.

Here is a basic template:

---
output: pdf_document
header-includes:
   - \usepackage{subfig}
---  

```{r fig-sub, fig.cap='two plots', fig.subcap=c('one plot', 'the other one'), out.width='.49\\linewidth', fig.asp=1, fig.ncol = 2}
plot(1:10)
plot(rnorm(10), pch=19)
```

Using with ggplot2

If you are plotting subfigures which contains multiple ggplot plots, can messy as they do not line up between the plots. You may want to check out this post here on how to use cowplot to force ggplots to have the same dimensions.

This stackoverflow post will help you make sure that all the plots line up as shown in the image below:

Providing a list to subfigures

Just sharing this as a possible extension of subfigures. Something I like to use them for is feeding them a list of figures. Maybe you have produced a function which you need to run on four different trial groups, or in four separate locations. For example, here is a list of four cities in the UK:

```{r}
locations <- c("Southampton, UK", "London, UK", "Bristol, UK", "Birmingham, 
```

This list can then be used within the fig.sub and an lapply to produce a list of subfigures. If you need to run the

```{r fig-sub-2, fig.cap='A collection of maps', fig.subcap= locations, out.width='.49\\linewidth', fig.asp=1, fig.ncol = 2}
library(ggmap)
lapply(locations, function(x) 
  ggmap(get_map(x))
  )
```

This makes the subfigures quite robust. If I need to run my model in an extra couple of cities, all I need to do is add extra values to the location list and the sub figures will be the same length.



回答3:

You can use Latex's subcaption package.

\documentclass{article}
\usepackage{subcaption}

\begin{document}

\begin{figure}
\centering
\begin{subfigure}[b]{0.3\textwidth}
\centering
<<echo = FALSE, out.width = "0.5\\textwidth", fig.align = "center">>=
  plot(1:10)
@
\caption{text for first figure}
\label{fig:gull}
\end{subfigure}%

\begin{subfigure}[b]{0.3\textwidth}
\centering
<<echo = FALSE, out.width = "0.5\\textwidth", fig.align = "center">>=
  plot(30:100)
@
\caption{text for second figure}
\label{fig:tiger}
\end{subfigure}
\caption{Figure caption}
\end{figure}

\end{document}