我有一些时间序列数据的工作图。在三年的过程中,我有一个值得规划的价值。查看该图,我希望每年将其绘制为不同的颜色。在我的data.frame中,我创建了Year因子。如何使用它来改变颜色?
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)
这将产生:
我尝试为“ Year”设置颜色,Year是一个因素-> {2016,2017,2018}没有成功。这有可能吗?
答案 0 :(得分:1)
您应该在初始ggplot
调用中指定美学。试试这个:
ggplot(wf, aes(x=Date, y=Gals, group = Year, colour = Year)) +
geom_line()
编辑:
示例数据:
wf <- structure(list(Date = structure(c(17095, 17096, 17097, 17098, 17099, 17100, 17095,
17096, 17097, 17098, 17099, 17100, 17095, 17096,
17097, 17098, 17099, 17100, 17095, 17096, 17097,
17098, 17099, 17100), class = "Date"),
Year = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L),
.Label = c("2016", "2017", "2018"), class = "factor"),
Gals = c(6, 6, 12.4, 26.8, 33.3, 28.3, 6, 28.3, 33.3, 6, 26.8, 12.4,
33.3, 28.3, 26.8, 6, 12.4, 33.3, 6, 26.8, 6, 12.4, 28.3, 6),
Days = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L),
GpD = c(6, 6, 12.4, 26.8, 33.3, 28.3, 6, 6, 12.4, 26.8, 33.3, 28.3,
6, 6, 12.4, 26.8, 33.3, 28.3, 6, 6, 12.4, 26.8, 33.3, 28.3),
GpM = c(186, 186, 384.4, 830.8, 1032.3, 877.3, 186, 186, 384.4, 830.8,
1032.3, 877.3, 186, 186, 384.4, 830.8, 1032.3, 877.3, 186,
186, 384.4, 830.8, 1032.3, 877.3)),
.Names = c("Date", "Year", "Gals", "Days", "GpD", "GpM"),
class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, -24L))
剧情通话:
ggplot(wf, aes(x=Date, y=Gals, group = Year, colour = Year)) + # Specifies colour inside aesthetics
geom_line() +
scale_color_manual(values=c('dodgerblue2', 'red', 'orange')) +
geom_hline(aes(yintercept = mean(wf$Gals)), colour = "pink", size=1) # <- Colour outside aesthetics
图: