Skip to contents

Installation is covered in Getting Started with ggguides.

Overview

ggguides provides styling functions to customize legend appearance without diving into ggplot2’s theme element hierarchy. The main functions are:

Font Styling

Font Size

Adjust the overall text size (applies to both title and labels). The plot below uses the theme’s default legend text:

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

p + ggtitle("Default size")

Scatter plot of car weight against fuel economy titled Default size, coloured by cylinders, with the legend in the default text size. 2 3 4 5 10 15 20 25 30 35 mpg wt Cylinders 4 6 8 Default size

With size = 18, the legend title and labels are drawn at 18 points:

p + legend_style(size = 18) + ggtitle("size = 18")

Scatter plot of car weight against fuel economy titled size = 18, coloured by 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 size = 18

Font Family

Change the font family for legend text, here to a serif font:

p + legend_style(family = "serif") + ggtitle("serif")

Scatter plot of car weight against fuel economy titled serif, with the legend text in a serif font. 2 3 4 5 10 15 20 25 30 35 mpg wt Cylinders 4 6 8 serif

and to a monospaced font:

p + legend_style(family = "mono") + ggtitle("mono")

Scatter plot of car weight against fuel economy titled mono, with the legend text in a monospaced font. 2 3 4 5 10 15 20 25 30 35 mpg wt Cylinders 4 6 8 mono

Title Emphasis

Make the title stand out with separate size and face settings:

p + legend_style(
  size = 14,
  title_size = 20,
  title_face = "bold"
)

Scatter plot of car weight against fuel economy, points coloured by number of cylinders, with a large bold legend title above smaller labels. 2 3 4 5 10 15 20 25 30 35 mpg wt Cylinders 4 6 8

Text Rotation

For legends with long category names, rotate the labels to save space:

Basic Rotation

p_long <- ggplot(mpg, aes(displ, hwy, color = class)) +
  geom_point()

p_long + legend_style(angle = 45)

Scatter plot of highway mileage against engine displacement, points coloured by the seven car classes, with the legend labels rotated 45 degrees. 20 30 40 2 3 4 5 6 7 displ hwy class 2seater compact midsize minivan pickup subcompact suv

Vertical Labels

Use 90 degrees for fully vertical text. Key height is automatically adjusted to prevent label overlap:

p_long + legend_style(angle = 90)
#> legend_auto_fit: Legend (17.2cm) exceeds 95% of panel (10.7cm). Wrapping to 4 rows.

Scatter plot of highway mileage against engine displacement, points coloured by the seven car classes, with the legend labels rotated to vertical. 20 30 40 2 3 4 5 6 7 displ hwy class 2seater compact midsize minivan pickup subcompact suv

Title Rotation

Rotate the legend title independently of the labels:

p_long +
  legend_style(title_angle = 90, title_hjust = 0.5) +
  ggtitle("Rotated title only")

Scatter plot of highway mileage against engine displacement, points coloured by the seven car classes, titled Rotated title only, with the legend title rotated to vertical and the labels horizontal. 20 30 40 2 3 4 5 6 7 displ hwy class 2seater compact midsize minivan pickup subcompact suv Rotated title only

Title and labels can be rotated together, each by its own angle:

p_long +
  legend_style(angle = 45, title_angle = 90) +
  ggtitle("Both rotated")

Scatter plot of highway mileage against engine displacement, points coloured by the seven car classes, titled Both rotated, with the legend title vertical and the labels at 45 degrees. 20 30 40 2 3 4 5 6 7 displ hwy class 2seater compact midsize minivan pickup subcompact suv Both rotated

Background and Border

Add visual containers around the legend:

p + legend_style(
  background = "#FFF3E0"
)

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

p + legend_style(
  background = "#FFF3E0",
  background_color = "#FF9800"
)

Scatter plot of car weight against fuel economy, points coloured by number of cylinders, with 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

Key Size

Adjust the size of legend keys (color swatches):

p + legend_style(
  key_width = 1.5,
  key_height = 1.5
)

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

Margin

Control spacing around the legend:

p + legend_style(
  background = "#FFF3E0",
  margin = 0.5
)

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

Full Styling Example

Combine all styling options:

p + legend_style(
  size = 14,
  title_size = 19,
  title_face = "bold",
  family = "sans",
  key_width = 1.2,
  background = "#FFF3E0",
  background_color = "#FF9800",
  margin = 0.3
)

Scatter plot of car weight against fuel economy, points coloured by number of cylinders, with a bold legend title 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, legend_wrap() creates multi-column or multi-row layouts.

By Columns

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

By Rows

Rows suit a horizontal legend below or above the panel:

ggplot(mpg, aes(displ, hwy, color = class)) +
  geom_point() +
  legend_wrap(nrow = 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 rows. 20 30 40 2 3 4 5 6 7 displ hwy class 2seater compact midsize minivan pickup subcompact suv

Customizing Legend Keys

When plot aesthetics like small point sizes or low alpha values make legend keys hard to read, legend_keys() overrides the key appearance without affecting the plot.

Enlarging Small Points

Points drawn at size = 1 give legend keys of the same size:

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

p_small + ggtitle("Small points in legend")

Scatter plot of car weight against fuel economy titled Small points in legend, with small points and equally small legend keys. 2 3 4 5 10 15 20 25 30 35 mpg wt Cylinders 4 6 8 Small points in legend

legend_keys(size = 4) enlarges the keys and leaves the points in the panel unchanged:

p_small + legend_keys(size = 4) + ggtitle("Enlarged legend keys")

Scatter plot of car weight against fuel economy titled Enlarged legend keys, with small points and large legend keys. 2 3 4 5 10 15 20 25 30 35 mpg wt Cylinders 4 6 8 Enlarged legend keys

Removing Transparency

Points drawn with alpha = 0.3 give equally faint legend keys:

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

p_alpha + ggtitle("Transparent legend keys")

Scatter plot of car weight against fuel economy titled Transparent legend keys, with faint points and equally faint legend keys. 2 3 4 5 10 15 20 25 30 35 mpg wt Cylinders 4 6 8 Transparent legend keys

legend_keys(alpha = 1) draws the keys opaque:

p_alpha + legend_keys(alpha = 1) + ggtitle("Opaque legend keys")

Scatter plot of car weight against fuel economy titled Opaque legend keys, with faint points and opaque legend keys. 2 3 4 5 10 15 20 25 30 35 mpg wt Cylinders 4 6 8 Opaque legend keys

Combining Overrides

ggplot(mtcars, aes(mpg, wt, color = factor(cyl))) +
  geom_point(alpha = 0.3, size = 1) +
  legend_keys(size = 4, alpha = 1)

Scatter plot of car weight against fuel economy, points coloured by number of cylinders, with small faint points and large opaque legend keys. 2 3 4 5 10 15 20 25 30 35 mpg wt factor(cyl) 4 6 8

Targeting Specific Aesthetics

By default, legend_keys() applies to both colour and fill legends. Target specific aesthetics with the aesthetic parameter:

ggplot(mtcars, aes(factor(cyl), mpg, fill = factor(cyl))) +
  geom_boxplot(alpha = 0.5) +
  legend_keys(alpha = 1, aesthetic = "fill")

Box plots of fuel economy for four, six and eight cylinders, filled translucent by cylinders, with opaque legend keys. 10 15 20 25 30 35 4 6 8 factor(cyl) mpg factor(cyl) 4 6 8

Changing Symbol Shapes

Use named shapes for clarity instead of numeric codes:

p + legend_keys(shape = "square") + ggtitle("Square")

Scatter plot of car weight against fuel economy titled Square, with square legend keys. 2 3 4 5 10 15 20 25 30 35 mpg wt Cylinders 4 6 8 Square

p + legend_keys(shape = "diamond") + ggtitle("Diamond")

Scatter plot of car weight against fuel economy titled Diamond, with diamond legend keys. 2 3 4 5 10 15 20 25 30 35 mpg wt Cylinders 4 6 8 Diamond

Available shape names include: "circle", "square", "diamond", "triangle", "plus", "cross", "asterisk".

Filled Shapes with Outlines

Shapes 21-25 (or names ending in _filled) support both outline and fill colors, making legends more visible against any background. A white fill with a colored outline works with the color mapping alone:

p + legend_keys(shape = "circle_filled", fill = "white", stroke = 1.5) +
  ggtitle("White fill, colored outline")

Scatter plot of car weight against fuel economy titled White fill, colored outline, with legend keys drawn as white circles with coloured outlines. 2 3 4 5 10 15 20 25 30 35 mpg wt Cylinders 4 6 8 White fill, colored outline

A colored fill with a black outline needs both color and fill mapped:

ggplot(mtcars, aes(mpg, wt, color = factor(cyl), fill = factor(cyl))) +
  geom_point(size = 3, shape = 21, stroke = 1) +
  legend_keys(colour = "black", stroke = 1) +
  ggtitle("Colored fill, black outline")
#> Warning: Duplicated `override.aes` is ignored.

Scatter plot of car weight against fuel economy titled Colored fill, black outline, with legend keys drawn as coloured circles with black outlines. 2 3 4 5 10 15 20 25 30 35 mpg wt factor(cyl) 4 6 8 Colored fill, black outline

Note: For colored fills with a custom outline, you must map both color and fill in the plot aesthetics. This is a ggplot2 limitation—override.aes can only set static values, it cannot make fill inherit from color.

Shape types:

Type Shapes Fill from Outline from
Outline only "circle_open", "square_open", "diamond_open" N/A colour
Solid filled "circle", "square", "diamond" colour N/A
Fill + outline "circle_filled", "square_filled", "diamond_filled" fill colour

Reordering Legend Entries

legend_order() changes the order of legend entries without modifying factor levels in your data.

Explicit Order

Specify the exact order you want:

p + legend_order(c("8", "6", "4"))

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

Using Functions

Apply functions like rev or sort to the current order:

p + legend_order(rev) + ggtitle("Reversed")

Scatter plot of car weight against fuel economy titled Reversed, with the legend entries ordered 8, 6, 4. 2 3 4 5 10 15 20 25 30 35 mpg wt Cylinders 8 6 4 Reversed

p + legend_order(sort) + ggtitle("Sorted")

Scatter plot of car weight against fuel economy titled Sorted, with the legend entries ordered 4, 6, 8. 2 3 4 5 10 15 20 25 30 35 mpg wt Cylinders 4 6 8 Sorted

Other Aesthetics

Reorder legends for fill, shape, or other aesthetics:

ggplot(mtcars, aes(factor(cyl), fill = factor(cyl))) +
  geom_bar() +
  legend_order(c("8", "4", "6"), aesthetic = "fill")

Bar chart of car counts for four, six and eight cylinders, with the fill legend ordered 8, 4, 6. 0 5 10 4 6 8 factor(cyl) count factor(cyl) 8 4 6

Reversing Legend Order

For simple reversal, legend_reverse() is a convenient shorthand:

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

Combining Style Functions

All ggguides functions compose with +:

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() +
  legend_style(size = 14, title_face = "bold")

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

Styling Continuous Color Bars

For continuous scales, colorbar_style() customizes the color bar appearance.

Basic Usage

p_cont <- ggplot(faithfuld, aes(waiting, eruptions, fill = density)) +
  geom_tile()

p_cont

Heat map of Old Faithful eruption duration against waiting time, filled by density, with a colour bar on the right. 2 3 4 5 40 50 60 70 80 90 waiting eruptions density 0.01 0.02 0.03

Size Adjustments

Create taller, thinner bars:

p_cont + colorbar_style(width = 0.5, height = 10, aesthetic = "fill")

Heat map of Old Faithful eruption duration against waiting time, filled by density, with a colour bar; the colour bar is tall and thin. 2 3 4 5 40 50 60 70 80 90 waiting eruptions density 0.01 0.02 0.03

Horizontal Orientation

p_cont + colorbar_style(width = 10, height = 0.5, direction = "horizontal", aesthetic = "fill") +
  legend_bottom()

Heat map of Old Faithful eruption duration against waiting time, filled by density, with a colour bar; the colour bar is horizontal below the panel. 2 3 4 5 40 50 60 70 80 90 waiting eruptions density 0.01 0.02 0.03
### Adding a Frame

frame = TRUE draws a black frame:

p_cont + colorbar_style(frame = TRUE, aesthetic = "fill") +
  ggtitle("Black frame")

Heat map of Old Faithful eruption duration against waiting time, filled by density, with a colour bar, titled Black frame; the colour bar has a frame. 2 3 4 5 40 50 60 70 80 90 waiting eruptions density 0.01 0.02 0.03 Black frame

A color string sets the frame color:

p_cont + colorbar_style(frame = "#FF9800", aesthetic = "fill") +
  ggtitle("Orange frame")

Heat map of Old Faithful eruption duration against waiting time, filled by density, with a colour bar, titled Orange frame; the colour bar has an orange frame. 2 3 4 5 40 50 60 70 80 90 waiting eruptions density 0.01 0.02 0.03 Orange frame

Removing Ticks

p_cont + colorbar_style(ticks = FALSE, frame = "#FF9800", aesthetic = "fill")

Heat map of Old Faithful eruption duration against waiting time, filled by density, with a colour bar; the colour bar has an orange frame and no tick marks. 2 3 4 5 40 50 60 70 80 90 waiting eruptions density 0.01 0.02 0.03

Combined Customization

p_cont + colorbar_style(
  width = 0.5,
  height = 8,
  frame = "#E65100",
  ticks_length = 0.3,
  aesthetic = "fill"
)

Heat map of Old Faithful eruption duration against waiting time, filled by density, with a colour bar; the colour bar is tall and thin with a dark orange frame and long ticks. 2 3 4 5 40 50 60 70 80 90 waiting eruptions density 0.01 0.02 0.03

Summary

Function Purpose Key Parameters
legend_style() Comprehensive styling size, title_size, title_face, family, angle, background, margin
legend_keys() Override key appearance size, alpha, shape, fill, colour, stroke, aesthetic
legend_order() Reorder entries order (vector or function), aesthetic
legend_wrap() Multi-column layout ncol, nrow
legend_reverse() Reverse entry order None
colorbar_style() Continuous color bar width, height, frame, ticks, direction

Learn more: