ggplot:改变基础上,一个名字的酒吧内堆积条的顺序(ggplot: change the ord

2019-08-21 02:46发布

ggplot(data,aes(x=ab,y=Freq/total,fill=Result))+
      geom_bar(stat="identity")+
     theme(strip.text.x = element_text(size=8, angle=0),
      strip.background = element_rect(colour="black", fill="#CCCCFF"))+
    ggtitle("H.somnus SIR %")+ylab("% SIR")+
    scale_y_continuous(labels=percent,breaks=seq(0,1,.1))+
    theme_set(theme_barplot())

以上是我使用的代码。 数据是我所熔融的表,但该列“结果”是在作为字母和STR(结果)的顺序与4个级别的因子:如A,B,C,d。 我想显示在底部和订单最大的酒吧酒吧将是d,B,C,A

谢谢

Answer 1:

这是一个有点砍死修复,但它的工作原理。 ggplot将绘制堆叠条形它采用STAT =“身份”的时候遇到它们的顺序。 为了获得订单d堆栈,B,C,A重新排序data.frame是这样的:

data <- data[c(data$Result == "D",
               data$Result == "B",
               data$Result == "C",
               data$Result == "A"),]

在进入GGPLOT2帮助文件可以在这方面做得更好。



文章来源: ggplot: change the order of stacked bars based on the a name within the bar