1
votes

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

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. I have tried fct_reorder and reorder within aes(), and tried removing coord_flip() and using - or desc() within the reorder command. 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

1
Try removing the group_by; tibbles typically won't let you modify a grouping variable.joran
when I remove the group_by, the reorder() command does nothing (the major_category defaults to alphabetical order)Faye Elisabeth Kollig

1 Answers

1
votes

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: enter image description here

I also got the same with the variations you described (eg fct_reorder) and without the 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?