ggplot2 aes的替代方案:订单功能?

时间:2016-01-21 14:05:35

标签: r ggplot2

有人知道排序ggplot2条形图堆栈的替代方法吗?

我曾经使用过例如

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)

创建像

这样的图表

enter image description here

然而,在版本2.0.0中,ggplot2 order()已被删除。现在图表就像:

enter image description here

有人知道另一种选择吗?

0 个答案:

没有答案