Side-by-side ggplots with working cross-reference and title #684
-
This builds on #590 In the accepted answer both graphs have been given the same caption (the text from 'fig-cap'). And adding a cross-reference doesn't seem to work (see image below code). `
` Does anyone has qmd code that adds just one overall caption to the figure, rather than duplicating the same caption for each figure (which I'm hoping would fix the cross-reference issue) please? Or alternatively, allows for a separate caption and label for each side-by-side figure? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
One approach would be to use sub figures. This would allow you to provide captions for the overall figure as well as the individual plots. In addition, your cross reference can then refer to the whole figure or either subfigure. For example: ---
title: A Simple Plot Example
format: html
---
```{r}
#| label: fig-testing
#| fig-cap: "A pair of plots, laid out side by side."
#| fig-subcap: ["Cars","Air Quality"]
#| layout: "[1, 1]"
library(ggplot2)
plt <- ggplot(mtcars, aes(mpg, wt, colour = factor(cyl))) +
geom_point(size = 3) +
theme_classic()
plt
plt2 <- ggplot(airquality, aes(Wind, Temp)) +
geom_point() +
theme_classic()
plt2
```
The overall figure @fig-testing
The first sub figure @fig-testing-1
The second subfigure @fig-testing-2
which renders as: Would this work for you? |
Beta Was this translation helpful? Give feedback.
One approach would be to use sub figures. This would allow you to provide captions for the overall figure as well as the individual plots. In addition, your cross reference can then refer to the whole figure or either subfigure. For example: