So this is my code for my ggplot. How is it easier for me to change the name of the legend? I know that I can just change my gg_group variable to my_title <- c(rep("train",10), rep("validation", 10)) . But I want to just change the title to " whatever I want " without changing any variables.
library(ggplot2) y <- c(rnorm(10,1), rnorm(10,3)) x <- rep(seq(1,10,1),2) gg_group <- c(rep("train",10), rep("validation", 10)) gg_data <- data.frame(y=y, x=x, gg_group=gg_group) p <- ggplot(gg_data, aes(x=x, y=y, group=gg_group)) p + geom_line(aes(colour=gg_group))
I also tried this code:
p + geom_line(aes(colour=gg_group)) + scale_shape_discrete(name="Dataset",labels=c("Train", "Validation"))
But that does not work. * Edit, check out the excellent snwer from Jaap and JasonAizkalns.
source share