I would like to left align the plot.title
, plot.subtitle
and plot.caption
in a horizontal ggplot2 barchart.
Example:
library("ggplot2") # ggplot2 2.2
df <- data.frame(type=factor(c("Brooklyn",
"Manhatten and\n Queens")),
value=c(15,30))
# manual hjust for title, subtitle & caption
myhjust <- -0.2
ggplot(df,
aes(x=type, y=value)) +
geom_bar(stat='identity') +
coord_flip() +
labs(
title = "This is a nice title",
subtitle = "A subtitle",
caption = "We even have a caption. A very long one indeed.") +
theme(axis.title=element_blank(),
plot.title=element_text(hjust = myhjust),
plot.subtitle=element_text(hjust = myhjust ),
plot.caption=element_text(hjust = myhjust))
How can I align all 3 labs
elements (plot.title
, plot.subtitle
and plot.caption
) to where the axis.text
starts (red vertical line, "M" of Manhatten)?
Besides: Why does a fixed myhjust
result in 3 different horizontal positions for plot.title
, plot.subtitle
and plot.caption
?
While you could edit those three grobs, you can also just:
Make a wrapper for it, put it in a personal pkg. Boom. Done.
This question refers to this github tidyverse/ggplot2 solved issue: https://github.com/tidyverse/ggplot2/issues/3252
And it is implemented in ggplot2 (development version): https://github.com/tidyverse/ggplot2/blob/15263f7580d6b5100989f7c1da5d2f5255e480f9/NEWS.md
To follow your example as a reprex:
Created on 2019-09-04 by the reprex package (v0.3.0)