0

I have problem to keep the original order in ggplot's geom_bar plot while faceting. Found some online help but they did not work when I tried to facet.

Code:

df <- as.data.frame(cbind(x =rep(c("N-on", "N-off", "R-on", "R-off"),2),
            y = c(13,6,7,11,20,16,17,19), z = c(rep("A", 4), rep("B", 4))))
ggplot(data=df, aes(x=x, y=y)) +
  geom_bar(stat="identity") +
  facet_wrap(~ z, ncol =1) +
  coord_flip()

Output: enter image description here

Expected output: Labels in the vertical axis will be in original order e.g., "N-on", "N-off", "R-on", "R-off".

enter image description here

Jaap
  • 81,064
  • 34
  • 182
  • 193
Rashedul Islam
  • 879
  • 1
  • 10
  • 23
  • 4
    Use `data.frame(x=..., y=..., z=...)` to create your sample data. `cbind` creates a matrix and in this case coerces everything to character (because all elements of a matrix must be of the same type), which then gets coerced to factor by `as.data.frame`. That's why the y-axis of your plot is messed up. – eipi10 Dec 14 '15 at 04:03
  • According to your comment, I used: >`df <- data.frame(x =rep(c("N-on", "N-off", "R-on", "R-off"),2), y = c(13,6,7,11,20,16,17,19), z = c(rep("A", 4), rep("B", 4))) ggplot(data=df, aes(x=x, y=y)) + geom_bar(stat="identity") + facet_wrap(~ z, ncol =1) + coord_flip()` But output is same as before. – Rashedul Islam Dec 14 '15 at 04:13
  • The y-axis is still in the wrong order? – eipi10 Dec 14 '15 at 04:15
  • @eipi10. Yes, vertical axis is same. – Rashedul Islam Dec 14 '15 at 04:20
  • 1
    Vertical axis or y-axis? The y-axis is your numeric axis and is printed horizontally in your question (due to `coord_flip()`). Fixing the sample data frame will just fix the y-axis. Use @jMathew's answer (or my comment to his answer) to fix the order of the x-axis categories. – eipi10 Dec 14 '15 at 04:22
  • @eipi10: This code runs perfectly without `coord_flip()`. However, it messed up with `coord_flip()` `>df <- as.data.frame(cbind(p = rep(c("N-on", "N-off", "R-on", "R-off"),2), q = c(13,6,7,11,20,16,17,19), z = c(rep("A", 4), rep("B", 4)))) df$p = (factor(df$p, levels=c("N-on", "N-off", "R-on", "R-off"))) ggplot(data=df, aes(x=p, y=q)) + geom_bar(stat="identity") + facet_wrap(~ z, ncol =1)` – Rashedul Islam Dec 14 '15 at 05:10

2 Answers2

6

Apparently geom_bar plots in order of the levels of factor ref

It seems that the order is preserved, in your plot except that its from bottom up. This seems to work,

df$x <- factor(df$x, levels=rev(levels(df$x)))


ggplot(data=df, aes(x=x, y=y)) +
geom_bar(stat="identity") +
facet_wrap(~ z, ncol =1) +
coord_flip()
Community
  • 1
  • 1
jMathew
  • 1,057
  • 8
  • 13
  • 1
    Sorry it gives `"N-off", "N-on", "R-off", "R-on"` order instead of `"N-on", "N-off", "R-on", "R-off"` order. I do not sort but want to keep the order as it is. Thanks! – Rashedul Islam Dec 14 '15 at 04:00
  • 4
    Then just do `df$x = factor(df$x, levels=c("N-on", "N-off", "R-on", "R-off"))` to get the order you want. In general, set the factor levels to whatever order you desire, and that's the order they will be plotted in. – eipi10 Dec 14 '15 at 04:04
  • 1
    I did this before but I am not sure why it is not working! Is is working when you run? `df <- as.data.frame(cbind(x =rep(c("N-on", "N-off", "R-on", "R-off"),2), y = c(13,6,7,11,20,16,17,19), z = c(rep("A", 4), rep("B", 4)))) df$x = factor(df$x, levels=c("N-on", "N-off", "R-on", "R-off")) ggplot(data=df, aes(x=x, y=y)) + geom_bar(stat="identity") + facet_wrap(~ z, ncol =1) + coord_flip()` – Rashedul Islam Dec 14 '15 at 04:22
  • Yes, it works for me. – eipi10 Dec 14 '15 at 04:24
0

Finally reverse order of levels and using data.frame worked for me. Thanks a lot @eipi10 and @jMathew for your suggestions. I modified the code little bit to remove ambiguity.

    df <- data.frame(p =rep(c("N-on", "N-off", "R-on", "R-off"),2),
                          q = c(13,6,7,11,20,16,17,19), 
                          z = c(rep("A", 4), rep("B", 4)))
#reverse order of the vertical axis data
df$p = factor(df$p, levels=rev(c("N-on", "N-off", "R-on", "R-off")))
#run ggplot
ggplot(df,        
       aes(x=p, y=q, fill=p)) +
  # adding or removing the aesthetic "order=vartype" doesn't change anything
  geom_bar(stat = "identity") + 
  facet_wrap(~ z, ncol = 1) +
  #facet_grid(. ~ z) + 
  coord_flip()

Output:

enter image description here

Rashedul Islam
  • 879
  • 1
  • 10
  • 23