【发布时间】:2018-07-10 07:18:55
【问题描述】:
我有一些时间序列数据的工作图。在三年的时间里,我有一个值得策划的地方。看着情节,我想把每一年都画成不同的颜色。在我的 data.frame 中,我创建了一个年因子。我怎样才能用它来改变颜色?
head(wf) Date Year Gals Days GpD GpM 2016-10-21 2016 6.0 1 6.0 186.0 2016-10-22 2016 6.0 1 6.0 186.0 2016-10-23 2016 12.4 1 12.4 384.4 2016-10-24 2016 26.8 1 26.8 830.8 2016-10-25 2016 33.3 1 33.3 1032.3 2016-10-26 2016 28.3 1 28.3 877.3 nrow(wf) 626这是我的代码的基础,适用于单一颜色...
myscat<-ggplot(wf, aes(x=Date, y=Gals)) +
scale_x_date(date_labels="%b-%y", date_breaks="1 month",expand=c(0,0), position = "top") +
geom_line(aes(lty = "Gals", color = "Gals")) +
geom_hline(aes(yintercept = mean(wf$Gals), linetype = "Mean", color = "Mean"), size=1) +
geom_hline(aes(yintercept = median(wf$Gals), linetype = "Median", color = "Median"), size = 1) +
scale_linetype('Water Usage', labels = labels) +
scale_color_manual('Water Usage', values=c('Gals'='dodgerblue2', 'Mean'='red', 'Median'='orange'),
labels = labels) +
scale_y_continuous(breaks=c(0,5,10,15,20,25,30,40,50,60,70,80,90,100),
minor_breaks =seq(5,10,5), limits=c(0,100),expand=c(0,0)) +
ggtitle( "Daily Water Usage", subtitle=subtext )+
xlab("") + ylab("Gallons per Day") + #labs(title="Daily Water Usage") +
theme(plot.title = element_text(hjust=0.5), plot.subtitle=element_text(family="Comic Sans MS")) +
theme(legend.box.background = element_rect(), legend.position = c(.975, .975),
legend.justification = c("right", "top"), legend.box.just = "right",
legend.box.margin = margin(3,3,3,3)) + mytheme02 +
theme(panel.background = element_blank(),
panel.grid.major.y = element_blank(),
panel.grid.major.x = element_line(size = .5, color = "lightgrey", linetype="dashed"))+
theme(plot.margin = unit(c(0.5,0.75,0.5,0.25), "cm"))+
geom_hline(mapping=NULL, yintercept=minors, colour="lightgrey", linetype="dashed", size = .5)+
annotate("rect", xmin=wf$Date[1], xmax=wf$Date[nrow(wf)], ymin=5, ymax=20, alpha=0.1, fill="lightgrey")
print(myscat)
这会产生:
我尝试 color="Year" 并且 Year is a factor ->{2016, 2017, 2018} 没有成功。这甚至可能吗?
【问题讨论】:
标签: r ggplot2 time-series