1

Does somebody know a alternative method for ordering stacks of a ggplot2 bar graph?

I used to use for example

library(ggplot2)
library(plyr)

a <- cbind(rep("a",5),sample(1:100,5), rep_len(c("1","2","3"),5))
b <- cbind(rep("b",7),sample(1:100,7), rep_len(c("1","2","3"),7))
c <- cbind(rep("c",3),sample(1:100,3), rep_len(c("1","2","3"),3))
d <- cbind(rep("d",10),sample(1:100,10), rep_len(c("1","2","3"),10))
e <- cbind(rep("e",15),sample(1:100,15), rep_len(c("1","2","3"),15))

dat <- rbind(a,b,c,d,e)
colnames(dat) <- c("x","count","example")
dat <- as.data.frame(dat)
dat$x <- as.character(dat$x)
dat$count <- as.numeric(dat$count)
dat$example <- as.character(dat$example)

GP <- ggplot(dat, aes(x= reorder(x, count, sum), y=count, fill = example, order = desc(count)))+ 
  geom_bar(stat="identity", fill= "grey", colour= "black", size = 1)+
  coord_flip() + 
  scale_y_continuous()+
  scale_x_discrete('')+
  #scale_fill_brewer()+
  labs(y="")+
  theme_bw()+
  theme(axis.text.y=element_text(size=8,face="bold"),
      axis.text.x=element_text(size=10,face="bold"), 
      axis.title.x=element_text(size=16,face="bold"),
      axis.title.y=element_text(size=16,face="bold"),
      plot.title=element_text(size=16,face="bold"),
      strip.text.x = element_text(size=10,face="bold"),
      strip.background = element_blank())
print(GP)

to create graphs like

enter image description here

however in version 2.0.0 of ggplot2 order() has been removed. and now the graph will be like:

enter image description here

Does anybody know a alternative?

Tanks

Robin
  • 59
  • 6
  • can you dput an example of `dat` – mtoto Jan 21 '16 at 15:13
  • 1
    Possible duplicate of [ggplot2: Changing the order of stacks on a bar graph](http://stackoverflow.com/questions/34716245/ggplot2-changing-the-order-of-stacks-on-a-bar-graph) – Heroka Jan 21 '16 at 15:21
  • Possible duplicate of [How do you order the fill-colours within ggplot2 geom\_bar](http://stackoverflow.com/questions/15251816/how-do-you-order-the-fill-colours-within-ggplot2-geom-bar) – aosmith Jan 22 '16 at 15:34

0 Answers0