ggplot: change the order of stacked bars based on the a name within the bar

前端 未结 1 1781
心在旅途
心在旅途 2021-01-15 05:51
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 =         


        
相关标签:
1条回答
  • 2021-01-15 06:41

    It's a bit of a hacked fix but it works. ggplot will plot the stacked bars in the order it encounters them when using stat = "identity". To get the stack in the order D,B,C,A reorder your data.frame like this:

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

    the entry in the ggplot2 help files could be better in this respect.

    0 讨论(0)
提交回复
热议问题