Reputation: 107
my_file <- read.csv("https://github.com/rfordatascience/tidytuesday/blob/master/data/2018-10-16/recent-grads.csv")
I use this code:
my_file %>%
mutate(Major_category = fct_reorder(Major_category,Unemployed)) %>%
ggplot(aes(x= Major_category,y= Unemployed, fill = Major_category)) +
geom_bar(stat = "identity") +
theme_minimal() +
labs(title = "Unemployed Students in Various Majors", x ="Major") +
theme(legend.position = "none",
plot.title = element_text(hjust = 0.5)) +
coord_flip()
Even after using reorder()
, the levels are not reordered? Please help
Upvotes: 0
Views: 1039
Reputation: 2246
you need to summarise
the categories first:
library(tidyverse)
my_file <- read_csv("https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2018-10-16/recent-grads.csv")
my_file %>%
# summarise categories
group_by(Major_category) %>%
summarise(Unemployed = sum(Unemployed, na.rm = TRUE)) %>%
#now use your code
mutate(Major_category = fct_reorder(Major_category, Unemployed)) %>%
ggplot(aes(x = Major_category,
y = Unemployed,
fill = Major_category)) +
geom_bar(stat = "identity") +
theme_minimal() +
labs(title = "Unemployed Students in Various Majors", x ="Major") +
theme(legend.position = "none",
plot.title = element_text(hjust = 0.5)) +
coord_flip()
Upvotes: 3