I have a data.frame :
df <- structure(list(sample = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L,
2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("A",
"B", "C"), class = "factor"), replicate = c(1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L),
treatment = structure(c(1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L,
2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L), .Label = c("V0",
"V2", "V4", "V6"), class = "factor"), mean = c(72.5, 66.4,
60.3, 56.3, 90.7, 86.9, 68.8, 64.6, 73.3, 69.8, 62.3, 68.7,
77.1, 73.6, 65.9, 65.3, 75.7, 73.8, 65.9, 66.8), sd = c(5.4,
6.1, 3.5, 4.7, 1.2, 4.9, 8.4, 6.1, 7.3, 3.5, 5.9, 2.2, 3.2,
3, 3.1, 4.2, 4.2, 2.4, 2.2, 2.6)), .Names = c("sample", "replicate",
"treatment", "mean", "sd"), class = "data.frame", row.names = c(NA,
-20L))
For some samples I have replicates while for others I don’t :
> head(df, 15)
sample replicate treatment mean sd
1 A 1 V0 72.5 5.4
2 A 1 V2 66.4 6.1
3 A 1 V4 60.3 3.5
4 A 1 V6 56.3 4.7
5 B 1 V0 90.7 1.2
6 B 1 V2 86.9 4.9
7 B 1 V4 68.8 8.4
8 B 1 V6 64.6 6.1
9 C 1 V0 73.3 7.3
10 C 1 V2 69.8 3.5
11 C 1 V4 62.3 5.9
12 C 1 V6 68.7 2.2
13 C 2 V0 77.1 3.2
14 C 2 V2 73.6 3.0
15 C 2 V4 65.9 3.1
I would like to plot means in a faceted bar plot for each sample value. I would like to get grouped bars when replicates > 1. Is it possible with ggplot2 ?
What I get so far :
g <- ggplot(df, aes(x=treatment, y=mean, fill=sample))
g <- g + geom_bar(position="dodge")
g <- g + geom_errorbar(aes(ymax=mean+sd, ymin=mean-sd), position="dodge")
g <- g + facet_grid(. ~ sample)
g

EDIT
Following the suggestion of @Tyler Rinker, I get :
g <- ggplot(df, aes(x=treatment, y=mean, fill=replicate))
g <- g + geom_bar(position="dodge", stat="identity")
g <- g + geom_errorbar(aes(ymax=mean+sd, ymin=mean-sd), position="dodge", width=0.25)
g <- g + facet_grid(. ~ sample)

However, I would like to get the bars for the ‘C’ sample replicates displayed side-by-side.
EDIT2 Very close to …
Simply by adding group = replicate :
g <- ggplot(df, aes(x=treatment, y=mean, group=replicate, fill=replicate))
g <- g + geom_bar(position="dodge", stat="identity")
g <- g + geom_errorbar(aes(ymax=mean+sd, ymin=mean-sd), position="dodge", width=0.25)
g <- g + facet_grid(. ~ sample)

However there is a remaining problem with the position of the error bars. I guess I have to play with dodge ? But I don’t know how …
EDIT3 Eventually… thanks to @joran
dodge <- position_dodge(width = 0.9)
I checked that other values than 0.9 did not lead to a correct positioning of the error bars
g <- ggplot(df, aes(x=treatment, y=mean, group=replicate, fill=replicate))
g <- g + geom_bar(position="dodge", stat="identity")
g <- g + geom_errorbar(aes(ymax=mean+sd, ymin=mean-sd), position=dodge, width=0.25, color="black")
g <- g + facet_grid(. ~ sample)

Try this and experiment a bit:
I think the different dodging in each panel was confusing the width setting. I’d have to read up a bit on the details of dodging (and there are several people around these parts who may be able to weigh in more easily than myself…) but I think that when you set the dodging “locally” in each geom, ggplot is deciding things about widths based upon information in each layer separately. So the trick here seemed to be to ensure a “global” dodge setting.