我正在尝试绘制我的数据并得到了很远的结果(因为我是 R 和 ggplot 的初学者)。现在我陷入了制作传奇的困境:
我想为线图层和点图层设置单独的图例条目,这意味着类似
- 女性
- 男性
-- 情节A
-- 情节B
O 开始
O端
第一年

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

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

最佳答案

您必须将 startendYear1 放在一个公共(public)变量中,并将 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))

关于r - ggplot2:每层单独的图例,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39531104/

10-12 16:33