A one-liner to position the legend on the left side of the plot. Slides the
legend along the left rail via justification, and left-aligns multiple
legend boxes via legend.box.just.
Arguments
- justification
Where the legend sits along the left edge. One of
"top","center","bottom", or a numeric value in[0, 1](0 = bottom, 1 = top). Default is"center". Only used whenbyis NULL. For per-guide justification, uselegend_style(by = ..., justification = ...).- by
Optional aesthetic name (character) to position only a specific legend. When specified, uses per-guide positioning via
guide_legend(position = "left"). Requires ggplot2 >= 3.5.0. Common values:"colour","fill","size".
Details
The left-positioned legend lives in a vertical rail along the panel's left
edge. justification slides it along that rail: "top" pins the
legend's top edge to the panel top; "bottom" pins its bottom edge to
the panel bottom; "center" centers it vertically.
Note the naming asymmetry with legend_inside: for side legends
the justification keyword refers to where the legend sits along the panel
edge; for inside legends it refers to which corner of the legend anchors to
the (x, y) position.
When by is NULL (default), this function sets:
legend.position = "left"legend.justification.left = justificationlegend.box.just = "left"to left-align multiple legend boxes
When by is specified, only the legend for that aesthetic is moved.
Examples
library(ggplot2)
# Basic usage — legend centered vertically on the left
ggplot(mtcars, aes(mpg, wt, color = factor(cyl))) +
geom_point() +
legend_left()
# Pin legend to the top of the left rail
ggplot(mtcars, aes(mpg, wt, color = factor(cyl))) +
geom_point() +
legend_left(justification = "top")
# Position only the colour legend on the left
ggplot(mtcars, aes(mpg, wt, color = factor(cyl), size = hp)) +
geom_point() +
legend_left(by = "colour") +
legend_bottom(by = "size")