如何将ggplot2中geom_bar的分组水平条形图按降序排列?
问题描述
我正在处理大学评分数据,已绘制出展示各院校对应评分的分组水平条形图,希望将条形按降序排列,请问该如何操作?
现有代码
ratings <- read.csv("senior_expert_ratings_tall.csv", stringsAsFactors = T)
ggplot(data=ratings, aes(x=Assessment,y=Rating,fill=Agency)) + theme_minimal() + geom_bar(position="dodge",stat="identity") + labs(title="Expert Ratings of Key Universities", subtitle="The INORMS Research Evaluation Group, in partnership with rankings\nexpert Dr Richard Holmes, produced a rating of six key global university\nrankings in 2021. Using 20 community-sourced criteria across four main\nthemes (good governance, rigour, transparency, and measure what matters)\nexperts were invited to assess the ranking agencies both qualitatively and\nquantitatively. The assessment found that all rankers fell short of the community\nexpectations, with the higher-profile agencies faring worse than others.", caption = "Figure 2: Global university ranking agencies assessed by international experts\nagainst criteria including transparency and rigour.") + theme(panel.grid.minor.x = element_blank(), panel.grid.minor.y = element_blank()) + theme(plot.caption = element_text(hjust=0)) + theme(plot.subtitle = element_text(size=8)) + theme(axis.title.x = element_blank()) + theme(axis.title.y = element_blank())
解决方法
要实现条形按降序排列,核心是将Assessment列转换为按评分排序的因子,以下是两种简洁的实现方式:
方式一:提前计算排序逻辑(直观可控)
- 先统计每个评估维度的总评分(或平均评分)作为排序依据
- 将
Assessment转换为按该依据降序排列的因子 - 直接用处理后的数据绘图
修改后的完整代码:
ratings <- read.csv("senior_expert_ratings_tall.csv", stringsAsFactors = T) # 计算每个评估维度的总评分,按降序排序 rating_sums <- aggregate(Rating ~ Assessment, data = ratings, sum) rating_sums_sorted <- rating_sums[order(-rating_sums$Rating), ] # 将Assessment转换为排序后的因子 ratings$Assessment <- factor(ratings$Assessment, levels = rating_sums_sorted$Assessment) ggplot(data=ratings, aes(x=Assessment,y=Rating,fill=Agency)) + theme_minimal() + geom_bar(position="dodge",stat="identity") + labs(title="全球顶尖大学排名机构专家评分", subtitle="INORMS研究评估小组与排名专家Richard Holmes博士合作,于2021年对全球六大顶尖大学排名进行了评分。专家们基于四大主题(良好治理、严谨性、透明度、衡量关键指标)下的20项社区提出的标准,对排名机构进行了定性和定量评估。评估发现所有排名机构均未达到社区预期,知名度较高的机构表现反而更差。", caption = "图2:国际专家针对透明度、严谨性等标准对全球大学排名机构的评估") + theme(panel.grid.minor.x = element_blank(), panel.grid.minor.y = element_blank()) + theme(plot.caption = element_text(hjust=0)) + theme(plot.subtitle = element_text(size=8)) + theme(axis.title.x = element_blank()) + theme(axis.title.y = element_blank())
如果想按平均评分排序,只需把aggregate中的sum替换为mean即可:
rating_means <- aggregate(Rating ~ Assessment, data = ratings, mean) rating_means_sorted <- rating_means[order(-rating_means$Rating), ] ratings$Assessment <- factor(ratings$Assessment, levels = rating_means_sorted$Assessment)
方式二:用reorder函数简化代码
无需提前处理数据,直接在ggplot的aes中使用reorder函数指定排序规则:
ratings <- read.csv("senior_expert_ratings_tall.csv", stringsAsFactors = T) ggplot(data=ratings, aes(x=reorder(Assessment, -Rating, FUN=sum), y=Rating, fill=Agency)) + theme_minimal() + geom_bar(position="dodge",stat="identity") + labs(title="全球顶尖大学排名机构专家评分", subtitle="INORMS研究评估小组与排名专家Richard Holmes博士合作,于2021年对全球六大顶尖大学排名进行了评分。专家们基于四大主题(良好治理、严谨性、透明度、衡量关键指标)下的20项社区提出的标准,对排名机构进行了定性和定量评估。评估发现所有排名机构均未达到社区预期,知名度较高的机构表现反而更差。", caption = "图2:国际专家针对透明度、严谨性等标准对全球大学排名机构的评估") + theme(panel.grid.minor.x = element_blank(), panel.grid.minor.y = element_blank()) + theme(plot.caption = element_text(hjust=0)) + theme(plot.subtitle = element_text(size=8)) + theme(axis.title.x = element_blank()) + theme(axis.title.y = element_blank())
其中reorder(Assessment, -Rating, FUN=sum)表示按Rating的总和降序排列Assessment,替换sum为mean即可按平均评分排序。
内容的提问来源于stack exchange,提问作者Newt8
相关产品推荐
相关产品推荐

