-1

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!

tonytonov
  • 25,060
  • 16
  • 82
  • 98
upendra
  • 2,141
  • 9
  • 39
  • 64
  • 1
    By including `position=position_dodge(0.95)` you have explicitly told ggplot to dodge, not stack. See the [documentation](http://docs.ggplot2.org/current/). – joran May 16 '14 at 20:36
  • @joran, please see my edit above in my original post.. – upendra May 16 '14 at 20:43
  • Please re-read my comment and take some time to examine the documentation, in this case `?stat_summary`, where it clearly indicates that the default position is "identity". – joran May 16 '14 at 20:44
  • sorry i am missing something here...i tried `position="identity"` but i am getting the same plot as above (2nd plot). But unfortunately the stacking is not correct. As you can see `A01` has three side bars but in the stacked one there is only one. – upendra May 16 '14 at 21:07
  • 1
    Go the documentation page I linked to. Scroll down to the Position section. You've tried dodge and identity, and neither worked. Is there another one that looks promising? – joran May 16 '14 at 21:42
  • @jaron Does the documentation change must from version to version? I have version 0.9.3.1 of ggplot2. I don't see any indications on the `?stat_summary` help page about what `position` values are allowed. All it says is "The position adjustment to use for overlappling points on this layer" – MrFlick May 17 '14 at 03:37
  • @MrFlick That's why I specifically linked to the web docs, which group all geoms, stats, positions, etc together in a big list, so that the OP could see what position adjustments are available. – joran May 19 '14 at 14:57

1 Answers1

1

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")
MrFlick
  • 195,160
  • 17
  • 277
  • 295
  • you are awesome. Thanks for the hint. It worked. How come i can't find the `position=stack` option anywhere. Anyway thanks.... – upendra May 17 '14 at 06:02