简体   繁体   中英

ggplot2: separate legend per layer

I'm trying to plot my data and got quite far with it (as I am a bloody beginner with R and ggplot). Now I'm stuck in making the legend:
I would like to have separate legend entries for line and point layers, which means something like
-- female
-- male
-- Plot A
-- Plot B
O Start
O End
O Year 1

Any suggestions how to solve that problem?

test<-data.frame(id=1:6, sex=rep(c("female", "male"),times=3),   plot=rep(c("A", "B"), times=3), start=sample(seq(as.Date('2015/01/01'), as.Date('2016/01/01'), by="day"), 6), end=sample(seq(as.Date('2016/01/01'), as.Date('2016/10/01'), by="day"),6))  
test$duration <- difftime(test$end, test$start, units="days")  
test$Year1 <- as.Date(test$start+366)  
test$Year1[test$Year1>=Sys.Date() | test$duration<365] <-NA

startTime<- as.Date("2015-01-01")  
endTime <- Sys.Date()  
start.end <-c(startTime, endTime)

ggplot(test, aes(x=start, y=id, color=sex, linetype=plot))+             
      geom_segment(aes(x=start, xend=end, y=id, yend=id), size=.75)+          
      geom_point(aes(Year1), na.rm=TRUE, shape=16, size=3)+
      geom_point(aes(start), shape=1, size=3)+
  geom_point(data=subset(test, end!= Sys.Date()), aes(end), shape=13, size=3)+
      guides(color=guide_legend(title=NULL))+                                        
      scale_x_date(date_breaks="6 months", date_minor_breaks = "1 month", date_labels="%m/%Y", name="duration", limits=start.end)+  
      scale_color_discrete(name="", breaks=c("female", "male"), labels=c("f", "m"))+
      scale_linetype_manual(name="", breaks=c("A", "B"), labels=c("Plot A", "Plot B"), values=c("dotdash","solid"))+
      scale_shape_manual(name="", guide='legend', breaks=c("Year1", "start", "end"), labels=c("Year1", "start", "end"), values=c("16", "1", "13"))

enter image description here

You have to place the start , end and Year1 in a common variable and map the shape aesthetic to this variable. This should work :

library(ggplot2)
library(tidyr)
library(dplyr)
test<-data.frame(id=1:6, sex=rep(c("female", "male"),times=3),   plot=rep(c("A", "B"), times=3), start=sample(seq(as.Date('2015/01/01'), as.Date('2016/01/01'), by="day"), 6), end=sample(seq(as.Date('2016/01/01'), as.Date('2016/10/01'), by="day"),6))  
test$duration <- difftime(test$end, test$start, units="days")  
test$Year1 <- as.Date(test$start+366)  
test$Year1[test$Year1>=Sys.Date() | test$duration<365] <-NA

startTime<- as.Date("2015-01-01")  
endTime <- Sys.Date()  
start.end <-c(startTime, endTime)

test_melt <- test %>% 
  select(id, sex, start, end, Year1) %>% 
  gather(type, value, -sex,-id)


ggplot(test)+             
  geom_segment(aes(x=start, xend=end, y=id, yend=id, color=sex, linetype=plot), size=.75)+          
  geom_point(aes(x = value, y = id, color = sex, shape = type), data = test_melt, size = 3) + 
  guides(color=guide_legend(title=NULL))+                                        
  scale_x_date(date_breaks="6 months", date_minor_breaks = "1 month", date_labels="%m/%Y", name="duration", limits=start.end)+  
  scale_color_discrete(name="", breaks=c("female", "male"), labels=c("f", "m"))+
  scale_linetype_manual(name="", breaks=c("A", "B"), labels=c("Plot A", "Plot B"), values=c("dotdash","solid"))+
  scale_shape_manual(name="", guide='legend', breaks=c("Year1", "start", "end"), labels=c("Year1", "start", "end"), values=c(16, 1, 13))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM