更改ggplot中背景的部分是不同的颜色 [英] Change portion of the background in ggplot be to a different color

查看:524
本文介绍了更改ggplot中背景的部分是不同的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用以下数据框 d

  day <-gl(8,1 ,24,标签= c(周一,周二,周三,周四,周五,周六,太阳,平均))
day < - factor日,level = c(星期一,星期二,星期三,星期四,星期五,星期六,太阳,平均))
月<-gl(3, 8,24,labels = c(Jan,Feb,Mar))
month <-factor(month,level = c(Jan,Feb,Mar))
雪<-gl(1,24,labels = c(Y))
雪<因子(雪,水平= c(Y,N))
计数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 =身份)+
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 = da y = y = count,group = month,color = month))+
facet_wrap(〜snow,ncol = 1,scales =free)+
scale_x_discrete(limits = levels(d $ day) )

是否可以更改条形图背后的背景颜色? b
$ b

没有部分背景颜色变化的图形

geom_rect()在直线和横条下绘制矩形。对于 ymin ymax 使用-Inf和Inf填充所有区域,但使用 xmin xmax 你应该发挥其效果。

  ggplot(data = d [d $ day ==Avg,],aes(x = day,y = count,fill = month))+ 
geom_rect(data = NULL,aes(xmin = 0.25 ,xmax = 7.25,ymin = -Inf,ymax = Inf),
fill =lightgreen)+
geom_rect(data = NULL,aes(xmin = 7.25,xmax = 8.75,ymin = -Inf ,ymax = Inf),
fill =darkgreen)+
geom_bar(position =dodge,width = 1.2,stat =identity)+
geom_text(aes(label =月,x =日,y =计数),
position = position_dodge(width = 1.2),vjust = - 。6,size = 3)+
geom_line(data = d [d $ day!= Avg,],aes(x = day,y = count,group = month,color = month))+
facet_wrap(〜snow,ncol = 1,scales =free)+
scale_x_discrete(limits = levels(d $ day))


Using the following data frame 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)

The background color behind the line graph is the same as the bar graph:

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))

Is it possible to change the background color on the portion behind the bar graph?

Graph without partial background color change

解决方案

You can use geom_rect() to draw rectangle under the lines and bars. For ymin and ymax use -Inf and Inf to fill all area but with xmin and xmax you should play to get desired effect.

ggplot(data=d[d$day=="Avg",], aes(x=day , y=count, fill=month)) + 
  geom_rect(data=NULL,aes(xmin=0.25,xmax=7.25,ymin=-Inf,ymax=Inf),
                    fill="lightgreen")+
  geom_rect(data=NULL,aes(xmin=7.25,xmax=8.75,ymin=-Inf,ymax=Inf),
                    fill="darkgreen")+
  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))

这篇关于更改ggplot中背景的部分是不同的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆