Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201839

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

$
0
0

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


Viewing all articles
Browse latest Browse all 201839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>