How To Change Legend Title in ggplot2?

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

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

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

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

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()

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()