Lucia
Lucia

Reputation: 21

Changing bar order when using stat_summary with ggplot

How can I change the order in this plot based on the values of column?

enter image description here

Upvotes: 1

Views: 969

Answers (1)

Sal-laS
Sal-laS

Reputation: 11649

This example should help you. You just need the reorder

Based on R documentation:

reorder is a generic function. The "default" method treats its first argument as a categorical variable, and reorders its levels based on the values of a second variable, usually numeric.

library(ggplot2)
dt<-data.frame("Name"=c("A","B","C","D","E"),value=sample(5))

ggplot(dt, aes( x= reorder(Name, value) , y= value))+
  geom_col(stat='identity', aes(fill=Name), width=.5)

The result is:

enter image description here

Upvotes: 6

Related Questions