Rmarkdown: Multiple plots on same page with separate captions

≯℡__Kan透↙ 提交于 2020-01-11 06:43:09

问题


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!


回答1:


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

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



来源:https://stackoverflow.com/questions/49907075/rmarkdown-multiple-plots-on-same-page-with-separate-captions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!