Learn R Programming

ggguides (version 1.1.4)

center_legend_title: Center Legend Title Over Keys

Description

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.

Usage

center_legend_title(plot, position = "all")

Value

A modified gtable object that can be drawn with grid::grid.draw()

or saved with ggplot2::ggsave().

Arguments

plot

A ggplot object.

position

Legend position to modify. One of "right", "left", "top", "bottom", or "all" (default).

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.

See Also

legend_style

Examples

Run this code
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
g <- center_legend_title(p)
grid::grid.draw(g)

Run the code above in your browser using DataLab