Rmarkdown:同一页上的多个图有单独的标题
Rmarkdown: Multiple plots on same page with separate captions
我正在用 R markdown 编写一份带有 pdf 输出的报告。我有几个图,我想在 2x2 矩阵中每页显示四个图。有没有办法让它们以单独的字幕显示?
这是我目前尝试过的方法:
Package gridExtra - 我可以轻松设置我想要的布局,但我无法向 plots.Is 添加字幕,那里有一个选项可以将字幕添加到 [=11 中的绘图=]?
将每个地块放在不同的块中并使用 R 块选项。基本上交替设置 out.width='.49\linewidth', fig.align='right'
和 fig.align='left'
。在这里,我可以使用 fig.cap
设置单独的标题,但图表总是显示在不同的页面上。
我尝试使用 fig.width
和 fig.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, 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
我正在用 R markdown 编写一份带有 pdf 输出的报告。我有几个图,我想在 2x2 矩阵中每页显示四个图。有没有办法让它们以单独的字幕显示?
这是我目前尝试过的方法:
Package gridExtra - 我可以轻松设置我想要的布局,但我无法向 plots.Is 添加字幕,那里有一个选项可以将字幕添加到 [=11 中的绘图=]?
将每个地块放在不同的块中并使用 R 块选项。基本上交替设置
out.width='.49\linewidth', fig.align='right'
和fig.align='left'
。在这里,我可以使用fig.cap
设置单独的标题,但图表总是显示在不同的页面上。我尝试使用
fig.width
和fig.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, 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