Skip to contents

Installation

install.packages("ggguides")

Or the development version from GitHub:

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

Overview

ggguides simplifies common legend operations in ggplot2. Instead of memorizing theme element names and their nested structure, you can use intuitive one-liner functions to position, style, and manage legends.

Key features:

Basic Usage

Let’s create a simple plot to demonstrate the legend helpers:

p <- ggplot(mtcars, aes(mpg, wt, color = factor(cyl))) +
  geom_point(size = 3) +
  labs(color = "Cylinders")

p

Scatter plot of car weight against fuel economy, points coloured by number of cylinders, with the cylinders legend on the right. 2 3 4 5 10 15 20 25 30 35 mpg wt Cylinders 4 6 8

Moving the Legend

Position the legend on any side with a single function call:

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

Scatter plot of car weight against fuel economy, points coloured by number of cylinders, with the legend below the panel and its keys in a row. 2 3 4 5 10 15 20 25 30 35 mpg wt Cylinders 4 6 8

Inside Positioning

Place the legend inside the plot area using coordinates or shortcuts:

p + legend_inside(position = "topright")

Scatter plot of car weight against fuel economy, points coloured by number of cylinders, with the legend inside the panel in the top-right corner. 2 3 4 5 10 15 20 25 30 35 mpg wt Cylinders 4 6 8

p + legend_inside(x = 0.02, y = 0.98, justification = c("left", "top"))

Scatter plot of car weight against fuel economy, points coloured by number of cylinders, with the legend inside the panel in the top-left corner. 2 3 4 5 10 15 20 25 30 35 mpg wt Cylinders 4 6 8

Removing the Legend

Scatter plot of car weight against fuel economy, points coloured by number of cylinders, without a legend. 2 3 4 5 10 15 20 25 30 35 mpg wt

Styling Legends

Use legend_style() to customize the legend appearance:

p + legend_style(size = 18)

Scatter plot of car weight against fuel economy, points coloured by number of cylinders, with the legend title and labels drawn larger. 2 3 4 5 10 15 20 25 30 35 mpg wt Cylinders 4 6 8

p + legend_style(
  size = 15,
  title_size = 20,
  title_face = "bold",
  background = "#FFF3E0",
  background_color = "#FF9800"
)

Scatter plot of car weight against fuel economy, points coloured by number of cylinders, with a bold legend title and the legend on a cream box with an orange border. 2 3 4 5 10 15 20 25 30 35 mpg wt Cylinders 4 6 8

Wrapping Legend Entries

For legends with many entries, use legend_wrap() to create multi-column layouts:

ggplot(mpg, aes(displ, hwy, color = class)) +
  geom_point() +
  legend_wrap(ncol = 2)

Scatter plot of highway mileage against engine displacement, points coloured by the seven car classes, with the legend keys in two columns on the right. 20 30 40 2 3 4 5 6 7 displ hwy class 2seater compact midsize minivan pickup subcompact suv

Combining Functions

ggguides functions compose naturally with the + operator:

ggplot(mpg, aes(displ, hwy, color = class)) +
  geom_point() +
  legend_left() +
  legend_style(size = 15, title_face = "bold", background = "#FFF3E0")

Scatter plot of highway mileage against engine displacement, points coloured by the seven car classes, with the legend on the left on a cream box, its title in bold. 20 30 40 2 3 4 5 6 7 displ hwy class 2seater compact midsize minivan pickup subcompact suv

ggplot(mpg, aes(displ, hwy, color = class)) +
  geom_point() +
  legend_wrap(ncol = 2) +
  legend_bottom()

Scatter plot of highway mileage against engine displacement, points coloured by the seven car classes, with the legend below the panel and its keys in two columns. 20 30 40 2 3 4 5 6 7 displ hwy class 2seater compact midsize minivan pickup subcompact suv

What’s Next