Skip to contents

Installation is covered in Getting Started with ggguides.

Overview

ggguides provides position helpers that handle legend placement without requiring knowledge of ggplot2’s theme system. Each function sets the appropriate combination of legend.position, legend.justification, and legend.box.just to achieve proper alignment.

Basic Positioning

Left and Right

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

p + legend_left() + ggtitle("Left")

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

p + legend_right() + ggtitle("Right")

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

Top and Bottom

When placing legends at the top or bottom, the legend is automatically oriented horizontally:

p + legend_top() + ggtitle("Top")

Scatter plot of car weight against fuel economy titled Top, coloured by cylinders, with the legend above the panel in a row. 2 3 4 5 10 15 20 25 30 35 mpg wt Cylinders 4 6 8 Top

p + legend_bottom() + ggtitle("Bottom")

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

Alignment with Titles

Use the align_to parameter to control how the legend aligns with the plot. The difference is most visible when there’s a y-axis label. By default the legend aligns with the panel:

p_labeled <- p + labs(title = "Fuel Economy", y = "Weight (1000 lbs)")

p_labeled + legend_top() + ggtitle("Panel alignment (default)")

Scatter plot of car weight against fuel economy titled Panel alignment, with the legend above the panel aligned to the panel's extent. 2 3 4 5 10 15 20 25 30 35 mpg Weight (1000 lbs) Cylinders 4 6 8 Panel alignment (default)

With align_to = "plot" the legend aligns with the full plot, including the axis title:

p_labeled + legend_top(align_to = "plot") + ggtitle("Plot alignment")

Scatter plot of car weight against fuel economy titled Plot alignment, with the legend above the panel aligned to the full plot including the axis title. 2 3 4 5 10 15 20 25 30 35 mpg Weight (1000 lbs) Cylinders 4 6 8 Plot alignment

Inside Positioning

legend_inside() places the legend within the plot panel. You can use either coordinate values or named shortcuts.

Using Shortcuts

Available shortcuts: "topright", "topleft", "bottomright", "bottomleft", "center"

p + legend_inside(position = "topright") + ggtitle("Top right")

Scatter plot of car weight against fuel economy titled Top right, 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 Top right

p + legend_inside(position = "bottomleft") + ggtitle("Bottom left")

Scatter plot of car weight against fuel economy titled Bottom left, with the legend inside the panel in the bottom-left corner. 2 3 4 5 10 15 20 25 30 35 mpg wt Cylinders 4 6 8 Bottom left

Using Coordinates

For precise control, specify x/y coordinates (0-1 scale) and justification:

p + legend_inside(x = 0.95, y = 0.95, justification = c("right", "top"))

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

Adding Background

Inside legends often need a background for readability:

p + legend_inside(
  position = "topright",
  background = "#FFF3E0",
  border = "#FF9800"
)

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 on a cream box with an orange border. 2 3 4 5 10 15 20 25 30 35 mpg wt Cylinders 4 6 8

Direction Control

Horizontal and Vertical

Control legend entry direction independently of position:

p + legend_horizontal() + ggtitle("Horizontal")

Scatter plot of car weight against fuel economy titled Horizontal, with the legend on the right and its keys in a row. 2 3 4 5 10 15 20 25 30 35 mpg wt Cylinders 4 6 8 Horizontal

p + legend_vertical() + ggtitle("Vertical")

Scatter plot of car weight against fuel economy titled Vertical, with the legend on the right and its keys in a column. 2 3 4 5 10 15 20 25 30 35 mpg wt Cylinders 4 6 8 Vertical

Combining Direction and Position

Scatter plot of car weight against fuel economy, points coloured by number of cylinders, with the legend on the right and its keys in a row. 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

Summary

Function Position Auto-Direction
legend_left() Left of panel Vertical
legend_right() Right of panel Vertical
legend_top() Above panel Horizontal
legend_bottom() Below panel Horizontal
legend_inside() Inside panel Unchanged
legend_none() Hidden N/A

Learn more: