0

I've read through quite a few similar questions, but none fit my use case closely enough for me to apply them successfully.

I have a simple bar graph (geom_bar) constructed from a single variable with levels, type. The code I use to construct it is

r <- ggplot(clv[!is.na(clv$type),], aes(type))
r + geom_bar()

How do I reorder the levels here, such that the bars are displayed with counts in descending order?

(Also, as I think this is probably simple and not deserving of a separate question: How can I label each bar with the total count of that level?)

Feel free to shut this down and link me to an existing answer that applies. I unfortunately haven't been able to find one by myself.


The linked answer worked perfectly!

For anyone in future who wants to see how the code in the linked answer changed to fit my use case, this is the code that worked for me:

clv <- within(clv[!is.na(clv$type),], type <- factor(type, levels=names(sort(table(type), decreasing=TRUE))))
ggplot(clv,aes(x=type)) + geom_bar()
userManyNumbers
  • 904
  • 2
  • 10
  • 26

0 Answers0