Lora
Lora

Reputation: 3

Sort or order multiple bars with geom_col barplot

I have this kind of table:

Year    Substance     Number
2013        A           32
2013        B           27
2013        C           17
2013        D           17
2013        E           15
2013        F           13
2014        B           20
2014        D           17
2014        A           16
2014        C           11
2014        F            9
2014        G            3

Basically, the years go up to 2018 with 6 or 7 substances every year, and each substance has a number (frequency of occurrence). The substances have actual names, but I cannot publish them on the Internet, so I changed them for A, B, C, D, E, F, and G. I am unable to order the bars as I want, in decreasing order.

I did a lot of research on the Internet and tried many things: forcats, factor, levels, reorder, etc. and none of it worked. I have an R novice, so I don't really now what would be the best way to do what I want.

When I try to plot like this, it places the substance in alphabetical order:

ggplot(Test, aes(x = Year, y = Number, fill = Substance)) + geom_col(position = "dodge")

For the first year, 2013, the order is right. I want it to look like that, in decreasing order, for every other year. What should I do?

Upvotes: 0

Views: 605

Answers (1)

Andrew Royal
Andrew Royal

Reputation: 346

This is kind of tricky because your ordering is changing by year, so factor variable conversion gets messy. Here is one way to do it by sorting x position using a separate numeric value:

library('data.table')
library('ggplot2')

Test[, Ranking:= rank(-Number, ties.method = 'first'), by = .(Year)]
ggplot(Test, aes(x = Ranking,
                 y = Number, 
                 fill = Substance)) +
  geom_col(position = 'dodge') +
  scale_x_continuous(name = '', breaks = 0) +
  facet_wrap(~Year)

Output:

enter image description here

Upvotes: 0

Related Questions