在ggplot中使用scale =“free”时,是否可以调整最小y值?

时间:2013-05-14 18:09:41

标签: r graph ggplot2

使用以下数据集:

day <- gl(8,1,48,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,48,labels=c("Jan","Mar","Apr"))
month<-factor(month,level=c("Jan","Mar","Apr"))

snow<-gl(2,24,48,labels=c("Y","N"))
snow<-factor(snow,levels=c("Y","N"))
count <- c(.94,.95,.96,.98,.93,.94,.99,.9557143,.82,.84,.83,.86,.91,.89,.93,.8685714,1.07,.99,.86,1.03,.81,.92,.88,.9371429,.94,.95,.96,.98,.93,.94,.99,.9557143,.82,.84,.83,.86,.91,.89,.93,.8685714,1.07,.99,.86,1.03,.81,.92,.88,.9371429)
d <- data.frame(day=day,count=count,month=month,snow=snow)

我喜欢此图表中的y比例,但不喜欢条形码:

ggplot()+
geom_line(data=d[d$day!="Avg",],aes(x=day, y=count, group=month, colour=month))+
geom_bar(data=d[d$day=="Avg",],aes(x=day, y=count, fill=month),position="dodge", group=month)+
scale_x_discrete(limits=levels(d$day))+
facet_wrap(~snow,ncol=1,scales="free")+
scale_y_continuous(labels = percent_format())

geom_bar

我喜欢这些要点,但不是规模:

ggplot(data=d[d$day=="Avg",],aes(x=day, y=count, fill=month,group=month,label=month),show_guide=F)+
facet_wrap(~snow,ncol=1,scales="free")+
geom_line(data=d[d$day!="Avg",],aes(x=day, y=count, group=month, colour=month), show_guide=F)+
scale_x_discrete(limits=levels(d$day))+
scale_y_continuous(labels = percent_format())+
geom_point(aes(colour = month),size = 4,position=position_dodge(width=1.2))

geom_point

  1. 如何在上图中结合所需的品质?
  2. 基本上,我问:在将y-min设置为零的同时,如何用变化的y-max绘制点?

    注意:我想要找到的解决方案将适用于从一个数据帧构建的大约27个图表。因此,我将对那些避免改变单个图表的解决方案进行投票。我希望有一个适用于所有facet包裹图的解决方案。

    次要问题(可能单独发布):   - 如何为每个构面包装图形添加图例?怎么样    我可以将图例的标题更改为“每周平均值”吗?怎么样    可以改变线/点的形状/颜色然后报告    在一个单一的传奇?

1 个答案:

答案 0 :(得分:3)

expand_limits(y=0),它基本上添加了一个带有不可见geom_blank的虚拟层,仅用于拉伸比例。