Include figure labels in R markdown for side by side plots

柔情痞子 提交于 2019-12-25 01:19:02

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/55084140/include-figure-labels-in-r-markdown-for-side-by-side-plots

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