0

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?

aosmith
  • 34,856
  • 9
  • 84
  • 118
Lora
  • 3
  • 2

1 Answers1

0

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

Andrew Royal
  • 336
  • 1
  • 5
  • Thank you for your quick answer :) When I try your solution, I have the following error : Error in FUN(X[[i]], ...) : object 'Ranking' not found I already have the ggplot2 and data.table packages, so I don't understand where this error comes from – Lora Mar 31 '19 at 16:28
  • No problem. It sounds like you haven't assigned the "Ranking" variable-- make sure you are running the first line of code: `Test[, Ranking:= rank(-Number, ties.method = 'first'), by = .(Year)]` and please accept the answer if this works :) – Andrew Royal Mar 31 '19 at 18:19
  • Yes, I did that, but it says : Error in `[.tbl_df`(Test, , `:=`(Ranking, rank(-Number, ties.method = "first")), : unused argument (by = .(Year)) when I try to run the first line And don't worry, I will accept it :) – Lora Mar 31 '19 at 19:44
  • make sure you have `data.table` installed and attached – Andrew Royal Apr 01 '19 at 00:13
  • also ensure "Test" `data.table` object before running the code: `Test <- as.data.table(Test)` – Andrew Royal Apr 01 '19 at 12:37