英文:
How to create a grouped bar chart where bars within groups are organized in descending order?
问题
I want to create a grouped bar chart showing the average tenure of four types of cabinet ministers (defense, agriculture, finance, and economy) across four autocratic regime subtypes (party, personal, military, and monarch).
# 创建分组条形图
ggplot(sorted_df, aes(x = regime_subtype, y = average_tenure, fill = minister_type)) +
geom_bar(stat = "identity", position = "dodge") +
scale_fill_manual(values = minister_colors) +
labs(x = "政权子类型", y = "平均任期", fill = "部长类型") +
ggtitle("部长类型和政权子类型的平均任期") +
theme_minimal()
What I want is for the bars to appear in descending order within each group. In other words, I want the minister with the longest tenure to be the first bar, next longest the second, and so on. I have been unable to get this to work. Any advice would be appreciated.
英文:
I want to create a grouped bar chart showing the average tenure of four types of cabinet ministers (defense, agriculture, finance, and economy) across four autocratic regime subtypes (party, personal, military, and monarch). Here is the dataset:
structure(list(minister_type = c("Defense", "Finance", "Foreign Affairs",
"Agriculture", "Defense", "Finance", "Foreign Affairs", "Agriculture",
"Defense", "Foreign Affairs", "Finance", "Agriculture", "Defense",
"Foreign Affairs", "Finance", "Agriculture"), regime_subtype = c("Military",
"Military", "Military", "Military", "Monarch", "Monarch", "Monarch",
"Monarch", "Party", "Party", "Party", "Party", "Personal", "Personal",
"Personal", "Personal"), average_tenure = c(2.60493827160494,
2.48979591836735, 2.33165829145729, 2.19170984455959, 5.94202898550725,
4.20212765957447, 4.14772727272727, 3.35576923076923, 4.93292682926829,
4.60164835164835, 3.91704035874439, 3.49769585253456, 3.46043165467626,
2.99093655589124, 2.78651685393258, 2.57228915662651)), row.names = c(NA,
-16L), class = "data.frame")
Here is the code to produce the plot:
# Create the grouped bar plot
ggplot(sorted_df, aes(x = regime_subtype, y = average_tenure, fill = minister_type)) +
geom_bar(stat = "identity", position = "dodge") +
scale_fill_manual(values = minister_colors) +
labs(x = "Regime Subtype", y = "Average Tenure", fill = "Minister Type") +
ggtitle("Average Tenure by Minister Type and Regime Subtype") +
theme_minimal()
What I want is for the bars to appear in descending order within each group. In other words, I want the minister with the longest tenure to be the first bar, next longest the second, and so on. I have been unable to get this to work. Any advice would be appreciated.
答案1
得分: 1
为了实现这个任务,我们添加了一个分组美学,并使用 geom_col()
与 position_dodge2()
:
sorted_df %>%
ggplot(aes(x = regime_subtype, y = average_tenure, group = regime_subtype, fill = minister_type)) +
geom_col(position = position_dodge2())+
scale_fill_brewer(palette="Set1")+
labs(x = "政权子类型", y = "平均任期", fill = "部长类型") +
ggtitle("部长类型和政权子类型的平均任期") +
theme_minimal()
英文:
To achieve this task we add a group aesthetics and use geom_col()
with position_dodge2()
:
sorted_df %>%
ggplot(aes(x = regime_subtype, y = average_tenure, group = regime_subtype, fill = minister_type)) +
geom_col(position = position_dodge2())+
scale_fill_brewer(palette="Set1")+
labs(x = "Regime Subtype", y = "Average Tenure", fill = "Minister Type") +
ggtitle("Average Tenure by Minister Type and Regime Subtype") +
theme_minimal()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论