Rename Legend Title of ggplot2 Plot in R (Example)

This tutorial shows how to change the legend title of a ggplot2 plot in the R programming language.

Example Data

my_data <- data.frame(x = 1:10,                                  # Example data
                      y = 1:10,
                      Default = c(rep("A", 5), rep("B", 5)))

Load ggplot2 Package & Create Default Plot

install.packages("ggplot2")                                      # Install ggplot2 package
library("ggplot2")                                               # Load ggplot2 package
my_ggplot <- ggplot(my_data, aes(x, y, group = Default)) +       # ggplot2 with default legend
  geom_point(aes(x, y, color = Default))
my_ggplot                                                        # Print plot

default legend ggplot2

Change Legend Name of ggplot2 Plot

my_ggplot + scale_color_discrete(name = "Manual Title")          # Manual legend title

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed