Rmarkdown:同一页上的多个图有单独的标题

Rmarkdown: Multiple plots on same page with separate captions

我正在用 R markdown 编写一份带有 pdf 输出的报告。我有几个图,我想在 2x2 矩阵中每页显示四个图。有没有办法让它们以单独的字幕显示?

这是我目前尝试过的方法:

这是一个示例代码:

```{r, echo=FALSE, cache=FALSE, results=FALSE, warning=FALSE,  comment=FALSE, message= FALSE, eval =T, fig.height= 9}

p1<- ggplot(mpg, aes(displ, hwy, colour = class)) + 
geom_point()+
labs(caption = "Lorem ipsum dolor sit amet, consectetur adipiscing     elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. ")+
theme(plot.caption = element_text(hjust = 0.5))

p2<- ggplot(mpg, aes(displ, hwy, colour = class)) + 
geom_point()+
labs(caption = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.  ")+
theme(plot.caption = element_text(hjust = 0.5))

p3<- ggplot(mpg, aes(displ, hwy, colour = class)) + 
geom_point()+
labs(caption = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. ")+
theme(plot.caption = element_text(hjust = 0.5))

p4<- ggplot(mpg, aes(displ, hwy, colour = class)) + 
geom_point()+
labs(caption = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. ")+
theme(plot.caption = element_text(hjust = 0.5))

library(gridExtra)
grid.arrange(p1,p2,p3,p4)
```

我将不胜感激任何帮助。谢谢!

您可以在 LaTeX 输出中使用子图,如 here 所述。如果你有很多情节并且你想更容易地提供标题,你可以在块之前的列表(即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
```

编辑

为了保证子图之间有边距,可以在加载包的时候加上margins选项:

  - \usepackage[margin = 8pt]{subfig}

查看包文档中的其他选项:http://anorien.csc.warwick.ac.uk/mirrors/CTAN/macros/latex/contrib/subfig/subfig.pdf