This is a wrapper around cowplot::draw_label() that creates a ggplot-based label that inherits formatting from a given theme element. It's more or less been superceded by ggplot's new plot.title.position theme argument.
themed_label(label, theme = NULL, element = "text", x = 0.01, hjust = 0, ...)A string of text for label.
A ggplot theme; if NULL (the default), will get current theme with ggplot2::theme_get().
Name of a theme element; defaults to base text.
x-position; defaults to 0.01
Horizontal alignment; defaults 0
Any other arguments to pass to cowplot::draw_label().
A ggplot object.
if (FALSE) {
if (requireNamespace("ggplot2", quietly = TRUE)) {
town_pops <- race_pops %>%
dplyr::filter(variable == "total") %>%
dplyr::mutate(name = forcats::fct_reorder(as.factor(name), estimate))
library(ggplot2)
p <- ggplot(town_pops, aes(x = name, y = estimate)) +
geom_col() +
coord_flip()
# With long labels on the left, ggplot's default title placement
# aligned to the panel can become awkward
p + ggtitle("Total population by town, 2017")
# Instead, make a label grob and arrange plot elements how you want
title <- themed_label("Total population by town, 2017", element = "plot.title")
cowplot::plot_grid(
title,
p,
ncol = 1,
rel_heights = c(1, 10)
)
}
}