ggplot facet网格条形图上的不同y-限制? [英] Different y-limits on ggplot facet grid bar graph?

查看:184
本文介绍了ggplot facet网格条形图上的不同y-限制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据:

 日变量值
1 Fri avg1 446.521127
2 Mon avg1 461.676056
3 Sat avg1 393.366197
4 Sun avg1 435.985714
5 Thu avg1 445.571429
6 Tue avg1 441.549296
7 Wed avg1 462.042254
8 Fri avg2 7.442113
9 Mon avg2 7.694648
10 Sat avg2 6.556056
11 Sun avg2 7.266571
12 Thu avg2 7.426286
13 Tue avg2 7.359577
14 Wed avg2 7.700282

我的问题是我想使用 facet_grid 显示条形图每天平均每组数据,但观察结果足够相似,我发现使用 scale_y_continuous 指定y-限制是有帮助的。所以,如果我将ggplot分配给 g < - ggplot(df,aes(x = day,y = value)), code>,我可以得到我想要的一半:

  g + geom_bar(stat =identity )+ facet_grid(variable〜。,scales =free)

AND

  g + geom_bar(stat =identity)+ scale_y_continuous(限制= c(300,500),oob = rescale_none)

然而,我不知道如何使用facet grid,然后指定一个scale_y_cont来限制单独y轴的大小。是否有解决方案?使用 geom_point 可以为不同的构面创建单独的y范围 / code>,但我不知道如何使用 geom_bar 来完成它。要用 facet_wrap geom_bar 设置特定的y范围,我知道的唯一方法是创建单独的图,然后使用 gridExtra 包中的> grid.arrange 并排放置它们。 (使用不下降为零的垂直比例将夸大点/线条之间的差异,这可能会产生误导,但您必须决定是否适合您的特定情况。)

首先,这里是 geom_point 版本:想法是创建一个虚拟数据框,其中包含ylim所需的较低和较高值,然后使用 geom_blank 来绘制它们。 geom_blank 不会绘制任何东西,但添加此geom将确保轴范围是您希望的每个方面的范围。

  ddummy = data.frame(day = NA,variable = rep(c(avg1,avg2),each = 2),
value = c(0.5 * max(df $ value [df $ variable ==avg1]),
1.1 * max(df $ value [df $ variable ==avg1 ]),
0.5 * max(df $ value [df $ variable ==avg2]),
1.1 * max(df $ value [df $ variable ==avg2])) )

g < - ggplot(df,aes(x = day,y = value))

g + geom_point()+
geom_blank(data = dummy, aes(day,value))+
facet_grid(variable〜。,scales =free)



在这里是分开的地块,与 grid.arrange

  avg1 = ggplot(df [df $ variable ==avg1,],aes(x = day,y = value))+ 
geom_bar(stat =identity)+
facet_wrap(〜variable)+
coord_cartesian(ylim = c(300,500))

avg2 = ggplot(df [df $ variable ==av g2,],aes(x = day,y = value))+
geom_bar(stat =identity)+
facet_wrap(〜variable)+
coord_cartesian(ylim = c 3.5,8))

gridExtra :: grid.arrange(avg1,avg2,ncol = 2)



  library(dplyr)

ggplot(df%>%group_by(variable)%>%
mutate(ymin = 0.5 * max(value)))+
geom_segment(aes(x = day,xend = day,y = ymin,yend = value),
size = 5,color = hcl(195,100,65))+
facet_grid(variable〜。,scale =free)


My data:

    day variable      value
1  Fri     avg1 446.521127
2  Mon     avg1 461.676056
3  Sat     avg1 393.366197
4  Sun     avg1 435.985714
5  Thu     avg1 445.571429
6  Tue     avg1 441.549296
7  Wed     avg1 462.042254
8  Fri     avg2   7.442113
9  Mon     avg2   7.694648
10 Sat     avg2   6.556056
11 Sun     avg2   7.266571
12 Thu     avg2   7.426286
13 Tue     avg2   7.359577
14 Wed     avg2   7.700282

My issue is I want to create a bar graph using facet_grid displaying each set of avg data by day, but the observations are similar enough that I've found it helpful to specify the y-limits using scale_y_continuous.

So, if I assign my ggplot to g <- ggplot(df, aes(x=day, y=value)), I can get half of what I want by each of:

g + geom_bar(stat="identity") + facet_grid(variable~., scales="free")

AND

g + geom_bar(stat="identity") + scale_y_continuous(limits=c(300,500), oob=rescale_none)

However, I don't know how to use facet grid and then specify a scale_y_cont that will limit the size of separate y-axes. Is there a solution?

解决方案

You can create separate y-ranges for different facets when using geom_point, but I don't know of a way to do it with geom_bar. To set specific y-ranges with facet_wrap and geom_bar, the only way I know of is to create separate plots and then put them side by side using grid.arrange from the gridExtra package. (Using a vertical scale that doesn't go down to zero will exaggerate differences between points/bars, which can be misleading, but you'll have to decide if it makes sense for your particular case.)

First, here's the geom_point version: The idea is to create a "dummy" data frame with lower and upper values you want for ylim and then "plot" them using geom_blank. geom_blank doesn't plot anything, but adding this geom will ensure that the axis range is what you want it to be for each facet.

ddummy = data.frame(day=NA, variable=rep(c("avg1", "avg2"), each=2), 
               value=c(0.5*max(df$value[df$variable=="avg1"]), 
                       1.1*max(df$value[df$variable=="avg1"]),
                       0.5*max(df$value[df$variable=="avg2"]), 
                       1.1*max(df$value[df$variable=="avg2"])))

g <- ggplot(df, aes(x=day, y=value))

g + geom_point() + 
  geom_blank(data=dummy, aes(day, value)) +
  facet_grid(variable ~ ., scales="free")

And here are separate plots, put together with grid.arrange:

avg1 = ggplot(df[df$variable=="avg1",], aes(x=day, y=value)) +
  geom_bar(stat="identity") +
  facet_wrap(~variable) +
  coord_cartesian(ylim=c(300,500))

avg2 = ggplot(df[df$variable=="avg2",], aes(x=day, y=value)) +
  geom_bar(stat="identity") +
  facet_wrap(~variable) +
  coord_cartesian(ylim=c(3.5,8))

gridExtra::grid.arrange(avg1, avg2, ncol=2)

To use geom_segment (per your comment) you could do this:

library(dplyr)

ggplot(df %>% group_by(variable) %>%
         mutate(ymin=0.5*max(value))) +
  geom_segment(aes(x=day, xend=day, y=ymin, yend=value), 
               size=5, colour=hcl(195,100,65)) + 
  facet_grid(variable ~ ., scales="free")

这篇关于ggplot facet网格条形图上的不同y-限制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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