1

I have been trying to do something that seems like it should be very simple but I cannot get it right, to reorder a grouped geom_bar plot by a numeric value. Reading other questions on here it was suggested to use reorder for factors, but it seems to be ignored here. I would like to avoid using grid.arrange like in this question if possible because I like the facets, but I don't understand what else to try, any suggestions are very welcome.

After reading the suggested posts here, all work to re-order the data and these examples are all with facet_grid, but I would like my "a" groups in 2 different rows so I need to use facte_wrap I guess and nothing works for labelling correctly the x-axis in facet_wrap... I have gotten this far:

df <- data.frame(a = factor(c(1,1,2,1,2,2,1,2)), 
         b = factor(c(1,2,3,4,5,6,7,2)), 
         colb = as.character(c("#00AF4F", "#000FEF", "#004FAF", "#003FBF", "#008F6F", "#00DF1F", "#002FCF", "#000FEF")),
         c = factor(c(1,2,1,2,1,2,1,2)),
         value= 1:8)

In base R I am trying to get a facet for each of the levels a like this, colored by colb:

plot(df$b[df$a==1], df$value[df$a==1])
plot(df$b[df$a==2], df$value[df$a==2])

Thanks to the help below, this is working...

df <- transform(df, category2 = factor(paste(a, b)))
df <- transform(df, category2 = reorder(category2, rank(value)))

ggplot(df, aes(category2, value, fill=colb)) +
  geom_bar(stat = "identity") +
 facet_grid(. ~ a, scales = "free_x") +
 scale_x_discrete(labels=df$b, breaks=df$category2)

but then when I try facet_wrap instead I can't get it to work with correct labels for b:

ggplot(df, aes(category2, value, fill=colb)) +
 geom_bar(stat = "identity") +
 facet_wrap( ~ a, nrow=2, scales = "free_x")

And now with correct labels! Thank you aosmith!

ggplot(df, aes(category2, value, fill=colb)) +
geom_bar(stat = "identity") + scale_x_discrete(labels=df$b, breaks=df$category2) + facet_wrap( ~ a, nrow=2, scales = "free_x")

What am I doing wrong? If there is no solution with facet_wrap, is there any other way I can place the groups in separate grids? I have been banging my head on this for days now...any help is VERY appreciated... Thank you very much...

Community
  • 1
  • 1
user971102
  • 3,005
  • 4
  • 30
  • 37
  • Thank you aosmith! Yes it is although I can't get ti to work properly: mmm yes but it doesn't work...library(dplyr) df = df %>% group_by(c) %>% mutate(position = rank(-value)) ggplot(df,aes(c,value,fill=colb, group=position))+ geom_bar(position="dodge",stat="identity") + scale_fill_manual(values=df$b) + facet_wrap(~ a) – user971102 Oct 27 '15 at 17:46
  • 2
    See [here](http://stackoverflow.com/questions/5409776/how-to-order-bars-in-faceted-ggplot2-bar-chart) for another option. – aosmith Oct 27 '15 at 17:49
  • Ok this almost got me where I need to be but not quite... I am struggling with trying to redifine the x-axis labels though: df = ddply(df, .(c), transform, hrank = rank(value)); ggplot(df,aes(-hrank,value,fill=colb))+ geom_bar(position="dodge",stat="identity") + facet_wrap(~c, nrow =3) + scale_x_discrete(labels=df$variable, breaks=df$hrank) The scale_x_discrete doesn't work...I have tried everything I am out of resources really... Any ideas would really help... – user971102 Nov 03 '15 at 22:48
  • or like this : http://stackoverflow.com/questions/5208679/order-bars-in-ggplot2-bar-graph/9231857#9231857 – Alex Brown Nov 03 '15 at 23:04
  • Thank you Alex! ... I can order stuff as I would like by the hack above, but then I can't define the labels anymore... I am going crazy...defining the limits as suggested doesn't work either...+ scale_x_discrete(limits = factor(df$hrank)) – user971102 Nov 04 '15 at 15:56
  • Hi aosmith, thank you. I am simply trying to plot b versus value ordered by value (with colors specified by colb) for each level of a, so no further expansion should be needed...but I would like to use facet_wrap because I would like each value of a plotted in different rows but all of the examples and answers use facet_grid so I can't get it right :/ – user971102 Nov 04 '15 at 21:29
  • 1
    Did you forget `scales = "free_x"`? No reason I know of that answer shouldn't work with `facet_wrap`... – aosmith Nov 04 '15 at 21:34
  • Thank you aosmith! I tried this but it still doesn't give me the labels of b (and the scale_x_discrete stretches the axis plot too which is not good...): ggplot(ttt,aes(-position,value,fill=colb))+ geom_bar(position="dodge",stat="identity") + facet_wrap(~a, nrow =3, scales = "free_x") + scale_x_discrete(labels=df$b, breaks=df$position) – user971102 Nov 05 '15 at 15:22
  • 1
    Try following [this answer](http://stackoverflow.com/a/5414445/2461552) exactly. Note the first two steps, where you make a variable that is the combination of the x variable and the facet variable and then order that variable. Then follow the rest of the algorithm, except use `facet_wrap`. If that plot isn't what you want you may need to clarify exactly how you picture the final product. – aosmith Nov 05 '15 at 16:22
  • Thanks again so much for the help! I have added your suggestion to the main question. This is what I have been trying to achieve, but I just don't understand how to get it to work with facet_wrap instead of facet_grid with correct labels... – user971102 Nov 06 '15 at 04:01

0 Answers0