如何创建一个分组的条形图,其中组内的条形按降序排列?

huangapple go评论67阅读模式
英文:

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()

Here is the resulting plot:
如何创建一个分组的条形图,其中组内的条形按降序排列?

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()

如何创建一个分组的条形图,其中组内的条形按降序排列?

huangapple
  • 本文由 发表于 2023年6月12日 01:16:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/76451634.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定