How two have two legends on a chart with different groups?

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

How two have two legends on a chart with different groups?

jcrosbie
I'm trying to have two legends on a chart, with two sets of data with subgroups. Currently ggplot is are merging the to sets of data together.

I would like to have the vertical lines in there own legend and not affect the colour shading or show up in the "type" legend.

How would I go about doing this?

df <- data.frame(val1=rnorm(300, 75, 10), val2=rnorm(300, 75, 10),type=rep(c("A", "B", "C"),100))

cuts1 <- data.frame(Stats="Stat A", vals=c(43, 70, 90))
cuts2 <- data.frame(Stats="Stat B", vals=cuts2 <- c(46, 79, 86))

cuts <- rbind(cuts1,cuts2)

ggplot(df, aes(x=val1, y=val2, group=type)) +
geom_line(aes(colour=type))+
geom_point(aes(colour=type))+
geom_vline(data=cuts,
         aes(xintercept=vals,
             linetype=Stats,
             colour = Stats),
         show_guide = TRUE)
I got the idea of how to do this from: Add vline to existing plot and have it appear in ggplot2 legend?