Hi im trying to add error bars to my barplots, there very basic, i have a few grapghs where the y variable is different but on all the X variable is Age (Adult and Juvenile) however this is split into two levels so i have males and females, so my graph basically has four bars on it. I know how to add eror bars for instance when there is only one level eg lookng at the diffrence between male and female lizards and tree height and have used the code: error.bars<-function(yv,z,nn) { xv<- barplot(yv,ylim=c(0,(max(yv)+max(z))),names=nn,ylab=deparse(substitute(yv) )) g=(max(xv)-min(xv))/50 for (i in 1:length(xv)) { lines(c(xv[i],xv[i]),c(yv[i]+z[i],yv[i]-z[i])) lines(c(xv[i]-g,xv[i]+g),c(yv[i]+z[i], yv[i]+z[i])) lines(c(xv[i]-g,xv[i]+g),c(yv[i]-z[i], yv[i]-z[i])) }} Have then worked out the standard errors and added that using: se<-c() and then added the final bit of code: labels<-as.character(levels(Sex2)) ybar<-as.vector(tapply(Height2,Sex2,mean)) error.bars(ybar,se,labels) So i was wondering, in order to get error bars on my barplot with two levels do i need to change the last bit of code. For example im looking at mean body condition (y) against age (x) and sex (level), i tried to change the code to the following but it didnt work, any suggestions? labels<-as.character(levels(list(sex,Age))) ybar<-as.vector(tapply(ConditionIndex,sex,Age,mean)) error.bars(ybar,se,labels) [[alternative HTML version deleted]]
Brown, Heidi wrote:> Hi im trying to add error bars to my barplots, there very basic, i have a few grapghs where the y variable is different but on all the X variable is Age (Adult and Juvenile) however this is split into two levels so i have males and females, so my graph basically has four bars on it. > > I know how to add eror bars for instance when there is only one level eg lookng at the diffrence between male and female lizards and tree height and have used the code: > > error.bars<-function(yv,z,nn) { > xv<- > barplot(yv,ylim=c(0,(max(yv)+max(z))),names=nn,ylab=deparse(substitute(yv) > )) > g=(max(xv)-min(xv))/50 > for (i in 1:length(xv)) { > lines(c(xv[i],xv[i]),c(yv[i]+z[i],yv[i]-z[i])) > lines(c(xv[i]-g,xv[i]+g),c(yv[i]+z[i], yv[i]+z[i])) > lines(c(xv[i]-g,xv[i]+g),c(yv[i]-z[i], yv[i]-z[i])) > }} > > Have then worked out the standard errors and added that using: > > se<-c() > > and then added the final bit of code: > > labels<-as.character(levels(Sex2)) > ybar<-as.vector(tapply(Height2,Sex2,mean)) > error.bars(ybar,se,labels) > > So i was wondering, in order to get error bars on my barplot with two levels do i need to change the last bit of code. For example im looking at mean body condition (y) against age (x) and sex (level), i tried to change the code to the following but it didnt work, any suggestions? > > labels<-as.character(levels(list(sex,Age))) > ybar<-as.vector(tapply(ConditionIndex,sex,Age,mean)) > error.bars(ybar,se,labels) >Hi Heidi, I think you may be having trouble with the x positions of the error bars. barplot returns these positions, so if you prepend "xpos<-" to the barplot call, it will give you the positions for your error bars. Jim
Brown, Heidi wrote:> Hi im trying to add error bars to my barplots, there very basic, i have a few grapghs where the y variable is different but on all the X variable is Age (Adult and Juvenile) however this is split into two levels so i have males and females, so my graph basically has four bars on it. > > I know how to add eror bars for instance when there is only one level eg lookng at the diffrence between male and female lizards and tree height and have used the code: > > error.bars<-function(yv,z,nn) { > xv<- > barplot(yv,ylim=c(0,(max(yv)+max(z))),names=nn,ylab=deparse(substitute(yv) > )) > g=(max(xv)-min(xv))/50 > for (i in 1:length(xv)) { > lines(c(xv[i],xv[i]),c(yv[i]+z[i],yv[i]-z[i])) > lines(c(xv[i]-g,xv[i]+g),c(yv[i]+z[i], yv[i]+z[i])) > lines(c(xv[i]-g,xv[i]+g),c(yv[i]-z[i], yv[i]-z[i])) > }} > > Have then worked out the standard errors and added that using: > > se<-c() > > and then added the final bit of code: > > labels<-as.character(levels(Sex2)) > ybar<-as.vector(tapply(Height2,Sex2,mean)) > error.bars(ybar,se,labels) > > So i was wondering, in order to get error bars on my barplot with two levels do i need to change the last bit of code. For example im looking at mean body condition (y) against age (x) and sex (level), i tried to change the code to the following but it didnt work, any suggestions? > > labels<-as.character(levels(list(sex,Age))) > ybar<-as.vector(tapply(ConditionIndex,sex,Age,mean)) > error.bars(ybar,se,labels)See http://biostat.mc.vanderbilt.edu/DynamitePlots for many reasons not to use dynamite plots. Frank -- Frank E Harrell Jr Professor and Chair School of Medicine Department of Biostatistics Vanderbilt University