I have written an answer here and would like to improve it.
What I would like to do is to remove the legend for geom_path
but it is not working with show.legend = FALSE
. The color
elements of geom_path
remain in the legend (Down
and Up
). Am I doing something wrong?
Is there alternatively a way to manually tell ggplot
just to show lets say the last two elements of the legend (y2015
, y2016
)?
My Code and the Output:
library(ggplot2)
library(reshape2)
library(dplyr)
ggplot2df <- read.table(text = "question y2015 y2016
q1 90 50
q2 80 60
q3 70 90
q4 90 60
q5 30 20", header = TRUE)
df <- ggplot2df %>%
mutate(direction = ifelse(y2016 - y2015 > 0, "Up", "Down"))%>%
melt(id = c("question", "direction"))
ggplot(df, aes(x=question, y = value, color = variable, group = question )) +
geom_point(size=4) +
geom_path(aes(color = direction), arrow=arrow(), show.legend = FALSE)