Reputation: 155
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:
Package gridExtra - I can easily setup the layout I want but I wasn't able to add captions to the plots.Is there an option to add captions to the plot in grid.arrange
?
Placing each plot in a different chunk and playing with R chunk options. Basically setting out.width='.49\\linewidth', fig.align='right'
and fig.align='left'
alternatively. Here I can set individual captions with fig.cap
but the plots always show up on separate pages.
I tried playing with the fig.width
and fig.height
options and was able to get them to display on the same page on their respective left or right sides of the page. However the caption always takes the full page width and stays in center instead of wrapping with the plot size. Is there a way to make caption follow the plot sizing rules?
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)
```
I'd appreciate any help. Thanks!
Upvotes: 2
Views: 6777
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
```
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