# Change Legend Title in ggplot2 (2 Example Codes) | Modify Text of ggplot Legends

This article explains how to **change the text of a ggplot2 legend** in R. The tutorial is structured as follows:

- Example Data
- Change Legend Text with
*scale_color_discrete*(Example 1) - Change Legend Text with
*labs*(Example 2) - Further Resources for the Modification of ggplot2 plots

Let’s move on to the examples!

## Create Example Data

First, we have to create some data for the examples of this tutorial:

set.seed(2468) # Set seed for reproducibility data <- data.frame(x = c(runif(200, 0, 2), runif(100, 1, 3)), # Create x variable y = c(runif(200, 1, 3), runif(100, 2, 4)), # Create y variable group = c(rep("g1", 200), rep("g2", 100))) # Create group variable head(data) # Print first 6 rows of data # x y group # 1 0.9251444 2.510920 g1 # 2 1.1482466 1.688871 g1 # 3 0.7495806 1.476128 g1 # 4 1.8013962 1.462917 g1 # 5 0.4498683 2.673079 g1 # 6 0.6840810 2.449923 g1 |

set.seed(2468) # Set seed for reproducibility data <- data.frame(x = c(runif(200, 0, 2), runif(100, 1, 3)), # Create x variable y = c(runif(200, 1, 3), runif(100, 2, 4)), # Create y variable group = c(rep("g1", 200), rep("g2", 100))) # Create group variable head(data) # Print first 6 rows of data # x y group # 1 0.9251444 2.510920 g1 # 2 1.1482466 1.688871 g1 # 3 0.7495806 1.476128 g1 # 4 1.8013962 1.462917 g1 # 5 0.4498683 2.673079 g1 # 6 0.6840810 2.449923 g1

Our example data frame contains an x variable, a y variable, as well as a group variable.

Now let’s see how the legend title looks by default. First, we need to install and load the ggplot2 package in R…

install.packages("ggplot2") # Install ggplot2 package library("ggplot2") # Load ggplot2 package |

install.packages("ggplot2") # Install ggplot2 package library("ggplot2") # Load ggplot2 package

…and then we can print a ggplot2 scatterplot as follows:

my_ggplot <- ggplot(data, aes(x, y, group = group)) + # ggplot2 with 2 legends geom_point(aes(x, y, color = group)) my_ggplot # Print plot with default legend |

my_ggplot <- ggplot(data, aes(x, y, group = group)) + # ggplot2 with 2 legends geom_point(aes(x, y, color = group)) my_ggplot # Print plot with default legend

**Figure 1: ggplot2 with Default Specification.**

As you can see based on Figure 1, the default specification of the ggplot2 package shows the column name of our group variable as legend title.

In the following examples, I’ll show you two alternatives how to change the text of this legend title in R.

Let’s dive in!

## Example 1: Change Text of ggplot Legend Title with *scale_color_discrete*

This example shows how to modify a ggplot legend title with *scale_color_discrete*:

my_ggplot + scale_color_discrete(name = "My Legend Title No. 1") # Manual legend title |

my_ggplot + scale_color_discrete(name = "My Legend Title No. 1") # Manual legend title

**Figure 2: ggplot2 with Legend Title Modified by scale_color_discrete.**

The previous R syntax changed the title to “My Legend Title No. 1”.

**Note:** We used the function scale**_color_**discrete, because our legend is created based on the color specification of our graphic. If we would have created our legend based on other aesthetics, we would have to use the corresponding scale_…_discrete function (e.g. scale_fill_discrete for fill). This is by the way true for all types of plots (e.g. histogram, barchart, dotplot, pairs plot and so on).

There’s another alternative, which is also quite popular for changing legend titles of ggplots. So keep on reading…

## Example 2: Change Text of ggplot Legend Title with *labs*

The second alternative that I want to show you is based on the labs function:

my_ggplot + labs(color = "My Legend Title No. 2") # Manual legend title |

my_ggplot + labs(color = "My Legend Title No. 2") # Manual legend title

**Figure 3: ggplot2 with Legend Title Modified by labs.**

As you can see, the second example resulted in the same legend text as the code of example 1 (besides calling the legend title *No. 2* at the end).

**Note:** We used the specification *color = “…”*, because our legend is created based on the color specification of our plot (see explanation above).

## Further Resources for ggplot2

The ggplot2 environment provides many different possibilities for the formatting of our plots. If you want to learn more about the ggplot2 package, I can recommend the following YouTube video of Roger Peng:

Besides the video, you might also want to have a look at the other R programming tutorials of this website:

- Remove ggplot2 Legend Entirely
- Change Position of ggplot Title
- R Graphics Gallery
- List of R Commands (+ Examples)
- The R Programming Language

This tutorial showed how to format the text of legends in ggplot2. If you have any feedback or comments, don’t hesitate to let me know in the comments!

### Subscribe to my free statistics newsletter:

### R Tutorials

abs Function in R

all & any R Functions

Set Aspect Ratio of Plot

attach & detach R Functions

attr, attributes & structure in R

cbind R Command

Change ggplot2 Legend Title

Character to Numeric in R

Check if Object is Defined

col & row sums, means & medians

Complete Cases in R

Concatenate Vector of Strings

Convert Date to Weekday

cumsum R Function

Data Frame Column to Numeric

diff Command in R

difftime R Function

dim Function in R

dir R Function

Disable Scientific Notation

Draw Segments in R

droplevels R Example

Evaluate an Expression

Extract Characters from String

Factor to Numeric in R

Format Decimal Places

get, get0 & mget in R

is.na R Function

is.null Function in R

jitter R Function

Join Data with dplyr Package

length Function in R

lowess R Smoothing Function

max and min Functions in R

NA Omit in R

nchar R Function

ncol Function in R

nrow Function in R

outer Function in R

pairs & ggpairs Plot

parse, deparse & R expression

paste & paste0 Functions in R

pmax and pmin R Functions

polygon Plots in R

pretty R Function

R Find Missing Values

R Functions List (+ Examples)

R NA – Values

R Replace NA with 0

rbind & rbind.fill in R

Read Excel Files in R

readLines, n.readLines & readline

Remove Element from List

Remove Legend in ggplot2

Rename Column Name in R

Replace Last Comma of String

rev R Command

Round Numeric Data in R

Save & Load RData Workspace

scan R Function

setdiff R Function

setNames vs. setnames in R

sink Command in R

Sort, Order & Rank Data in R

sprintf Function in R

Square Root in R

str_c Function of stringr Package

str_sub Function of stringr Package

strptime & strftime Functions

substr & substring R Commands

sweep R Function

Transform Data Frames

union Function in R

unlist in R

weekdays, months, quarters & julian in R

with & within R Functions

Write Excel File in R