0

I am trying to put 4 plots together and add a long caption (Figure caption) underneath of the plots. However, when I try to add the caption, it puts the wrapped caption over top of the graphs. Additionally, there is a part of my caption that needs to be superscripted in the middle, but the caption won't allow for the superscript.

The following code is what I have been trying.

caption<-"Long arbitrary caption with word^superscripted in the middle. "

annotate_figure((ggarrange(HR1Plot,HR3Plot,HR12Plot,HR24Plot,
      ncol=2,nrow=2,
      labels=c("A","B","C","D"))),
      fig.lab = caption,
      fig.lab.pos = "bottom.left"
      )

The caption is now over the entire graph instead of under the graphs. I'm also asking for help on how to superscript the word in the middle.

stefan
  • 90,330
  • 6
  • 25
  • 51

1 Answers1

0

One option to fix your issue would be to switch to the patchwork package to glue your plots together and which allows to add a title, caption, ... the same way as we would do for a single ggplot. Second, to fix your issue with the superscript you could create your caption as a ?plotmath expression.

Using a minimal reproducible example based on mtcars:

library(ggplot2)
library(patchwork)

HR1Plot <- HR3Plot <- HR12Plot <- HR24Plot <- ggplot(mtcars, aes(hp, mpg)) +
  geom_point()

caption <- expression(
  Long ~ arbitrary ~ caption ~ with ~
  word^{superscripted} ~ "in" ~ the ~ middle.
)

list(HR1Plot, HR3Plot, HR12Plot, HR24Plot) |>
  wrap_plots(ncol = 2) &
  plot_annotation(tag_levels = "A", caption = caption) &
  theme(
    plot.caption = element_text(hjust = 0),
    plot.tag = element_text(face = "bold")
  )

EDIT First, wrap_plots requires the patchwork package which you have to install and attach as I do. Second, plotmath could be a bit tricky, i.e. you have to wrap the , in quotes, i.e. use ",". Also besides the ~ separattor which adds a space there is also a * separator.

library(ggplot2)
library(patchwork)

HR1Plot <- HR3Plot <- HR12Plot <- HR24Plot <- ggplot(mtcars, aes(hp, mpg)) +
  geom_point()

caption <- expression(
  th~or~without~forskolin~on~S16~cell~viability.~
    Using~the~CyQUANT^TM~MTT~Cell~Viability~Assay~Kit~(Thermo~Fisher)*","
  ~the~immortalized~rat~S16~cell~line~(ATCC~CRL-2941)~was~treated~with~0*","~0.1,
)

list(HR1Plot, HR3Plot, HR12Plot, HR24Plot) |>
  wrap_plots(ncol = 2) &
  plot_annotation(tag_levels = "A", caption = caption) &
  theme(
    plot.caption = element_text(hjust = 0),
    plot.tag = element_text(face = "bold")
  )

However, while plotmath is the way to go for math expression, in your case I would suggest to switch to e.g. ggtext to add your superscript text. The latter allows to add use some basic HTML, CSS or markdown to format text, e.g. you could add a superscript via a <sup> tag. Additionally, as you caption is quite long ggtext makes it easy to split your caption over mutliple lines (which plotmath does not) and via ggtext::elment_textbox_simple you could achieve that more or less automatically:


library(ggtext)

caption <- "
  th or without forskolin on S16 cell viability. 
    Using the CyQUANT<sup>TM</sup> MTT Cell Viability Assay Kit (Thermo Fisher)
   the immortalized rat S16 cell line (ATCC CRL-2941) was treated with 0, 0.1,
"

list(HR1Plot, HR3Plot, HR12Plot, HR24Plot) |>
  wrap_plots(ncol = 2) &
  plot_annotation(tag_levels = "A", caption = caption) &
  theme(
    plot.caption = ggtext::element_textbox_simple(hjust = 0),
    plot.tag = element_text(face = "bold")
  )

stefan
  • 90,330
  • 6
  • 25
  • 51
  • I am attempting this and for the caption, I am getting the following error: Error: unexpected '~' in "th~or~without~forskolin~on~S16~cell~viability.~Using~the~"CyQUANT^TM"~MTT~Cell~Viability~Assay~Kit~(Thermo~Fisher),~the~immortalized~rat~S16~cell~line~(ATCC~CRL-2941)~was~treated~with~0,~0.1,~" I also get the following error when trying to do the list function: Error in wrap_plots(list(HR1Plot, HR3Plot, HR12Plot, HR24Plot), ncol = 2) : could not find function "wrap_plots" – Mackenzie Wilcox Apr 09 '23 at 00:23
  • `plotmath` could be a bit tricky at the beginning. Instead I would suggest to switch to the `ggtext` package. See my edit. – stefan Apr 10 '23 at 10:05