0

How can I sort a bar graph from highest to lowest in R? (ggplot2)

the code is this, but feel free to do a better code haha Ps: it is a huge data

ggplot(kiva, aes(repayment_interval, loan_amount, fill = repayment_interval)) + 
  geom_bar(stat = "identity") +
  ggtitle("Total of loan for different types of repayment intervals")
neilfws
  • 32,751
  • 5
  • 50
  • 63
Liztc
  • 5
  • 1
  • 3

1 Answers1

0

I assume you want highest to lowest y-variable, loan_amount.

Difficult to answer without example data but something like this should work, using reorder. Also geom_col = less typing.

ggplot(kiva, 
       aes(reorder(repayment_interval, -loan_amount), loan_amount), 
           fill = repayment_interval)) + 
geom_col() +
ggtitle("Total of loan for different types of repayment intervals")
neilfws
  • 32,751
  • 5
  • 50
  • 63