【发布时间】:2013-08-24 14:29:23
【问题描述】:
我正在尝试创建一个图例,该图例将采用自定义标签和颜色,alphas 对应于注释图中突出显示的区域,而不是使用代码在图表中绘制的数据系列:
library(ggplot2)
data(economics)
p1 <- ggplot(data=economics, mapping=aes(x=date, y=unemploy)) +
geom_line(size=1) +
annotate("rect", xmin=as.Date('1970-01-01'), xmax=as.Date('1980-01-01'), ymin=-Inf, ymax=Inf, alpha=0.2, fill="red") +
annotate("rect", xmin=as.Date('1990-01-01'), xmax=as.Date('2000-01-01'), ymin=-Inf, ymax=Inf, alpha=0.2, fill="green") +
p1
我想在其中添加一个带有标签的图例,标签为“1970s”、“1990s”,相应的颜色为红色和绿色,alpha 为 0.2,对应于注释元素。有没有办法做到这一点?
【问题讨论】:
标签: r plot ggplot2 legend annotate