I have the below bar chart where I am plotting various shopping categories and the percentages. I would like to order them in descending order, can you please advise?
S2<- ggplot(data = data) +
geom_bar(mapping = aes(x = category), ..prop.., group = 1), stat = "count") +
scale_y_continuous(labels = scales::percent_format()) +
coord_flip()+ ylab("Total transactions %") + labs(title="Total Transactions by Category")+
theme_classic()
S2
However if I try to use fct_reorder() in the code below it breaks:
data %>%
count(category) %>%
mutate(category = fct_reorder(category, n, .desc = TRUE)) %>%
ggplot(aes(x = category, y = n)) + geom_bar(stat = 'identity')+
geom_bar(mapping = aes(x = category), ..prop.., group = 1), stat = "count")+
coord_flip()+ ylab("Total transactions %") + labs(title="Total Transactions by Category")+
theme_classic()