Reputation: 37
I have been given the below function for creating a boxplot with error bars which works nicely.
However I need to add axis labels and I have been two days trying to figure out where to add code like this
# col=c(2,7),ylab="Relative Fitness",xlab="Block")
error.bars<-function(Response,x1,x2) {
mean.table<-tapply(Response,list(x1,x2),mean)
mean.table[is.na(mean.table)]<-0
var.table<-tapply(Response,list(x1,x2),var)
n.table<-tapply(Response,list(x1,x2),length)
std.errors<-sqrt(var.table/n.table)
std.errors[is.na(std.errors)]<-0
biggest.value<-max(mean.table+std.errors)
bartable<-barplot(mean.table,beside=TRUE,
ylim=c(0,biggest.value+1))
errbar.width<-(max(bartable)-min(bartable))/50
for(i in 1:length(mean.table[,1])) {
for(j in 1:length(mean.table[1,])) {
lines(c(bartable[i,j],bartable[i,j]),
c(mean.table[i,j]-std.errors[i,j],
mean.table[i,j]+std.errors[i,j]))
lines(c(bartable[i,j]-errbar.width,
bartable[i,j]+errbar.width),
c(mean.table[i,j]+std.errors[i,j],
mean.table[i,j]+std.errors[i,j]))
lines(c(bartable[i,j]-errbar.width,
bartable[i,j]+errbar.width),
c(mean.table[i,j]-std.errors[i,j],
mean.table[i,j]-std.errors[i,j]))
}
}
}
any tips greatly appreciated
So apologies for my lack of clarity i'm new to R and this site so unsure how to get message across. My data is pretty straight forward and looks like this. n=3 Blocks,n=33 Lines, n=2 for Sex and my y variable with is Fitness. I've been able to make a boxplot with the function the errorbars function I posted earlier. However all i'm trying to do is add x and y axis labels to the function. Seems easy but I can't figure it out.
head(OzGLM) Block Line Sex Fitness 1 1 3 1 0.6865626 2 1 3 2 0.4874816 3 1 4 1 0.4219811 4 1 4 2 0.3829161 5 1 5 1 0.6071388 6 1 5 2 0.4432990
Upvotes: 1
Views: 300
Reputation: 240
This is how I did it, assuming I know what you want. I built on agstudy's great suggestion. I made your function into this:
error.bars<-function(Response,x1,x2,label.x,label.y)
Then I added this to agstudy's code at the bottom of your code:
}
mtext(label.y,2)
mtext(label.x,3)
}
And the finally issue the command:
error.bars(data1,data2,data3,"x-axis","y-axis")
at which point my labels were labeled with "x-axis" and "y-axis". However, I am unclear what is supposed to go in the Response, x1, and x2 places, so I got a plot of mostly garbage, except for the axis labels. :)
Upvotes: 0
Reputation: 121588
You can use mtext
to add axis labels.
For example add this to your function:
}
}
mtext('axisY',2)
mtext('axisx',3) ## since your barplot can hide the text I put the x axis label in the top
}
Upvotes: 1