Remove ggplot title and caption indentation

My chart has a long title and caption and I am trying to align them to the very left of the plot without indentation. Let me explain my problem with an example:

date <-  c('2000-01-01',
           '2000-02-01',
           '2000-03-01',
           '2000-04-01',
           '2000-05-01')
value <- c(23, 41, 32, 58, 26)
df <- data.frame(date, value)

library(ggplot2)

When I use the \n operator to break up the lines, I get the title and caption to appear on multiple lines, all left adjusted.

ggplot(df, aes(date, value)) +
  geom_col() +
  labs(
    title = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit,\nsed do eiusmod tempor incididunt ut labore et dolore magna\naliqua.',
    caption = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit,\nsed do eiusmod tempor incididunt ut labore et dolore magna aliqua.',
    x = NULL,
    y = NULL
  ) +
  theme_classic() +
  theme(
    plot.caption = element_text(hjust = 0),
    plot.background = element_rect(color = 'black', size = 0.5))
  ) 

image

It looks pretty good, but I a trying to get both the title and caption to be in the left most position, all the way up against the left wall of the plot. When I add some negative hjust positions for the plot.title and plot.caption, I get some unexpected indentation...

ggplot(df, aes(date, value)) +
  geom_col() +
  labs(
    title = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit,\nsed do eiusmod tempor incididunt ut labore et dolore magna\naliqua.',
    caption = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit,\nsed do eiusmod tempor incididunt ut labore et dolore magna aliqua.',
    x = NULL,
    y = NULL
  ) +
  theme_classic() +
  theme(
    plot.title = element_text(hjust = -0.05),
    plot.caption = element_text(hjust = -0.11),
    plot.background = element_rect(color = 'black', size = 0.5))
  )

image

Can someone please help me get rid of this indentation, so the multiple lines continue to be aligned. I have no idea why changing the hjust positioning does this - I've been trying to troubleshoot this problem for months.

1 Like

You can use the theme parameters plot.title.position and plot.caption.position. See ?theme:

Alignment of the plot title/subtitle and caption. The setting for plot.title.position applies to both the title and the subtitle. A value of "panel" (the default) means that titles and/or caption are aligned to the plot panels. A value of "plot" means that titles and/or caption are aligned to the entire plot (minus any space for margins and plot tag).

So using

  theme(
    plot.title.position = "plot",
    plot.caption = element_text(hjust = 0),
    plot.caption.position = "plot",
    plot.background = element_rect(color = 'black', size = 0.5)
)

you get what you want:

3 Likes

Thank you so much! This is exactly what I have been looking for!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.