A bar chart with error bars is another commonly used data visualization technique. In this recipe, we will produce a bar chart with error bars.
To produce a bar chart with error bars, we need to process the dataset in such a way that we have the relevant data at hand. In fact, to produce the error bar, we need to have the standard error of the mean that can be transformed into the lower and upper limit. To make this happen, we will call ggplotdata
first, and then will do some processing using the following code:
# Summarizing the dataset to calculate mean # calculating margin of error for 95% confidence interval of mean bardata <- ddply(ggplotdata,.(econ_status),summarize,n=length(disA),meandisA=mean(disA),sdA=sd(disA),errMargin= 1.96*sd(disA)/sqrt(length(disA))) # transforming the dataset to calculate # lower and upper limit of confidence interval bardata <- transform(bardata, lower= meandisA-errMargin, upper=meandisA+errMargin)
Now...