简体   繁体   English

为什么我的 geom_bar 上的条不会使用 reorder() 命令重新排序?

[英]Why won't the bars on my geom_bar reorder with reorder() command?

My dataset has data about college graduates.我的数据集有关于大学毕业生的数据。 Each observation is a major, which fall into twelve categories.每个观察都是一个主要的,分为十二个类别。 I am trying to find the category of majors that have the highest percentage of women.我试图找到女性比例最高的专业类别。 The variables I'm concerned with are major_category (character) and sharewomen (percentage of graduates who were women in each major).我关心的变量是major_category(性格)和sharewomen(每个专业中女性毕业生的百分比)。

I'm able to get the data onto a bar chart, but the bars of the column will not reorder properly.我能够将数据放到条形图上,但列的条形无法正确重新排序。 grads$major_category is a factor, and I have tried reordering the factor by sharewomen outside of the dplyr pipe, which did not work. grads$major_category 是一个因素,我曾尝试通过 dplyr 管道之外的共享女性重新排序该因素,但没有奏效。 I have tried fct_reorder and reorder within aes(), and tried removing coord_flip() and using - or desc() within the reorder command.我在 aes() 中尝试过 fct_reorder 和 reorder,并尝试删除 coord_flip() 并在 reorder 命令中使用 - 或 desc()。 Below is the code I have now and the graph it produces:下面是我现在拥有的代码及其生成的图表:

grads %>%
filter(major_category != "Interdisciplinary") %>%
select(major_category, total, sharewomen) %>%
group_by(major_category) %>%
ggplot(aes(x = reorder(major_category, -sharewomen), y = sharewomen), stat = "identity")+
geom_bar(stat = "identity")  + coord_flip()

bar chart with unordered bars带有无序条形的条形图

I tried your code using the data:我使用数据尝试了您的代码:

grads <-
  tribble(
    ~major_category, ~sharewomen,
    "Business", 6.2,
    "Social Science", 5.1,
    "Biology", 8.05,
    "Education", 12,
    "Interdisciplinary", 1.2
  )

Came out as you described:按照你的描述出来了: 在此处输入图片说明

I also got the same with the variations you described (eg fct_reorder ) and without the group_by .我对您描述的变体(例如fct_reorder )也得到了相同的结果,而没有group_by For instance:例如:

grads %>%
  filter(major_category != "Interdisciplinary") %>%
  select(major_category, sharewomen) %>%
  ggplot(aes(x = fct_reorder(major_category, -sharewomen), y = sharewomen))+
  geom_col() + 
  coord_flip()

Maybe tidyverse dplyr need an update?也许tidyverse dplyr需要更新?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM