在ggplot中的条形图中重新排序x轴

Reorder x axis in bar chart in ggplot

我想绘制条形图并按值对 x 轴进行排序。我知道我以前设法做到了,但我可能已经在某处删除了该代码。我已经坚持了一段时间,所以我现在问你。

由于我的数据框包含超过 24,000 行,这里是示例数据。我希望它足以重新创建它。

structure(list(country = c("Palestine", "Iraq", "Algeria", "Libya", 
"Lebanon", "Jordan", "Egypt", "Palestine", "Libya", "Egypt", 
"Libya", "Jordan", "Yemen", "Jordan", "Tunesia", "Iraq", "Lebanon", 
"Lebanon", "Morocco", "Algeria", "Jordan", "Egypt", "Kuwait", 
"Morocco", "Tunesia", "Palestine", "Yemen", "Lebanon", "Sudan", 
"Lebanon", "Libya", "Palestine", "Yemen", "Tunesia", "Sudan", 
"Yemen", "Morocco", "Jordan", "Palestine", "Palestine", "Libya", 
"Palestine", "Libya", "Jordan", "Jordan", "Lebanon", "Iraq", 
"Algeria", "Yemen", "Tunesia", "Lebanon", "Libya", "Yemen", "Egypt", 
"Yemen", "Libya", "Palestine", "Egypt", "Tunesia", "Sudan", "Tunesia", 
"Egypt", "Lebanon", "Iraq", "Kuwait", "Libya", "Tunesia", "Algeria", 
"Morocco", "Egypt", "Tunesia", "Morocco", "Palestine", "Kuwait", 
"Morocco", "Kuwait", "Morocco", "Palestine", "Morocco", "Lebanon", 
"Iraq", "Egypt", "Morocco", "Algeria", "Jordan", "Sudan", "Sudan", 
"Algeria", "Sudan", "Egypt", "Palestine", "Jordan", "Sudan", 
"Iraq", "Egypt", "Tunesia", "Sudan", "Yemen", "Lebanon", "Iraq"
), female_head_gov = structure(c(3L, 3L, 4L, 3L, 2L, 2L, 4L, 
2L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 1L, 3L, 2L, 3L, 3L, 1L, 1L, 3L, 
3L, 4L, 2L, 4L, 2L, 1L, 2L, 3L, 2L, 2L, 3L, 3L, 3L, 5L, 2L, 2L, 
4L, 2L, 4L, 2L, 4L, 3L, 2L, 1L, 4L, 3L, 2L, 2L, 2L, 2L, 1L, 3L, 
2L, 2L, 2L, 2L, 3L, 5L, 1L, 1L, 1L, 2L, 2L, 4L, 2L, 2L, 3L, 2L, 
4L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 2L, 2L, 2L, 3L, 2L, 4L, 1L, 2L, 
3L, 2L, 2L, 1L, 2L, 1L, 1L, 1L, 2L, 3L, 3L, 1L, 1L), .Label = c("I strongly agree", 
"I agree", "I disagree", "I strongly disagree", "Don't know"), class = "factor")), class = "data.frame", row.names = c(NA, 
-100L))

我使用 forcats::fct_reorder 尝试了以下代码,但它没有给我想要的结果:

dataset %>%
  group_by(country) %>%
  filter(!is.na(female_head_gov), female_head_gov != "Don't know") %>%
  mutate(female_head_gov = fct_collapse(female_head_gov,
                                `Agree/strongly agree` = c("I strongly agree", "I agree"),
                                `Disagree/strongly disagree` = c("I disagree", "I strongly disagree"))
  ) %>%
  count(female_head_gov) %>%
  mutate(prop = n / sum(n)) %>%
  ggplot(aes(fct_reorder(country, prop), prop, fill = female_head_gov)) +
  geom_col(position = "fill")

我也没有计算比例就试过了,但是我没有变量来订购国家/地区变量。

dataset %>%
  group_by(country) %>%
  filter(!is.na(female_head_gov), female_head_gov != "Don't know") %>%
  mutate(female_head_gov = fct_collapse(female_head_gov,
                                        `Agree/strongly agree` = c("I strongly agree", "I agree"),
                                        `Disagree/strongly disagree` = c("I disagree", "I strongly disagree"))
  ) %>%
  ggplot(aes(country, fill = female_head_gov)) +
  geom_bar(position = "fill")

知道我做错了什么吗?如果您有任何如何更有效地创建这样的条形图的提示,请告诉我。 :)

我猜这是因为每个国家/地区都有两个 prop 值。如果您指定 prop 的第一个值,它将按预期工作:

plot_df <- dataset %>%
  group_by(country) %>%
  filter(!is.na(female_head_gov), female_head_gov != "Don't know") %>%
  mutate(female_head_gov = fct_collapse(female_head_gov,
                                        `Agree/strongly agree` = c("I strongly agree", 
                                                             "I agree"),
                                        `Disagree/strongly disagree` = c("I disagree", 
                                                                     "I strongly disagree"))
  ) %>%
  count(female_head_gov) %>%
  mutate(prop = n / sum(n))

  plot_df$country <- fct_reorder(plot_df$country, plot_df$prop, function(x) -x[1])
  ggplot(plot_df, aes(country, prop, fill = female_head_gov)) +
  geom_col(position = "fill")

data.table

的解决方案
dataset <- data.table(dataset)

to_plot <- dataset[, .(
  female_head_gov = fifelse(
    female_head_gov %in% c("I strongly agree", "I agree"),
    'Agree/strongly agree',
    'Disagree/strongly disagree',
    na = 'Don not know'
  )
), country][, .(prop = .N), .(country, female_head_gov)][, .(prop = prop / sum(prop), female_head_gov), country]


to_plot <-
  merge(to_plot,
        to_plot[female_head_gov == 'Agree/strongly agree'][order(-prop)][, .(country, my_order = 1:.N)],
        by = 'country')

ggplot(to_plot, aes(reorder(country, my_order), prop, fill = female_head_gov)) +
  geom_bar(stat = "identity")