1

I have the following dataset:

      Marker   Method NAtype        Ratio
1     CSF1PO      GEL     -9 3.623417e-01
2     CSF1PO      GEL     -1 4.713273e-02
3     CSF1PO      GEL     NA 0.000000e+00
4     CSF1PO MegaBACE     -9 1.417205e-02
5     CSF1PO MegaBACE     -1 8.312974e-03
6     CSF1PO MegaBACE     NA 5.405026e-06
7     CSF1PO      ABI     -9 4.714592e-02
8     CSF1PO      ABI     -1 1.989925e-03
9     CSF1PO      ABI     NA 4.174494e-05
10  D10S1248      GEL     -9 9.999201e-01
11  D10S1248      GEL     -1 7.098303e-05
12  D10S1248      GEL     NA 0.000000e+00
13  D10S1248 MegaBACE     -9 6.562637e-01
14  D10S1248 MegaBACE     -1 2.518769e-03
15  D10S1248 MegaBACE     NA 1.081005e-05
16  D10S1248      ABI     -9 7.138484e-03
17  D10S1248      ABI     -1 1.029723e-03
18  D10S1248      ABI     NA 1.391498e-05

I want to make a stacked bar graph with these data. I'm not fluent in ggplot2 and I don't know how to create 3 bars for each category.

For example, the 1st column has my categories, these are the names that go in the x axis of my graph. For each category ("CSF1PO" and "D10S1248"), I want 3 bars ("GEL", "MegaBACE", "ABI" in the 2nd column). Each bar should be divided in 3 subsets ("-9", "-1", "NA" in the 3rd column). And the 4th column (Ratio) has my values.

I want something like

ggplot(data, aes(x=Marker, y=Ratio, fill=NAtype)) + geom_bar(stat="identity") + facet_grid(~Method)

but with the bars for each category side-by-side, not in separated grids. Can anyone help please? Thanks!

vitor
  • 1,240
  • 2
  • 13
  • 27
  • If you're trying to both dodge and stack the bars, I don't [believe](http://stackoverflow.com/q/12715635/3243640) you can. – joran Oct 31 '12 at 20:06

1 Answers1

2

ggplot2 does not allow you to include both stack and dodge as positions in the same plot. But you can achieve what you want by using facet_wrap. See this example with your data:

ggplot(df, aes(Method, Ratio, fill = Method)) + 
geom_bar(stat = "identity", postion = "dodge") + facet_wrap(~Marker)

enter image description here

Maiasaura
  • 32,226
  • 27
  • 104
  • 108
  • that's interesting. Thanks! But now I don't have the subsets of my data (df$NAtype) plotted... they should be the stacks in the bars. So it's not possible to do that? – vitor Oct 31 '12 at 21:36