首页 > 解决方案 > 无法对列重新排序 (fct_reorder())

问题描述

我正在尝试重新排序列以获得降序列的形成。我到目前为止的情节附在下面:

在此处输入图像描述

理想情况下,情节应该是这样的,但仍充满专家级别。

在此处输入图像描述

这是我到目前为止的 R 代码:

# !!!
survey_data_barchart %>%
    dplyr::filter(!is.na(expert.level)) %>% 
    dplyr::filter(!is.na(country)) %>% 
    group_by(expert.level) %>% 
    count(country) %>%
    # mutate(country = fct_reorder(country, n)) %>% 
    ggplot(aes(x = reorder(country, +n), n, fill = expert.level)) +
    geom_bar(stat = "identity") +
    coord_flip() +
    theme(legend.position="top",
          axis.text.x = element_text(size = 20, vjust = 1.3),
          axis.text.y = element_text(size = 15),
          legend.title = element_blank(),
          axis.ticks = element_blank(),
          # axis.title.y = element_blank(),
          # axis.title.x = element_blank(),
          rect = element_blank(),
          legend.text = element_text(size=25),
          axis.title.x = element_text(size=20, vjust = 0.5)) +
    guides(fill = guide_legend("")) +
    labs(x = "",
         y = "Responses") +
    scale_fill_brewer(palette = "Set2")
    #scale_x_discrete(guide = guide_axis(n.dodge = 2))

进入 ggplot 表达式的数据如下所示:

# A tibble: 104 × 3
# Groups:   expert.level [5]
   expert.level           country         n
   <chr>                  <chr>       <int>
 1 Anaesthesia technician Belgium         2
 2 Anaesthesia technician Greece          1
 3 Anaesthesia technician Moldova         1
 4 Anaesthesia technician Romania         2
 5 Anaesthesia technician Russia          3
 6 Anaesthesia technician Switzerland     1
 7 Consultant             Albania         3
 8 Consultant             Austria        24
 9 Consultant             B&H             4
10 Consultant             Belarus         1

感谢您的帮助,祝您周末愉快!

格里戈里

标签: rggplot2visualization

解决方案


推荐阅读