Search code examples
rggplot2geom-bar

Cannot make stacked barplot in ggplot2


I am trying to make a stacked bar plot in ggplot2 but I am not getting it right. Here is the code for making barplot.

ggplot(chr_all_m, aes(chr, value, fill=factor(Genome))) + 
  stat_summary(fun.y = "mean", geom="bar", position=position_dodge(0.95)) + 
  labs(x="chromosome", y="kaks") 

And here is the plot that I am getting it back with this code. As you can see it is not stacked and I thought stacking is the default behavior for most area plots in ggplot2 package. Am I missing something? ggplot2 barplot

I even tried removing position=position_dodge(0.95) option and the figure I am getting is not correct. Obviously I am doing something wrong here... ggplot2 barplot no dodge option

Thanks!


Solution

  • How about position="stack"

    dd<-data.frame(
        chr=rep(paste0("A",1:3), 3),
        Genome=rep(c("LF","MF1","MF2"), each=3),
        value=rpois(9,100)
    )
    
    
    ggplot(dd, aes(chr, value, fill=factor(Genome)))+
    stat_summary(fun.y = "mean", geom="bar", position="stack")