1

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

eoin.duff
  • 37
  • 2
  • 8
  • 2
    I'm sorry, but do you think anybody will try do understand such a code block without any commenting or indention? I think this would be better done with `ggplot2`. Please [provide some data](http://stackoverflow.com/a/5963610/1412059) with your question. – Roland May 27 '13 at 12:16

2 Answers2

1

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
}
agstudy
  • 119,832
  • 17
  • 199
  • 261
  • Thanks for your comment i've tried to add the code you suggested am I correct in adding it in as you suggested and just sub in my labels at the end of the function for example ..+ c(mean.table[i,j]-std.errors[i,j], + mean.table[i,j]-std.errors[i,j], + ))}},mtext(axisY,2)mtext(axisx,3} such that I sub in my label for x and y or am I leaving something really obvious out here? Thanks again – eoin.duff May 27 '13 at 19:23
0

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. :)

jeramy townsley
  • 240
  • 3
  • 18
  • Great thanks very much I was re looking at this code this week and used what you suggested @jeramy townsley – eoin.duff Sep 06 '14 at 13:35