Hi, I was trying to get error bars in my matplot. I looked at an earlier thread, and the sample code that I made is: #------------------ library(plotrix) mat1 <- matrix(sample(1:30,10),nrow=5,ncol=2) ses <- matrix(sample(1:3,10,replace=T),nrow=5,ncol=2) vect <- seq(20,100,20) rownames(mat1) <- rownames(ses) <- vect colnames(mat1) <- colnames(ses) <- letters[1:2] matplot(mat1,pch=c('x','o'),type = "b",lwd = 2,lty = c(1,2), col = c("green","black"),cex.main = 1.8,cex=2,cex.lab=1.5, main = "Graph 1",xlab = "Numbers 1",ylab = "Numbers 2",cex.axis = 1.6,axes=F) plotCI(rep(vect,2),mat1,ses2,pch=NA,add=T, col=rep(c("green","black"),each=nrow(mat1))) axis(1,1:5,labels = vect,cex.axis=1.5) axis(2,cex.axis=1.5) #------------------ I don't get the error bars though. If I set 'add = F' in plotCI function, then I can see the error bars, but they just can't be added to the matplot. What am I doing wrong? Is there any other way to get the error bars? thanks! [[alternative HTML version deleted]]
Hi Tim, there are a couple of problems in your example. (1) The most important is that your 'x' values for the matplot are 1:5 (that is row numbers of your mat1 matrix) and are seq(20,100,20) (that is, your vect vector) for your error bars. Error bars are thus plotted outside the plotting area ... (2) the ses2 variable is not defined in your example. I thus modified your code accordingly : library(plotrix) mat1 <- matrix(sample(1:30,10),nrow=5,ncol=2) ses <- sample(1:3,10,replace=T) vect <- seq(20,100,20) rownames(mat1) <- rownames(ses) <- vect colnames(mat1) <- colnames(ses) <- letters[1:2] # I added the vect vector as 'x' values. # No more need to define the axes # Just supressed the box around the plot with bty='n' # to fit the look of your original plot matplot(x=vect, y=mat1, pch=c('x','o'), type = "b", lwd = 2, lty = c(1,2), col = c("green","black"), main = "Graph 1", xlab = "Numbers 1", ylab = "Numbers 2", cex.main = 1.8, cex=2, cex.lab=1.5, cex.axis = 1.6, bty='n') # I changed ses2 to ses and matrices to vectors plotCI(x=rep(vect,2), y= as.vector(mat1), uiw=as.vector(ses), col=rep(c("green","black"),each=nrow(mat1)), add=T) HTH, Matthieu
Tim Smith wrote:> Hi, > > I was trying to get error bars in my matplot. I looked at an earlier thread, and the sample code that I made is: > > #------------------ > library(plotrix) > > mat1 <- matrix(sample(1:30,10),nrow=5,ncol=2) > ses <- matrix(sample(1:3,10,replace=T),nrow=5,ncol=2) > vect <- seq(20,100,20) > rownames(mat1) <- rownames(ses) <- vect > colnames(mat1) <- colnames(ses) <- letters[1:2] > > matplot(mat1,pch=c('x','o'),type = "b",lwd = 2,lty = c(1,2), > col = c("green","black"),cex.main = 1.8,cex=2,cex.lab=1.5, > main = "Graph 1",xlab = "Numbers 1",ylab = "Numbers 2",cex.axis = 1.6,axes=F) > plotCI(rep(vect,2),mat1,ses2,pch=NA,add=T, > col=rep(c("green","black"),each=nrow(mat1))) > axis(1,1:5,labels = vect,cex.axis=1.5) > axis(2,cex.axis=1.5) > > #------------------ > > I don't get the error bars though. If I set 'add = F' in plotCI function, then I can see the error bars, but they just can't be added to the matplot. What am I doing wrong? Is there any other way to get the error bars? > >Hi Tim, Try the dispersion function in plotrix to add error bars. You will probably have to add ylim=c(0,29) to your matplot command. dispersion(rep(1:5,2),mat1,ses) Jim