• 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
  • Python Viz
  • Seaborn
  • Altair
  • R Viz
  • ggplot2
  • About
    • Privacy Policy
  • Show Search
Hide Search

How To Adjust Legend Position in ggplot2?

datavizpyr · June 15, 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

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

Tags

Altair barplot Boxplot boxplot python boxplot with jiitered text labels Bubble Plot Color Palette Countplot Density Plot Facet Plot gganimate ggplot2 ggplot2 Boxplot ggplot2 error ggplot boxplot ggridges ggtext element_markdown() Grouped Barplot R heatmap heatmaps Histogram Histograms Horizontal boxplot Python lollipop plot Maps Matplotlib Pandas patchwork pheatmap Pyhon Python R RColorBrewer reorder boxplot ggplot Ridgeline plot Scatter Plot Scatter Plot Altair Seaborn Seaborn Boxplot Stock Price Over Time Stripplot UpSetR Violinplot Violin Plot World Map ggplot2

Buy Me a Coffee

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

Go to mobile version