1
votes

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
))
1
Please provide sample data to receive a faster, more accurate answer. You can use the dput() function on your data to output a structure, which you can then copy/paste into your question so that we can reproduce this.www
Always a bad idea to use $ in the aes. Use the data argument instead. Cannot be sure without testing, but inherit.aes = FALSE might helpRichard Telford
Thank you both for your quick replies! I've added the sample data as requested, and using the data argument instead of $ didn't seem to help. Although adding inherit.aes= FALSE did yield a simpler error of Error: Aesthetics must be either length 1 or the same as the data (128): ymin, ymax.amb
Is it perhaps because I'm using two different data frames to make the bar chart and to make the error bars?amb

1 Answers

0
votes

Using two different data frames for your plot and errors is not the problem. The problem is how they are calculated. You are first melting your data yielding 128 entries and for errors/mean you are aggregating by Tr which gives you 5 entries, but your data has 5x3 categories.

A possible solution is shown below.

You could plot only the means and the errors. The aggregation would be done on Tr and variable.

enter image description here

gg <- melt(RootTree, id="Tr", na.rm = T)
errors = aggregate(. ~ Tr + variable, 
                   data = gg, 
                   FUN = sd)
means = aggregate(. ~ Tr + variable, 
                  data = gg, 
                  FUN = mean)

ggplot(means, aes(x = variable, 
                  y = value,
                  fill = factor(Tr))) + 
  geom_bar(stat = 'identity', 
           position = position_dodge(0.7)) + 
  geom_errorbar(aes(ymin = means$value - errors$value,
                    ymax = means$value + errors$value),
                width = 0.3,
                position = position_dodge(0.7)) +
  xlab("Seedlings") +
  ylab("Mean of Root Weight (gr)") + 
  ggtitle("Root Weight of Seedlings by Treatment")