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))) |
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 |
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 |
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
Change Legend Name of ggplot2 Plot
my_ggplot + scale_color_discrete(name = "Manual Title") # Manual legend title |
my_ggplot + scale_color_discrete(name = "Manual Title") # Manual legend title