my_file <- read.csv("https://github.com/rfordatascience/tidytuesday/blob/master/data/2018-10-16/recent-grads.csv")
我使用以下代码:
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()
答案 0 :(得分:3)
您需要先summarise
个类别:
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()