Skip to contents

rearranging the legends on a ggplot

ggguides places, styles and combines the legends of ggplot2 plots with named functions such as legend_left(), each added to a plot with +.

ggplot2 controls legends through its theme and guide settings, and a single change often touches several of them. Each ggguides function sets the settings that belong to one change together: for a legend on the left, its position, its justification along the edge and the alignment of the legend boxes. The result is an ordinary ggplot2 component, so it 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:

Placing two legends at different points of the same edge edits ggplot2’s legend layout when the plot is drawn. The package checks that layout against the ggplot2 version it was verified with (4.0.3); if a later ggplot2 arranges legends differently, it warns once and falls back to ggplot2’s whole-plot setting, so the plot still draws.

Installation

Install the released version from CRAN:

install.packages("ggguides")

Or install the development version from GitHub:

# install.packages("pak")
pak::pak("gcol33/ggguides")

Usage

  1. Build the plot with ggplot2.
  2. Add a placement function such as legend_left() or legend_inside().
  3. 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()

Scatter plot of car weight against fuel economy from mtcars, points coloured by number of cylinders, with the colour legend on the left of the panel. 2 3 4 5 10 15 20 25 30 35 mpg wt factor(cyl) 4 6 8

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")

The same scatter plot with the colour legend inside the panel in the top-right corner, its title in bold and its text larger. 2 3 4 5 10 15 20 25 30 35 mpg wt factor(cyl) 4 6 8

Compared with theme()

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")

Several legends on one plot

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")

Scatter plot of car weight against fuel economy with points coloured by cylinders and sized by horsepower; the colour legend is above the panel and the size legend to its right. 2 3 4 5 10 15 20 25 30 35 mpg wt hp 100 150 200 250 300 factor(cyl) 4 6 8

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:

Scatter plot of car weight against fuel economy with six legends: colour and fill above the panel, size and transparency on the right, shape below and line type on the left. 2 3 4 5 10 15 20 25 30 35 mpg wt HP 100 150 200 250 300 QSec 15.0 17.5 20.0 22.5 VS 0 1 AM 0 1 Cyl 4 6 8 Gear 3 4 5

One legend for several plots

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)

Three scatter plots of mtcars stacked vertically, of weight, horsepower and displacement against fuel economy, with one colour legend for cylinders on the right spanning all three panels. 2 3 4 5 10 15 20 25 30 35 mpg wt 100 200 300 10 15 20 25 30 35 mpg hp 100 200 300 400 10 15 20 25 30 35 mpg disp factor(cyl) 4 6 8

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.

Documentation

Start with Getting Started with ggguides: placing, styling and wrapping a legend, and combining the functions on one plot.

Getting help

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.

Support

“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.

Buy Me A Coffee

Acknowledgements

ggguides sets options of ggplot2 and edits the plots it draws with gtable; multi-plot layouts come from patchwork.

License

ggguides is licensed under MIT.

Citation

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/},
}

Download the citation as BibTeX or RIS.