Sarah Vandome
2008-Oct-26 11:21 UTC
[R] Using the legend function inside a bar chart error.bars function
Hello, I am just starting out using R on my Uni course. I have been given a function to work with that adds error bars to a bar chart. I want to add a legend and have been trying to amend the code in the function error.bars (see below) so it automatically places a legend on the chart. It does this, but the legend is sitting over the top of one of my data columns. I need the function to decide where best to place the legend, depending on where the chart bar data and error bars are located. Is it possible to do this? Many thanks, Sarah # Function error.bars # This function displays a bar chart for two determining factors against # a response of measured data, such as the length of something. # It takes three arguments: Response (representing the Y coordinate data), # x1 as the first x-coordinate factor, x2 as the second x-coordinate factor. # Its initial task is to work out the mean values for the response data # and then to work out the standard errors for the data around the means. # It then plots the bar chart and adds the error bars. error.bars<-function(Response,x1,x2){ # mean calculating bit 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) # standard errors bit std.errors<-sqrt(var.table/n.table) std.errors[is.na(std.errors)]<-0 biggest.value<-max(mean.table+std.errors) # Barchart plotting bit # COMMENTED OUT CODE bartable<-barplot(mean.table, beside=TRUE, ylim=c(0,biggest.value+1), col=c("lightblue", "mistyrose")) bartable<-barplot(mean.table, beside=TRUE, ylim=c(0,biggest.value+1), col=c("lightblue", "mistyrose"), legend=rownames(mean.table)) # Error bars 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])) } # End of for loop } # End of for loop } # End of function error.bars # The function error.bars can now be run as follows: error.bars(Foot,Sex,Side) [[alternative HTML version deleted]]
Jim Lemon
2008-Oct-26 11:36 UTC
[R] Using the legend function inside a bar chart error.bars function
Sarah Vandome wrote:> Hello, > > I am just starting out using R on my Uni course. I have been given a function to work with that adds error bars to a bar chart. I want to add a legend and have been trying to amend the code in the function error.bars (see below) so it automatically places a legend on the chart. It does this, but the legend is sitting over the top of one of my data columns. I need the function to decide where best to place the legend, depending on where the chart bar data and error bars are located. Is it possible to do this? > >Hi Sarah, The "emptyspace" function in the plotrix package can find the empty space on a bar chart, but would need a little modification to avoid error bars. You could feed it the bar heights plus the error bars, but that might not be automatic enough. Jim