# creating errror bars with ggplot2

Hey everyone,

I want to create error bars with my data.
I found this code, but I dont know which kind of error bar it represents. Do they represent the standard devation, standard error or confidence interval ?

Thanks for help!

#Dataframe

Methode <- c(rep(c("Methode A", "Methode B"), c(10,10)))
Lernerfolg <- c(15,45,16,41,7,48,46,37,40,35,5,36,18,25,10,40,43,30,35,29)
data <- data.frame(Methode, Lernerfolg)

# specify variables

Faktor <- "Methode" # Name of Factor
AV <- "Lernerfolg" # dependent varaible
CI <- .95 #confidence interval

# calculate widths of confidence interval

data2 <- na.omit(data[, c(AV, Faktor), drop=FALSE])
Kennwerte <- function(x) c(lsmean=mean(x), lower.CL=mean(x)-sd(x)/sqrt(length(x))*qt((1+CI)/2, length(x)-1), upper.CL=mean(x)+sd(x)/sqrt(length(x))*qt((1+CI)/2, length(x)-1))
Formel <- as.formula(paste(".~", Faktor))
data.aggr <- aggregate(Formel, data2[,c(Faktor, AV)], Kennwerte)
data.aggr <- data.frame(data.aggr[,Faktor, drop=FALSE],data.aggr[,AV])

# plot diagram

library(ggplot2)
ggplot(data.aggr, aes_string(x=Faktor, y="lsmean", width=0.75)) + geom_bar(colour="black", fill="gray", stat="identity", position="identity") + geom_errorbar(aes(ymax = upper.CL, ymin = lower.CL), width=0.25) + ylab(AV) + theme_bw()

This comment in the code itself seems to imply it is calculating the confidence interval.

In R, you can easily calculate the standard deviation of `x` by using `sd(x)`.

For the standard error, you'd do something like `sd(x)/sqrt(length(x))`.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.