ggplot2: Changing the order of stacks on a bar graph

后端 未结 2 1235
有刺的猬
有刺的猬 2020-11-29 11:35

I\'m trying to make a stacked bar graph with a facet_wrap, but I want the order of my stacked variables (\"developed\") to be flipped. I\'ve reordered the factors, and trie

相关标签:
2条回答
  • 2020-11-29 11:41

    Fwiw, here is a solution with dplyr, and it uses scale_fill_manual to be explicit about the colors:

    library(ggplot2)
    library(dplyr)
    
    developed=rep(c("developed","available"),6)
    agriculture=rep(c(rep("loi",2), rep("dryland",2), rep("agroforestry",2)),2)  
    acres=c(7435,24254,10609,120500,10651,75606,6037,9910,4390,895,9747,46893)
    islands=c(rep("All islands",6), rep("Oahu",6))
    all_is2=data.frame(developed, agriculture, acres, islands)
    
    all_is2$agriculture=factor(all_is2$agriculture,levels=c("loi","dryland","agroforestry"))
    #all_is2$developed=factor(all_is2$developed,levels=c("available","developed"))
    
    all_is3 <- all_is2 %>% group_by(islands,agriculture,developed) %>% 
                           summarize(acres=sum(acres)) 
    
    ggplot(all_is3,aes(x=agriculture,y=acres,fill=developed))+
      geom_bar(position="stack", stat="identity")+
      facet_wrap(~islands)+ 
      xlab("")+ylab("Acres")+theme_bw() +
      scale_fill_manual(name="",values=c("available"="black","developed"="light gray"))
    

    0 讨论(0)
  • 2020-11-29 11:53

    This might be a solution.

    What I did was ordering the dataset, so that the value I wanted to appear closest to the x-axis appeared first in the dataset. (I've used your ordering of factors here). This fixt the positioning of the bars.

    Then, we had to change the colors and order of the legend. I could not wrap my head around scale_fill_grey, so I changed it to scale_fill_manual instead, setting both values and breaks.

    ggplot(all_is2[rev(order(all_is2$developed)),] ,aes(x=agriculture,y=acres,fill=developed))+
      geom_bar(position="stack", stat="identity")+theme_bw()+
      facet_wrap(~islands)+ 
      scale_fill_manual(values=c(developed="grey80",available="grey20"),name="",
                        breaks=c("developed","available"))+
     xlab("")+ylab("Acres")
    

    I don't know if it's a bug or a feature, and I think this also happened with previous versions in ggplot, but it appears that with stat_identity the first observation is plotted closest to the x-axis, the second on top of that etc.

    Demonstration:

    set.seed(123)
    testdat <- data.frame(x=1,y=sample(5))
    
    
    p1 <- ggplot(testdat, aes(x=x,y=y,fill=factor(y))) +geom_bar(stat="identity")+labs(title="order in dataset")
    p2 <- ggplot(testdat[order(testdat$y),],aes(x=x,y=y,fill=factor(y))) +
      geom_bar(stat="identity") + labs(title="ordered by y")
    p3 <- ggplot(testdat[rev(order(testdat$y)),],aes(x=x,y=y,fill=factor(y))) +
      geom_bar(stat="identity") + labs(title="reverse ordered by y")
    

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