I am trying to get a plot (using ggplot) that will plot geom_point
and geom_line
where different variables are plotted in different colors (according to scale_colour_manual(value = color)
where color
is a custom array of colors) in addition to two horizontal black lines (geom_hline
).
My trouble arises when I attempt to get the legend text for the horizontal lines customized. It appears that I can have only one of the two:
- the color of the horizontal line as black but the legend text for this line is incorrect
- the legend text for this line is correct but the color of the horizontal line is determined by the aforementioned
scale_colour_manual
color.
plot <- ggplot(data, aes(x = factor(Month), y = avgLoss, colour = type, order = -as.numeric(type)))
+ geom_line() + geom_point()
meanplus2sd <- mean(data$avgLoss) + 2*sd(data$avgLoss)
plot <- plot + geom_hline(aes(yintercept = meanplus2sd), colour = "black")
produces black line that says "black" in the legend
plot <- plot + geom_hline(aes(yintercept = meanplus2sd, colour = "Mean + 2 Stdev."))
produces a line that is the next color in my defined scale_colour_manual
array, but the legend text is "Mean + 2 Stdev."
Any help in getting both custom color and legend text for a horizontal line in addition to the standard geom_point + geom_line
plotting would be excellent. Thanks.
data
so that others can copy / paste your code exactly as you have it. You can usehead(dput(data), 20)
and then paste the results of that back into the question. Alternatively, make up some sample data usingsample
,rnorm
, et al. – Chase