I'm new to R and statistics. I'm trying to make a grouped barplot with group means and sd error bars, using ggplot. I've imported the dataset from excel (xlsx).
Basically, there're two plants (Var) p & c in two treatments (Temp) c & w and their moisture is measured. I'm trying to make a barplot with the means and errorbars grouped as pc, pw and sc, sw. I'm able to make the barplot, but the errorbars I'm stuck with. When I try to calculate the sd separately and then use it as limits in geom_errorbar, it shows error: aesthetics must be either length 1 or the same as the data (112): ymax, ymin. I really don't know what else to do.
Dummy data -
Var Temp moisturep
Purple Teepee cool 89.01974
Purple Teepee cool 80.87346
Safari cool 78.97458
Safari cool 86.98475
Purple Teepee warm 98.98734
Purple Teepee warm 98.76593
Safari warm 87.98745
Safari warm 90.45870
Code -
data <- pgpr
temp.mean <- t(tapply(data$moisturep,
list(data$Var, data$Temp),
mean))
temp.sd <- t(tapply(data$moisturep,
list(data$Var, data$Temp),
sd))
limits <- aes(ymax = temp.mean + temp.sd,
ymin = temp.mean - temp.sd)
ggplot(data,
aes(fill = data$Temp,
x = data$Var,
y = data$moisturep)) +
geom_bar(position = "dodge",
stat = "summary",
fun.y = "mean") +
geom_errorbar(limits,
position = position_dodge(0.9),
width = 0.25)
dput
to share the data – Lunalo Johndata$
in theaes
bit – morgan121