Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Left align ggplot caption

Tags:

r

ggplot2

ggtext

I'm using the R package ggtext to "plot align" (max left align) my title and subtitle. I also want to use these ggtext methods to "plot align" my caption.

library(tidyverse)
library(ggtext)
ggplot(mpg, aes(cty, hwy)) + 
  geom_point() +
  theme(plot.title.position = "plot",
        plot.caption.position = "plot",
        plot.title = element_markdown(),
        plot.subtitle = element_markdown(),
        plot.caption = element_markdown()) +
  labs(title = "This is the title.", 
       subtitle = "This is the subtitile.", 
       caption = "This is the caption.")

You'll probably notice the caption is right aligned, whereas the title and subtitle are "plot aligned".

right aligned caption

How do I "plot align" my caption?

like image 652
Display name Avatar asked Jan 22 '26 01:01

Display name


1 Answers

For the benefit of others, you can left align the caption in ggplot2 in the following way:

library(ggplot2)

ggplot(mpg, aes(cty, hwy)) + 
  geom_point() +
  theme(plot.caption = element_text(hjust = 0)) + # set the left align here
  labs(title = "This is the title.", 
       subtitle = "This is the subtitile.", 
       caption = "This is the caption.")

plot with caption left aligned

like image 164
Marcos Avatar answered Jan 24 '26 17:01

Marcos