ggplot barplot in decending order

Can anyone help me how i reorder bar plot in decreasing order. The following code gives me bar plot in ascending order but i want it to be descending order. I tried as. factor and tried to reorder y axis.None works. I also did not understand why we need to use -most_award in x aes. I am new in r programming and will appreciate any help

# which player has won more than 10 man of the match award

most_mom_award <- match%>%
  group_by(player_of_match)%>%
  summarize(most_award = n())%>%
  filter(most_award >= 10)
# let's visualize to see who are those players who have 
# won more than 10 mom awards

ggplot(
   most_mom_award,
   aes(
      x =  reorder(player_of_match, -most_award), 
      y = most_award)
   ) +
  geom_bar(stat = "identity")+
  coord_flip()+
  xlab("Players")

Here are three examples of graphing a simple data frame which may help you understand how the reorder function works. It orders the first argument as a function of the second argument. Also, keep in mind that you are using coord_flip, so you reorder the x axis and then it is flipped to become the y axis.

library(ggplot2)
df <- data.frame(Name = c("B", "C", "D", "A"), Number = c(4, 7, 2, 9))
df
#>   Name Number
#> 1    B      4
#> 2    C      7
#> 3    D      2
#> 4    A      9

ggplot(data = df, mapping = aes(x = Name, y = Number)) + 
  geom_bar(stat = "identity") + coord_flip()


ggplot(data = df, mapping = aes(x = reorder(Name, Number), Number)) + 
  geom_bar(stat = "identity") + coord_flip()


ggplot(data = df, mapping = aes(x = reorder(Name, -Number), Number)) + 
  geom_bar(stat = "identity") + coord_flip()

Created on 2019-05-18 by the reprex package (v0.2.1)

3 Likes

Thanks for the clarification. My error was on -most_award instead i should have used most_award. It might be a dummy question but what does "-" means in this scenario.

I could make something up but, honestly, I had not seen that use of the negative sign with reorder until I saw it in your example. You taught it to me!

1 Like

Minus sign allows you to switch between ascending/descending order.

Personally, I have trouble telling from the top of my head which is which, so I usually try them both and see which one I need :slight_smile:

1 Like

Thank you. That is what i thought too but wasn't quiet sure.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.