I'm trying to have a ggplot with two vertical lines on it, with a separate custom legend to explain what the lines represent. This is my code (using iris):
irate <- as.data.frame(iris)
irate$Species <- as.character(irate$Species)
irritating <- ggplot(irate) +
geom_line(aes(y = Sepal.Length, x = Sepal.Width), color = "blue") +
geom_point(aes(y = Sepal.Length, x = Sepal.Width, color = Species), size = 5) +
theme(legend.position = "right", axis.text.y = element_blank(), axis.title.y = element_blank(), axis.ticks.y = element_blank(), panel.grid.major.y = element_blank())+
labs(title = "The chart", x = "Sepal Width") +
geom_vline(color = "black", linetype = "dashed", aes(xintercept = 3))+
geom_vline(color = "purple", linetype = "dashed", aes(xintercept = 4))
irritating
I've tried using things like scale_color_manual (etc), but for some reason when doing so it will interfere with the main legend and not produce a separate one.
Using answers to questions like: Add legend to geom_vline
I add: +scale_color_manual(name = "still problematic", values = c("black", "purple", "red"))
the addition of "red" in the vector the only way to get it to produce a chart (otherwise there's a: "Insufficient values in manual scale. 3 needed but only 2 provided." error).