Rmarkdown:同一页面上的多个图,带有单独的标题 [英] Rmarkdown: Multiple plots on same page with separate captions

查看:138
本文介绍了Rmarkdown:同一页面上的多个图,带有单独的标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

这是我迄今为止尝试过的:

  • Package gridExtra - 我可以轻松设置我想要的布局,但我无法为绘图添加标题.是否可以选择在 grid.arrange?

  • 将每个情节放在不同的块中并使用 R 块选项.基本上设置 out.width='.49\\linewidth', fig.align='right'fig.align='left' 交替.在这里,我可以使用 fig.cap 设置单独的标题,但这些图总是显示在单独的页面上.

  • 我尝试使用 fig.widthfig.height 选项,并且能够让它们显示在各自左侧的同一页面上或页面的右侧.然而,标题总是占据整个页面的宽度并保持在中心而不是用绘图大小换行.有没有办法让标题遵循情节大小规则?

这是一个示例代码:

```{r, echo=FALSE, cache=FALSE, results=FALSE, warning=FALSE, comment=FALSE, message= FALSE, eval =T, fig.height= 9}p1<- ggplot(mpg, aes(displ, hwy, color = class)) +geom_point()+实验室(标题=Lorem ipsum dolor sat amet,consectetur adipiscing elit,sed do eiusmod tempor incididunt ut laboure et dolore magna aliqua.Ut enim ad minim veniam,quis nostrud exercitation ullamcolaboris nisi ut aliquip exeritation"主题(plot.caption = element_text(hjust = 0.5))p2<- ggplot(mpg, aes(displ, hwy, color = class)) +geom_point()+实验室(标题=Lorem ipsum dolor sat amet,consectetur adipiscing elit,sed do eiusmod tempor incididunt ut laboure et dolore magna aliqua.Ut enim ad minim veniam,quis nostrud exercitation ullamcolaboris nisi ut aliquip exeritation"主题(plot.caption = element_text(hjust = 0.5))p3<- ggplot(mpg, aes(displ, hwy, color = class)) +geom_point()+实验室(标题=Lorem ipsum dolor sat amet,consectetur adipiscing elit,sed do eiusmod tempor incididunt ut laboure et dolore magna aliqua.Ut enim ad minim veniam,quis nostrud exercitation ullamcolaboris nisi ut aliquip exeritation"主题(plot.caption = element_text(hjust = 0.5))p4<- ggplot(mpg, aes(displ, hwy, color = class)) +geom_point()+实验室(标题=Lorem ipsum dolor sat amet,consectetur adipiscing elit,sed do eiusmod tempor incididunt ut laboure et dolore magna aliqua.Ut enim ad minim veniam,quis nostrud exercitation ullamcolaboris nisi ut aliquip exeritation"主题(plot.caption = element_text(hjust = 0.5))图书馆(gridExtra)grid.arrange(p1,p2,p3,p4)``

我很感激任何帮助.谢谢!

解决方案

您可以在 LaTeX 输出中使用子图,如

编辑

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

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

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

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!

解决方案

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

这篇关于Rmarkdown:同一页面上的多个图,带有单独的标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆