您可以在 LaTeX 输出中使用子图,如下所述here https://stackoverflow.com/questions/12546365/subfigures-or-subcaptions-with-knitr/49086985#49086985。如果您有很多绘图并且希望更容易地提供标题,您可以在列表中指定这些标题(即captions <- c("Caption 1", "Caption 2"
) 在块之前并将此列表提供给块作为fig.subcap=captions
---
output: pdf_document
header-includes:
- \usepackage{subfig}
---
```{r}
captions <- c("Caption 1",
"Caption 2",
"Caption 3",
"Caption 4: a very very very very very very very very very long one")
```
```{r, echo=FALSE, cache=FALSE, results=FALSE, warning=FALSE, comment=FALSE, message= FALSE, eval =T, fig.cap = "Overall Caption", fig.subcap=captions, out.width='.49\\linewidth', fig.asp=1, fig.ncol = 2}
library(ggplot2)
p1<- ggplot(mpg, aes(displ, hwy, colour = class)) +
geom_point()+
theme(plot.caption = element_text(hjust = 0.5))
p2<- ggplot(mpg, aes(displ, hwy, colour = class)) +
geom_point()+
theme(plot.caption = element_text(hjust = 0.5))
p3<- ggplot(mpg, aes(displ, hwy, colour = class)) +
geom_point()+
theme(plot.caption = element_text(hjust = 0.5))
p4<- ggplot(mpg, aes(displ, hwy, colour = class)) +
geom_point()+
theme(plot.caption = element_text(hjust = 0.5))
p1
p2
p3
p4
```
Edit
为了确保子图之间有边距,您可以添加margins
加载包时的选项:
- \usepackage[margin = 8pt]{subfig}
查看包文档中的其他选项:http://anorien.csc.warwick.ac.uk/mirrors/CTAN/macros/latex/contrib/subfig/subfig.pdf http://anorien.csc.warwick.ac.uk/mirrors/CTAN/macros/latex/contrib/subfig/subfig.pdf