First off, here's an example data set (FreeF.csv) in .csv format:
I'll read this file into R:Sample,FreeF,State1,63.46,A2,0.5,B3,0.33,C4,0.16,B5,0.45,C6,0.1,C7,0.18,B8,0.6,A9,2.01,A10,16.84,C11,1.27,C12,0.14,B13,1.75,C14,5.13,B15,0.84,A16,0.82,A17,3.52,A18,0.4,C19,0.12,B
> freef = read.csv(file="FreeF.csv",head=T,sep=",")Next, we'll enter a function that I've borrowed and altered that will plot error bars for me from Monkey's Uncle. This function will plot a positive error bar only, unlike the original function.
> error.bar = function(x, y, upper, length=0.1,...){
+ arrows(x,y+upper, x, y, angle=90, code=1, length=length, ...)
+ }For this example data set, I have 3 states: A, B, and C. We'll have to calculate and store the FreeF means and standard errors for each state. We'll use the aggregate() function to do so:
> freef.mean = aggregate(freef$FreeF,by=list(freef$State),FUN=mean)
> freef.sd = aggregate(freef$FreeF,by=list(freef$State),FUN=sd)
> freef.n = aggregate(freef$FreeF,by=list(freef$State),FUN=length)
> freef.sem = freef.sd$x / sqrt(freef.n$x)We can now plot the data. First, we plot the means:
> freef.bp = barplot(freef.mean$x, main="FreeF By State", ylab="FreeF", xlab="State", ylim=range(0,25), names.arg=freef.mean$Group.1, axis.lty=1)This will plot a vertical bar plot for the FreeF means aggregated by State. I'll explain the arguments in the command:
main: Sets the title of the plot
ylab: Sets the y-axis label
xlab: Sets the x-axis label
ylim: Sets the y-axis limits
names.arg: Sets the x-axis labels for each State (A, B, and C)
axis.lty=1: Displays the x-axis in the same style as the y-axis (otherwise, it won't show the x-axis at all)
Now to add the error bars:
> error.bar(freef.bp,freef.mean$x,freef.sem)Those are some massive-looking error bars. Oh well, back to lab.
No comments:
Post a Comment