Modifies a ggplot so that legend titles are centered over the key column only, rather than over the full legend width (keys + labels). This is particularly useful when legend labels are rotated, as the default centering places the title too far to the right.
Value
A modified gtable object that can be drawn with grid::grid.draw()
or saved with ggplot2::ggsave().
Details
This function works by modifying the legend's internal gtable structure, restricting the title's column span to only the keys column. Long titles will automatically wrap to fit within the key column width, and proper spacing is added to prevent overlap with rotated labels.
The title should have hjust = 0.5 set (done automatically by
legend_style(angle = ...)) for proper centering.
Examples
library(ggplot2)
p <- ggplot(mpg, aes(displ, hwy, color = class)) +
geom_point() +
legend_style(angle = 45) +
labs(color = "Vehicle Class")
# Center title over keys only (long titles wrap automatically)
# Returns a gtable - use grid::grid.draw() to render
# \donttest{
g <- center_legend_title(p)
grid::grid.draw(g)
# }