ggplot - Order bars per value

Problem

We have a barchart with different lengths. We want to order the bars by decreasing value

data <- data.frame("patient"=c("A", "B", "C"), "value"=c(25, 20, 30))
ggplot(data) + geom_col(aes(x=patient, y=value))
  patient value
1       A    25
2       B    20
3       C    30

Solution

Use reorder in the aestetics and provide the value to reorder by, put - to reorder by decreasing value.

ggplot(data) + geom_col(aes(x=reorder(patient, -value), y=value))

How can we help?

Whether you already know about your data pain points or just want to have a friendly conversation, send us a message and tell us who you are.