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))
)
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))
)
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.