rearranging the legends on a ggplot
ggguides places, styles and combines the legends of
ggplot2 plots with named functions,
such as legend_left(), legend_inside() or legend_style(), each
added to a plot with +.
Each function sets the ggplot2 theme and guide options that belong to
one change together, for a legend on the left its position, its
justification and the alignment of the legend boxes, and returns an
ordinary ggplot2 component that combines with the rest of the plot’s
theme. For layouts of several plots, collect_legends() and
shared_legend() edit the gtable, the table of graphical objects that
ggplot2 draws, so that one legend remains. You choose where each legend
goes and how it looks, one aesthetic at a time when a plot has several.
ggguides supports:
- Placement.
legend_left(),legend_right(),legend_top()andlegend_bottom()move the legend to a side,legend_inside()into a corner or tox/ycoordinates, andlegend_none()removes it. - Styling.
legend_style()sets size, font, background, border and margins.legend_wrap()arranges the keys in columns or rows,legend_reverse()andlegend_order()set the order of the entries, andcolorbar_style()styles continuous scales. - Several legends on one plot. The
byargument of every placement and style function targets one aesthetic.legend_hide()andlegend_select()drop or keep legends by aesthetic, andlegend_order_guides()sets their order. - One legend for several plots.
collect_legends()andcollect_axes()remove duplicates from a patchwork layout,shared_legend()arranges plots with one legend without patchwork, andget_legend()extracts a legend for cowplot or grid.
install.packages("ggguides")Development version:
# install.packages("pak")
pak::pak("gcol33/ggguides")- Build the plot with ggplot2.
- Add a placement function such as
legend_left()orlegend_inside(). - Add
legend_style()to change how the legend looks.
library(ggplot2)
library(ggguides)
p <- ggplot(mtcars, aes(mpg, wt, colour = factor(cyl))) +
geom_point(size = 3)
p + legend_left()Inside the panel, a corner is named with position, and the style is
added as a further layer:
p +
legend_inside(position = "topright") +
legend_style(size = 18, title_face = "bold")In ggplot2, a legend on the left with its keys aligned at the top takes
three theme() arguments. legend_left() sets them together:
p + theme(legend.position = "left",
legend.justification.left = "top",
legend.box.just = "left")
p + legend_left(justification = "top")A plot with several aesthetics has one legend per aesthetic. The by
argument places or styles one of them and leaves the others as they are:
q <- ggplot(mtcars, aes(mpg, wt, colour = factor(cyl), size = hp)) +
geom_point()
q + legend_top(by = "colour") + legend_right(by = "size")Calls with by add up, one line per legend and setting. Multiple
Legends
builds this plot with six legends, two on top, two on the right, one at
the bottom and one on the left:
A patchwork layout repeats the legend of each plot. collect_legends()
keeps one, and span = TRUE stretches it along the full height of
stacked plots:
library(patchwork)
p1 <- ggplot(mtcars, aes(mpg, wt, colour = factor(cyl))) + geom_point()
p2 <- ggplot(mtcars, aes(mpg, hp, colour = factor(cyl))) + geom_point()
p3 <- ggplot(mtcars, aes(mpg, disp, colour = factor(cyl))) + geom_point()
stacked <- (p1 / p2 / p3) + plot_layout(heights = c(2, 1.5, 1))
gt <- collect_legends(stacked, position = "right", span = TRUE)
grid::grid.draw(gt)With span = 1:2 the legend sits beside rows 1 and 2 only. Without
patchwork, shared_legend(p1, p2, p3, ncol = 1, position = "bottom")
arranges the plots with one legend. Patchwork
Integration
covers both, along with collected axes and cowplot.
Start with Getting Started with ggguides: placing, styling and wrapping a legend, and combining the functions on one plot.
- Legend Positioning: the four sides, positions inside the panel, direction and removing the legend.
- Styling & Customization: fonts, text rotation, backgrounds, borders, key size, wrapping, entry order and colour bars.
- Multiple Legends: hiding, selecting, ordering, merging and placing legends per aesthetic.
- Patchwork Integration: one legend and shared axes across a layout of plots.
- Full reference: every function, grouped by job.
Questions and bug reports go to the GitHub issues. A bug report is easiest to act on with a small reproducible example, for instance made with reprex.
“Software is like sex: it’s better when it’s free.” — Linus Torvalds
I’m a PhD student who builds R packages in my free time because I believe good tools should be free and open. I started these projects for my own work and figured others might find them useful too.
If this package saved you some time, buying me a coffee is a nice way to say thanks. It helps with my coffee addiction.
ggguides sets options of ggplot2 and edits the plots it draws with gtable; multi-plot layouts come from patchwork.
ggguides is licensed under MIT.
Colling G (2026). _ggguides: Simplified Legend and Guide Alignment for
ggplot2_. doi:10.32614/CRAN.package.ggguides
<https://doi.org/10.32614/CRAN.package.ggguides>. R package version
1.1.10, <https://gillescolling.com/ggguides/>.
BibTeX:
@Manual{colling2026ggguides,
title = {ggguides: Simplified Legend and Guide Alignment for ggplot2},
author = {Gilles Colling},
year = {2026},
note = {R package version 1.1.10},
doi = {10.32614/CRAN.package.ggguides},
url = {https://gillescolling.com/ggguides/},
}