在 R markdown 中包含图形标签以进行并排绘图 [英] Include figure labels in R markdown for side by side plots

查看:22
本文介绍了在 R markdown 中包含图形标签以进行并排绘图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 rmarkdown 文档中,我想并排包含绘图以节省空间.例如,我想包括:

情节(rnorm(100))历史(runif(100))

情节(rnorm(100))历史(runif(100))

我真的不在乎两个子图都有一个标题,还是每个子图都有一个标题.我只是真的想并排包含数字,并有一些方法可以引用它们(图 1 等).有人有建议吗?我的标题中有这个:

标题包括:- usepackage{subfig}

当我的块中没有fig.show='hold'"时,我的所有字幕都可以正常工作,但我的情节不会并排显示.当我添加 fig.show='hold' 时,布局看起来很棒,但标题消失了.

解决方案

In my rmarkdown document, I want to include plots side by side to save space. For example, I want to include:

plot(rnorm(100))
hist(runif(100))

or

plot(rnorm(100))
hist(runif(100))

I don't really care if there is one caption for both subplots or one caption for each subplot. I just really want to include figures side by side and have some way to refer to them (Figure 1, etc). Does anyone have suggestions? I have this in my header:

header-includes: - usepackage{subfig}

When I don't have "fig.show='hold' " in my chunks, all of my captions work fine but my plots do not show up side by side. When I add fig.show='hold', the layout looks great but the captions disappear.

解决方案

Adapting my answer from this post, you can combine cross-referencing by including the output format bookdown::pdf_document2". Note that I am manually appending the subfigure letter to the cross reference i.e. @ref(fig:fig-sub)a:

---
output: bookdown::pdf_document2
header-includes:
   - usepackage{subfig}
---  

See Figures @ref(fig:fig-sub)a and @ref(fig:fig-sub)b

```{r fig-sub, echo = FALSE, fig.cap='two plots', fig.subcap=c('one plot', 'the other one'), out.width='.49\linewidth', fig.asp=1, fig.ncol = 2}
plot(1:10)
plot(rnorm(10), pch=19)
```

这篇关于在 R markdown 中包含图形标签以进行并排绘图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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