将图例与geom_ribbon相关联

时间:2016-03-24 21:02:51

标签: r ggplot2

采用以下简单示例。我无法让图例正确关联相应的颜色。我哪里出错:

ggplot()+
  geom_point(data = df, aes(time,x)) +
  geom_ribbon(data = df, aes(x=as.numeric(time), ymax=x.upper, ymin=x.lower), fill="T1", alpha=.5) +
  geom_point(data = df2, aes(time,x2)) +
  geom_ribbon(data = df2, aes(x=as.numeric(time), ymax=x.upper2, ymin=x.lower2), fill="T2", alpha=.5) +
  scale_fill_manual( "",
                     breaks = c("T1", "T2"),
                     values = c("pink", "blue"))

然后绘制

col

或者,如果我们在aes中使用ggplot()+ geom_point(data = df, aes(time,x)) + geom_ribbon(data = df, aes(x=as.numeric(time), ymax=x.upper, ymin=x.lower, col="T1"), fill="pink", alpha=.5) + geom_point(data = df2, aes(time,x2)) + geom_ribbon(data = df2, aes(x=as.numeric(time), ymax=x.upper2, ymin=x.lower2, col="T2"), fill="blue", alpha=.5) + scale_color_manual( "", breaks = c("T1", "T2"), values = c("pink", "blue")) ,则颜色无法正确关联

{{1}}

1 个答案:

答案 0 :(得分:1)

这是你想要的吗?

ggplot()+
  geom_point(data = df, aes(time,x)) +
  geom_ribbon(data = df, aes(x=as.numeric(time), ymax=x.upper, ymin=x.lower, fill="pink"), alpha=.5) +
  geom_point(data = df2, aes(time,x2)) +
  geom_ribbon(data = df2, aes(x=as.numeric(time), ymax=x.upper2, ymin=x.lower2, fill="blue"), alpha=.5) +
scale_fill_manual( "",
                     labels = c("T1", "T2"), values=c("pink"="pink","blue"="blue"))