Using element_markdown() from ggtext to place two captions in a ggplot figure

591 views Asked by At

I am trying to create a ggplot2 figure with two captions, one left-aligned and one right-aligned. This works properly when using element_text() in theme() to format the figures.

  ggplot(mtcars) + 
  aes(x=cyl, y=disp) + 
  geom_point() + 
  labs(caption=c("First caption here","Second caption here")) + 
  theme(plot.caption=element_text(hjust=c(0,1)))

Figure created using element_text()

When I attempt to replicate this with element_markdown() from the ggtext package instead of element_text(), it does not place the captions properly.

  ggplot(mtcars) + 
  aes(x=cyl, y=disp) + 
  geom_point() + 
  labs(caption=c("First caption here","Second caption here")) + 
  theme(plot.caption=element_markdown(hjust=c(0,1)))

Figure created using element_markdown()

Ideally, I'd like to use element_markdown() because it would allow more formatting options in the captions (bolding text, including images, etc). I'd greatly appreciate any advice on how to troubleshoot this. Thank you!

1

There are 1 answers

1
Cullen.Molitor On

I am unsure why this is not working the same way but you could simply change the hjust to a more extreme value. It will be harder to get exactly right but should work.

library(ggplot2)
library(ggtext)
ggplot(mtcars) + 
  aes(x=cyl, y=disp) + 
  geom_point() + 
  labs(caption=c("First caption here","Second caption here")) + 
  theme(plot.caption=element_markdown(hjust=c(0, -5)))

enter image description here