简体   繁体   English

按多个因素对 geom_bar 频率图进行排序

[英]Ordering geom_bar plots of frequency by multiple factors

I'm creating a bar plot to display the number of survey responses from each county, and I want to group the responses by county and region.我正在创建一个条形图来显示每个县的调查回复数量,我想按县和地区对回复进行分组。 My data looks like this:我的数据如下所示:

head(df)
# A tibble: 6 x 4
  responseid region     county      industry             
       <dbl> <fct>      <fct>       <chr>                
1        137 West Coast Los Angeles Construction         
2        138 West Coast San Diego   Energy               
3        139 West Coast Orange      Professional Services
4        140 East Coast Queens      Restaurants          
5        144 West Coast San Diego   Energy               
6        145 East Coast Miami-Dade  Public Sector    

I'm running this code:我正在运行此代码:

ggplot(df, mapping = aes(x = fct_rev(fct_infreq(county)), y = stat(count))) +
  geom_bar(aes(fill = region)) + 
  coord_flip()+
  scale_y_continuous() +
  ggtitle("Responses by County") +
  ylab("Number of Responses")+
  xlab("County") + 
  labs(fill = "Region") +
  geom_text(stat='count', aes(label=..count..), vjust = .5, hjust = -1)

Which generates this plot:生成这个图: 各县响应图,按地区着色

The plot is ordered by response frequency by county.该图按响应频率按县排序。 I'd like to make it ordered first by region, then by response count.我想先按地区排序,然后按响应计数排序。 I want this same chart, but with all the West Coast counties in order from most to least responses, then the East Coast counties in order from most to least responses.我想要这个相同的图表,但所有西海岸县按响应从最多到最少的顺序排列,然后东海岸县按响应从最多到最少的顺序排列。

Faceting it doesn't give the effect I want, since it pulls the West Coast responses to a separate grid and you can't compare all the counties by the same y-axis anymore;对它进行分面不会产生我想要的效果,因为它将西海岸响应拉到单独的网格中,并且您不能再通过相同的 y 轴比较所有县; faceting without the axis flip makes the county names overlap and become illegible.没有轴翻转的刻面会使县名重叠并变得难以辨认。

I also tried to add an interaction argument like this but that didn't change the plot at all:我还尝试添加这样的交互参数,但这根本没有改变情节:

ggplot(df, mapping = aes(x = fct_rev(fct_infreq(county)), y = stat(count), group = interaction(region, county))) +
  geom_bar(aes(fill = region)) + 
  coord_flip()+
  scale_y_continuous() +
  ggtitle("Responses by County") +
  ylab("Number of Responses")+
  xlab("County") + 
  labs(fill = "Region") +
  geom_text(stat='count', aes(label=..count..), vjust = .5, hjust = -1)

Edit: This is what it looks like with facet wrap.编辑:这就是使用 facet wrap 时的样子。 I'm not a fan because it's harder to visually compare the bars when they don't start from the same y-axis:我不是粉丝,因为当条形不是从相同的 y 轴开始时,很难直观地比较它们: 在此处输入图片说明

If you remove the coord_flip, all the bars start from the same place, but then you can't read the county names at all.如果您删除 coord_flip,所有条形都从同一个位置开始,但是您根本无法读取县名。 在此处输入图片说明

I added the following two lines to the original plot to get the outcome I wanted.我在原始图中添加了以下两行以获得我想要的结果。 Thanks everyone who helped out!感谢所有帮助过的人!

facet_grid(region ~ ., scales = "free_y", space = "free") +
  theme(strip.background = element_blank(), strip.text = element_blank())

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM