ggplot2:每层单独的图例

时间:2016-09-16 12:06:44

标签: r ggplot2

我试图绘制我的数据并且相当远(因为我是R和ggplot的血腥初学者)。现在我被困在制作传奇:
我想为线和点图层分别设置图例,这意味着像 - 女
- 男
- 情节A
- 情节B
 O开始
 O结束
 O年1岁

有任何建议如何解决这个问题?

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

1 个答案:

答案 0 :(得分:2)

您必须将startendYear1放在一个公共变量中,并将shape美学映射到此变量。这应该有效:

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))