[R] How two have two legends on a chart with different groups?

jcrosbie james at crosb.ie
Sat Apr 12 20:03:03 CEST 2014


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?



--
View this message in context: http://r.789695.n4.nabble.com/How-two-have-two-legends-on-a-chart-with-different-groups-tp4688666.html
Sent from the R help mailing list archive at Nabble.com.




More information about the R-help mailing list