• Skip to secondary menu
  • Skip to main content
  • Skip to primary sidebar

Python and R Tips

Learn Data Science with Python and R

  • Home
  • Python
  • Pandas
    • Pandas 101
  • tidyverse
    • tidyverse 101
  • R
  • Linux
  • Conferences
  • Python Books
  • About
    • Privacy Policy
You are here: Home / R / ggplot2 / change legend title ggplot2 / How To Change Legend Title in ggplot2?

How To Change Legend Title in ggplot2?

October 17, 2019 by cmdlinetips

In this post, we will see multiple examples of how to change the legend title in ggplot2. When you make a plot with ggplot2 and color/highlight data points by a variable in the input dataframe, ggplot2 uses the name of the variable present in the dataframe. However, sometimes you might want to change the legend title. In the simplest case, you might want to change the title of a single legend. Sometimes, you might want to change the titles of multiple legend titles.

We will see three examples of changing legend titles in ggplot2. First we will change the title of a single legend with scale_colour_discrete(). Then we will see an example of changing titles of two legends using labs() and guides().

Let us load the packages we need to make the plot.

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

Here is how our data looks like.

head(gapminder)
## # A tibble: 3 x 6
##   country     continent  year lifeExp      pop gdpPercap
##   <fct>       <fct>     <int>   <dbl>    <int>     <dbl>
## 1 Afghanistan Asia       1952    28.8  8425333      779.
## 2 Afghanistan Asia       1957    30.3  9240934      821.
## 3 Afghanistan Asia       1962    32.0 10267083      853.

Let us make a scatter plot between gdpPercap and lifeExp. Let us also highlight the data points by the column variable continent.

gapminder %>%
  ggplot(aes(x=lifeExp,y=gdpPercap, col=continent))+ 
  geom_point(alpha=0.5)+
  scale_y_log10() 

Note that the legend title by default is “continent”, which is the variable name in the gapminder dataframe.

ggplot2 single legend title default
ggplot2 single legend title default

How to change legend title with scale_colour_discrete()

Sometimes you might want to change the legend title to something more meaningful. Let us say we want to change the title to “Continents”. We know that we used the continent variable to color the data points. We can use the function scale_colour_discrete() with the argument being the new legend title we want.

gapminder %>%
  ggplot(aes(x=lifeExp,y=gdpPercap, col=continent))+ 
  geom_point(alpha=0.5)+
  scale_y_log10() +
  scale_colour_discrete("Continents")
ggplot2 change single legend title
ggplot2 change single legend title

That was easy. Sometimes we highlight the data by more than one variable. Let us use color and size to highlight two variables in gapminder data frame. We use size to display the population size, the pop variable in gapminder dataframe. And we use color to display different colors for different continent as before.

gapminder %>%
  ggplot(aes(x=lifeExp,y=gdpPercap, size=pop, col=continent))+ 
  geom_point(alpha=0.5)+
  scale_y_log10()

By default, ggplot2 uses the variable names present in the dataframe as legend titles.

ggplot2 default legend title
ggplot2 default legend title

We can change multiple legend titles in at least two ways.

Changing legend titles with labs()

We will first use labs() in ggplot to specify the titles for the legend. Labs() allows us to change legend title easily for more than one legend titles.

Since we used size and color to highlight the data points, we use size and col argument in side labs to specify the new legend titles we want.

gapminder %>%
  ggplot(aes(x=lifeExp,y=gdpPercap, size=pop, col=continent))+ 
  geom_point(alpha=0.5)+
  scale_y_log10()+
  labs(size="Population",col="Continent")

In this example, we change the title for size legend to “Population” and the title for color legend to “Continent”.

ggplot2 change legend title with labs()
ggplot2 change legend title with labs()

Changing legend titles with guides

Another way to change legend titles is to use guides() function in ggplot2. Here, guides() function can take two legend titles as arguments.

gapminder %>%
  ggplot(aes(x=lifeExp,y=gdpPercap, size=pop, col=continent))+ 
  geom_point(alpha=0.5)+
  scale_y_log10()+
  guides(col=guide_legend("My Continents"),
         size=guide_legend("Population"))

We use guide_legend() to specify the new title we want one for size and other for color.

ggplot2 change legend title with guides()
ggplot2 change legend title with guides()

Share this:

  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on X (Opens in new window) X

Related posts:

How To Highlight Select Data Points with ggplot2 in R? Scatter plot tips: Color & Shape by variable9 Tips to Make Better Scatter Plots with ggplot2 in R Tips to Customize Text in ggplot2 plot10 Tips to Customize Text Color, Font, Size in ggplot2 with element_text() Customizing Grouped BoxplotHow To Make Grouped Boxplots with ggplot2?

Filed Under: change legend title ggplot2, R Tips, tidyverse 101 Tagged With: ggplot change legend title, ggplot2, ggplot2 legend title

Primary Sidebar

Subscribe to Python and R Tips and Learn Data Science

Learn Pandas in Python and Tidyverse in R

Tags

Altair Basic NumPy Book Review Data Science Data Science Books Data Science Resources Data Science Roundup Data Visualization Dimensionality Reduction Dropbox Dropbox Free Space Dropbox Tips Emacs Emacs Tips ggplot2 Linux Commands Linux Tips Mac Os X Tips Maximum Likelihood Estimation in R MLE in R NumPy Pandas Pandas 101 Pandas Dataframe Pandas Data Frame pandas groupby() Pandas select columns Pandas select_dtypes Python Python 3 Python Boxplot Python Tips R rstats R Tips Seaborn Seaborn Boxplot Seaborn Catplot Shell Scripting Sparse Matrix in Python tidy evaluation tidyverse tidyverse 101 Vim Vim Tips

RSS RSS

  • How to convert row names to a column in Pandas
  • How to resize an image with PyTorch
  • Fashion-MNIST data from PyTorch
  • Pandas case_when() with multiple examples
  • An Introduction to Statistical Learning: with Applications in Python Is Here
  • 10 Tips to customize ggplot2 title text
  • 8 Plot types with Matplotlib in Python
  • PCA on S&P 500 Stock Return Data
  • Linear Regression with Matrix Decomposition Methods
  • Numpy’s random choice() function

Copyright © 2025 · Lifestyle Pro on Genesis Framework · WordPress · Log in

Go to mobile version