I am creating a .docx document from rmarkdown. I use bookdown
rendering as I like to use text references. I'm having difficulty with the caption for a figure that contains two plots, for which I need a caption with superscripts. Rendering to pdf works fine, but the caption does not appear in the docx.
---
title: "Untitled"
output:
bookdown::word_document2:
number_sections: false
bookdown::pdf_document2:
number_sections: false
bookdown::html_document2:
number_sections: false
date: "2023-06-19"
always_allow_html: false
bibliography: C:/Users/mrd19rph/OneDrive - Bangor University/PhD Folder/General introduction/library.bib
---
'''{r setup, include=FALSE}
library(knitr)
knitr::opts_chunk$set(echo = FALSE, warning = F, message = F)
library(ggplot2)
'''
Details are shown in Figures \@ref(fig:testfig) and \@ref(fig:testfig2).
(ref:testfigcap) Caption for figure with a citation [@Borg1967].
'''{r testfig, out.height= "20%", fig.cap="(ref:testfigcap)"}
ggplot(iris, aes(x = Sepal.Length , y = Petal.Length))+geom_point()
'''
Text here.
(ref:testfigcap2) Caption containing text^superscript^ for A: by hp and B by wt
'''{r testfig2, fig.show='hold', out.width= "55%", fig.cap= '(ref:testfigcap2)'}
plota = ggplot(mtcars, aes(x= mpg, y =hp))+geom_point()+
labs(title = "A")
plotb = ggplot(mtcars, aes(x= mpg, y =wt))+geom_point()+
labs(title = "B")
plota
plotb
'''
The use of the text reference for a caption works for the single plot figure, but not for the double plot figure.
Word rendering
I think this is a limitation of Word, i.e. you can't have a caption for two separate figures. One possible option to fix that would be to create one plot by glueing your two charts together using e.g. gridExtra::grid.arrange
or patchwork
or ...:
---
title: "Untitled"
output:
bookdown::word_document2:
number_sections: false
bookdown::pdf_document2:
number_sections: false
bookdown::html_document2:
number_sections: false
date: "2023-06-19"
always_allow_html: false
---
```{r setup, include=FALSE}
library(knitr)
knitr::opts_chunk$set(echo = FALSE, warning = F, message = F)
library(ggplot2)
```
Details are shown in Figures \@ref(fig:testfig) and \@ref(fig:testfig2).
(ref:testfigcap) Caption for figure with a citation [@Borg1967].
```{r testfig, out.height= "20%", fig.cap="(ref:testfigcap)"}
ggplot(iris, aes(x = Sepal.Length, y = Petal.Length)) +
geom_point()
```
Text here.
(ref:testfigcap2) Caption containing text ^superscript^ for A: by hp and B by wt
```{r testfig2, fig.cap= '(ref:testfigcap2)'}
plota <- ggplot(mtcars, aes(x = mpg, y = hp)) +
geom_point() +
labs(title = "A")
plotb <- ggplot(mtcars, aes(x = mpg, y = wt)) +
geom_point() +
labs(title = "B")
gridExtra::grid.arrange(plota, plotb, nrow = 1)
```
The use of the text reference for a caption works for the single plot figure, but not for the double plot figure.