I am trying to plot 2 solid lines in 2 different colours for each group, but also add dashed lines of the same colour around those lines, then add a legend. For some reason I am having trouble using "dashed" or "dotted", it seems as I am plotting over the dashed lines twice. I am also not getting the legend right, I get the error Adding another scale for 'colour', which will replace the existing scale
.
Can you please help me figure out what I am doing wrong?
Here is an example dataset and what I have tried:
x <- c(10, 20, 50, 10, 20, 50)
mean = c(52.4, 98.2, 97.9, 74.1, 98.1, 97.6)
group = c(1, 1, 1, 2,2,2)
upper = c(13.64, 89, 86.4, 13.64, 89, 86.4)
lower = c(95.4, 99.8, 99.7, 95.4, 99.8, 99.7)
data <- data.frame(x=x,y=mean, group, upper, lower)
ggplot(data, aes(x = x, y= mean, group = as.factor(data$group), colour=as.factor(data$group))) + geom_line() + geom_point() + geom_line(data=data,aes(x=x, y=lower, group = as.factor(data$group), colour=as.factor(data$group), linetype="dotted")) + geom_line(data=data,aes(x=x, y=upper, group = as.factor(data$group), colour=as.factor(data$group), linetype="dotted")) + scale_color_manual(values=c("red", "blue")) + scale_colour_discrete(name="Groups")
I have also tried with geom_ribbon
, again with no luck for the grouping part…
ggplot(data, aes(x = x, y= mean, group = group)) + geom_line() +
geom_ribbon(aes(ymin = lower, ymax = upper)) +
geom_line(aes(y = mean), colour = "Mean")) +
scale_colour_manual(name = "", values = c("Group1", "Group2"))
See Question&Answers more detail:
os