shashwat vajpeyi
shashwat vajpeyi

Reputation: 155

Rmarkdown: Multiple plots on same page with separate captions

I am writing a report in R markdown with a pdf output. I have several plots and I would like to display four plots per page laid out in a 2x2 matrix. Is there a way to get them to display like that with separate captions?

Here is what I have tried so far:

Here is a sample code:

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

Here is the output image I'd appreciate any help. Thanks!

Upvotes: 2

Views: 6777

Answers (1)

Michael Harper
Michael Harper

Reputation: 15369

You can use subfigures within LaTeX Outputs, as describe here. If you have lots of plots and you want to provide the captions easier, you can specify these in a list (i.e. captions <- c("Caption 1", "Caption 2") before the chunk and the provide this list to the chunk as 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
```

enter image description here

Edit

To ensure there is margin between the subfigures, you can add the margins option when loading the package:

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

Check out the other options in the package documentation: http://anorien.csc.warwick.ac.uk/mirrors/CTAN/macros/latex/contrib/subfig/subfig.pdf

Upvotes: 10

Related Questions