Thomas Hunter
Thomas Hunter

Reputation: 31

How do I make a bar chart go in descending order in R?

I provided an image of the code I already have, but how do I make it in descending order?

enter image description here

Upvotes: 1

Views: 584

Answers (1)

Susan Switzer
Susan Switzer

Reputation: 1922

df <- data.frame(
  stringsAsFactors = FALSE,
          type = c('Ave', 'Blvd', 'Cirle', 'Court', 'Dr'),
              total = c(254, 25, 30, 35, 550)
)

ggplot(data = df, 
       mapping = aes(x = fct_reorder(type, total),  y = total)) +
  geom_bar(stat = 'identity', fill = '#112446')+
  theme_bw()

sample

add .desc = TRUE to fct_reorder for descending

ggplot(data = df, 
       mapping = aes(x = fct_reorder(type, total,  .desc = TRUE),  y = total)) +
  geom_bar(stat = 'identity', fill = '#112446')+
  theme_bw()

sample

Upvotes: 5

Related Questions