[英]Ordering geom_bar plots of frequency by multiple factors
我正在創建一個條形圖來顯示每個縣的調查回復數量,我想按縣和地區對回復進行分組。 我的數據如下所示:
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
我正在運行此代碼:
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)
該圖按響應頻率按縣排序。 我想先按地區排序,然后按響應計數排序。 我想要這個相同的圖表,但所有西海岸縣按響應從最多到最少的順序排列,然后東海岸縣按響應從最多到最少的順序排列。
對它進行分面不會產生我想要的效果,因為它將西海岸響應拉到單獨的網格中,並且您不能再通過相同的 y 軸比較所有縣; 沒有軸翻轉的刻面會使縣名重疊並變得難以辨認。
我還嘗試添加這樣的交互參數,但這根本沒有改變情節:
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)
編輯:這就是使用 facet wrap 時的樣子。 我不是粉絲,因為當條形不是從相同的 y 軸開始時,很難直觀地比較它們:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.