将ggplot中背景的部分更改为不同的颜色[复制]

问题描述 投票:5回答:1

这个问题在这里已有答案:

使用以下数据框d:

day <- gl(8,1,24,labels=c("Mon","Tues","Wed","Thurs","Fri","Sat","Sun","Avg"))
day <- factor(day, level=c("Mon","Tues","Wed","Thurs","Fri","Sat","Sun","Avg"))
month<-gl(3,8,24,labels=c("Jan","Feb","Mar"))
month<-factor(month,level=c("Jan","Feb","Mar"))
snow<-gl(1,24,labels=c("Y"))
snow<-factor(snow,levels=c("Y","N"))
count <- c(4,5,6,8,3,4,9,5.57,2,4,3,7,1,9,3,4.14,7,9,6,3,1,2,8,5.14)
d <- data.frame(day=day,count=count,month=month,snow=snow)

折线图背后的背景颜色与条形图相同:

ggplot(data=d[d$day=="Avg",], aes(x=day , y=count, fill=month)) + 
geom_bar(position = "dodge", width = 1.2, stat="identity") +
geom_text(aes(label=month, x=day, y=count), position=position_dodge(width=1.2), vjust=-.6, size=3) + 
geom_line(data=d[d$day!="Avg",], aes(x=day, y=count, group=month, colour=month)) + 
facet_wrap(~snow,ncol=1,scales="free") + 
scale_x_discrete(limits=levels(d$day))

是否可以更改条形图后面部分的背景颜色?

没有部分背景颜色变化的图表enter image description here

r graph colors background ggplot2
1个回答
10
投票

您可以使用qazxsw poi在线条和条形下绘制矩形。对于qazxsw poi和qazxsw poi使用-Inf和Inf填充所有区域但是使用geom_rect()ymin你应该发挥以获得理想的效果。

ymax

© www.soinside.com 2019 - 2024. All rights reserved.