• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

Data Viz with Python and R

Learn to Make Plots in Python and R

  • Home
  • Seaborn
  • Matplotlib
  • ggplot2
  • Altair
  • About
    • Privacy Policy
  • Visualizing Activation Functions in Neural Networks
  • Confusion Matrix Calculator
  • Visualizing Dropout Rate in Neural Network
  • Visualizing Loss Functions in Neural Networks
  • Show Search
Hide Search

How To Adjust Legend Position in ggplot2?

datavizpyr · June 15, 2020 ·

Last updated on June 13, 2020

Legends are useful to add additional variables to a data visualization. By default, when we make a plot with legend using ggplot2, it places the legend on outside the plot on the right side.

In this post, we will learn how to change or adjust the legend position in ggplot2. We will use ggplot2’s theme() function and legend.position argument to put the legend on top of the plot, at the bottom of the plot.

Let us load tidyverse and load gapminder data for making a scatter plot with legend.

library(tidyverse)
library(gapminder)
theme_set(theme_bw(16))

Let us first make a simple scatter plot colored by a third variable. In this example, we plot gdpPercap and lifeExp on scatter plot and color by continent.

gapminder %>%
  ggplot(aes(gdpPercap,lifeExp, color=continent)) +
  geom_point() +
  scale_x_log10()
ggsave("simple_scatter_plot_with_legend_ggplot2.png")
By default, ggplot2 makes a legend on the right hand side of the plot.
ggplot2 legend default position
ggplot2 legend default position

ggplot2 legend at the top

We can adjust the location of legend in ggplot2 using theme() layer with legend.position argument.

gapminder %>%
  ggplot(aes(gdpPercap,lifeExp, color=continent)) +
  geom_point() +
  scale_x_log10()+
  # place the legend at the top
  theme(legend.position = "top")
ggsave("place_legend_on_top_of_the_plot_ggplot2.png")

With legend.position=”top” option, we will get our legend on top of the plot.

ggplot2 legend on top of a plot
ggplot2 legend on top of a plot

ggplot2 legend at the bottom

Similarly, with legend.position=”bottom” option, we will get our legend at the bottom of the plot.

gapminder %>%
  ggplot(aes(gdpPercap,lifeExp, color=continent)) +
  geom_point() +
  scale_x_log10()+
  # place the legend at the bottom
  theme(legend.position = "bottom")
ggsave("place_legend_on_bottom_of_the_plot_ggplot2.png")
ggplot2 legend at the bottom of a plot
ggplot2 legend at the bottom of a plot

We can also place the legend on the left side of the plot, with theme(legend.position = “left”) option as shown below.

gapminder %>%
  ggplot(aes(gdpPercap,lifeExp, color=continent)) +
  geom_point() +
  scale_x_log10()+
  # place the legend on the left side
  theme(legend.position = "left")
ggsave("place_legend_on_left_side_of_the_plot_ggplot2.png")
ggplot2 legend on the left side
ggplot2 legend on the left side

Explore the Complete ggplot2 Guide

35+ tutorials with code: scatterplots, boxplots, themes, annotations, facets, and more—tested and beginner-friendly.

Visit the ggplot2 Hub → No fluff—just code and visuals.

Related posts:

Customizing Labels on Bars in Side by side Stacked BarplotHow To Add Labels to Grouped Barplot with Bars Side-By-Side in R? Customizing Mean mark to boxplot with ggplot2How To Show Mean Value in Boxplots with ggplot2? Visualizing Missing Data with Barplot in R ggplot2Visualizing Missing Data with Barplot in R Annotate Clusters with Ellipse with Labels ggforceHow To Annotate Clusters with Circle/Ellipse by a Variable in R

Filed Under: adjust legend position ggplot2, ggplot2, R Tagged With: ggplot2, R

Primary Sidebar

Python & R Viz Hubs

  • Seaborn Guide & Cookbook
  • ggplot2 Guide & Cookbook
  • Matplotlib Guide & Cookbook
  • Confusion Matrix Calculator
  • Visualizing Activation Functions
  • Visualizing Dropout
  • Visualizing Loss Functions

Buy Me a Coffee

Copyright © 2026 · Daily Dish Pro on Genesis Framework · WordPress · Log in

Go to mobile version