How can I change the legend labels in ggplot2? 2

How can I change the legend labels in ggplot2?

GGplot2 is a popular data visualization package in R that allows users to create high quality, customizable graphs and plots. One of the key features of ggplot2 is its ability to create informative legends that provide context for the plotted data. However, it is not uncommon for users to want to change the default labels of these legends to better suit their specific needs. This can be achieved by using the “scale_” functions in ggplot2, which allow users to modify various elements of the plot, including the legend labels. By using these functions, users can easily change the legend labels to accurately reflect the data being plotted, making their visualizations more informative and effective.

Change Legend Labels in ggplot2 (With Examples)


You can use the following syntax to change the legend labels in ggplot2:

p + scale_fill_discrete(labels=c('label1', 'label2', 'label3', ...))

The following example shows how to use this syntax in practice.

Example: Change Legend Labels in ggplot2

Suppose we create the following grouped boxplot in ggplot2:

library(ggplot2) 

#make this example reproducible
set.seed(1)

#create dataset
data <- data.frame(team=rep(c('A', 'B', 'C'), each=50),
                   program=rep(c('low', 'high'), each=25),
                   values=seq(1:150)+sample(1:100, 150, replace=TRUE))

#create grouped boxplots
p <- ggplot(data, aes(x=team, y=values, fill=program)) + 
       geom_boxplot() 

#display grouped boxplots
p

By default, the legend labels take on the following values for the fill variable:

  • high
  • low

However, suppose we’d like to change the legend labels to:

  • High Program
  • Low Program

We can use the following syntax to do so:

#create grouped boxplots with custom legend labels
p <- ggplot(data, aes(x=team, y=values, fill=program)) + 
       geom_boxplot() +
       scale_fill_discrete(labels=c('High Program', 'Low Program'))

#display grouped boxplots
p

The legend now displays the labels that we specified.

The following tutorials explain how to perform other common tasks in ggplot2:

Cite this article

stats writer (2024). How can I change the legend labels in ggplot2?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-change-the-legend-labels-in-ggplot2/

stats writer. "How can I change the legend labels in ggplot2?." PSYCHOLOGICAL SCALES, 2 May. 2024, https://scales.arabpsychology.com/stats/how-can-i-change-the-legend-labels-in-ggplot2/.

stats writer. "How can I change the legend labels in ggplot2?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-change-the-legend-labels-in-ggplot2/.

stats writer (2024) 'How can I change the legend labels in ggplot2?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-change-the-legend-labels-in-ggplot2/.

[1] stats writer, "How can I change the legend labels in ggplot2?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, May, 2024.

stats writer. How can I change the legend labels in ggplot2?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top