I'm trying to create error bars for a bar graph, which I created using reshape2 and melting the data. I'm getting the error message below and really can't figure out why! The plot works great until I add the geom_errorbar
line.
gg <- melt(RootTree, id="Tr", na.rm = T)
errors = aggregate(. ~ Tr, data=gg, FUN=sd)
means = aggregate(. ~ Tr, data=gg, FUN=mean)
x_melt <- melt(means, value.name="Mean")
y_melt <- melt(errors, value.name="SD")
datN1 <- merge(x_melt, y_melt)
ggplot(gg, aes(x=variable, y=value, fill=factor(Tr))) +
stat_summary(fun.y=mean, geom="bar",position=position_dodge(0.7)) +
scale_color_discrete("Tr") +
xlab("Seedlings")+
ylab("Mean of Root Weight (gr)") +
scale_fill_manual(values=cbPalette, name="Treatment") +
ggtitle(" Root Weight of Seedlings by Treatment" ) +
geom_errorbar(aes(ymin=datN1$Mean-datN1$SD, ymax=datN1$Mean+datN1$SD), width=0.1)`
Error: Aesthetics must be either length 1 or the same as the data (128): ymin, ymax, x, y, fill
Here is a dput output of the data:
structure(list(Tr = structure(c(5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L,
5L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L), .Label = c("Control", "Gram", "Legu", "Mix",
"Topsoil"), class = "factor"), JP = c(2.59, 2.82, 2.8, 4.26,
5.65, 7.01, 4.51, 3.27, 4.1, 4.73, 2.32, 5.09, NA, NA, NA, 1.47,
2.26, 1.82, 1.31, 4.94, 2.64, 1.12, 2.51, 3.29, 1.83, 1.56, 3.05,
1.98, 2.03, 2.95, 2.57, 2.1, 2.19, 2.92, 1.69, 2.24, 1.13, NA,
NA, 1, 1.37, 1.65, 2.02, 2.47, 1.9), W = c(0.58, 0.4, 0.96, 0.47,
0.69, 0.71, 0.61, 0.39, 0.34, 0.46, 0.13, 0.28, 0.31, 0.21, 0.14,
0.65, 0.75, 0.19, 0.74, 1.8, 1.19, 1.74, 1.39, 0.6, 1.36, 1.91,
0.5, 0.17, 0.25, 0.35, 0.77, 0.75, 0.33, 0.13, 0.14, 0.12, 0.18,
0.29, 1.71, 1.18, 0.51, 0.99, 0.05, 0.36, NA), Ta = c(5.36, 2.8,
4.46, 6.86, 2.05, 2.13, 2.94, 2.85, 3.26, 2.07, 2.5, 1.84, 2.07,
2.5, 1.84, 3.66, 2.61, 1.8, 1.86, 2.89, 2.5, 1.8, 1.69, 1.83,
4.97, 2.22, 3.93, 3.7, 2.58, 3.16, 4.01, 1.85, 2.91, 3.49, 2.96,
NA, 1.53, 2.22, 2.53, 1.55, 1.58, 1.99, 0.54, 2.39, 0.85)), .Names = c("Tr",
"JP", "W", "Ta"), class = "data.frame", row.names = c(NA, -45L
))
$
in theaes
. Use thedata
argument instead. Cannot be sure without testing, butinherit.aes = FALSE
might help – Richard Telfordinherit.aes= FALSE
did yield a simpler error ofError: Aesthetics must be either length 1 or the same as the data (128): ymin, ymax
. – amb