Hi, I have measured two response variables (y1, y2) at each treatment level (x = 0, 1.5 or 3). Now I would like to show the y1 and y2 against x in a bar plot. However, y1 and y2 differ in scale so I need two y-axises, one on the left side and one on the right side (and I dont want to standardize my responses). This is fairly easy if you want to show points,lines etc, but gets more complicated with bars.Although these kind of bar graphs are quite common, I have found very limited information about how to do them in R. I have been struggling with the barplot() command. My problem is that the bars for y1 and y2 end up at the same place (blocking each other) and not beside each other when I use par(new=TRUE). Is there a way to separate them so y1 and y2 are placed beside each other at each x level, or is this easier to do this with lattice?? I would also like to add error bars but I guess that should not be a problem. For code and data, see below. Cheers, Gustaf Granath, phd student My code so far: #Creating data c(6.34,13.38,17.87)->y1 c(0.85,1.88,2.33)->y2 c(0,1.5,3)->x cbind(y1,y2,x)->mydata data.frame(mydata)->mydata with(mydata, tapply(y1,x,mean))->mean.y1 with(mydata, tapply(y2,x,mean))->mean.y2 #Barplot par(mar=c(5,4,4,4)+0,1) barplot(mean.y1,density=1,las=1) par(new=T) barplot(mean.y2,density=3,angle=3,axes=F) axis(4,las=1)
On Jan 29, 2008 10:15 AM, Gustaf Granath <Gustaf.Granath at ebc.uu.se> wrote:> Hi, > I have measured two response variables (y1, y2) at each treatment level > (x = 0, 1.5 or 3). Now I would like to show the y1 and y2 against x in a > bar plot. However, y1 and y2 differ in scale so I need two y-axises, one > on the left side and one on the right side (and I dont want to > standardize my responses). This is fairly easy if you want to show > points,lines etc, but gets more complicated with bars.Although these > kind of bar graphs are quite common, I have found very limited > information about how to do them in R. I have been struggling with the > barplot() command. My problem is that the bars for y1 and y2 end up at > the same place (blocking each other) and not beside each other when I > use par(new=TRUE). Is there a way to separate them so y1 and y2 are > placed beside each other at each x level, or is this easier to do this > with lattice?? I would also like to add error bars but I guess that > should not be a problem.Why don't you just use two separate plots? Using double y-axes is only desirable if one axis is a transformation of the other, or you are deliberately trying to confuse the data. If you want to learn more about why it's a bad idea, trying start with: K. W. Haemer. Double scales are dangerous. The American Statistician, 2(3):24?24, 1948. You'll note that people have been advising against them for over 50 years! Hadley -- http://had.co.nz/
Try this X = rbind(y1, y2) colnames(X) = as.character(x) barplot( X, density=c(35,40), col=c("red","blue"), beside=FALSE) barplot( X, density=c(35,40), col=c("red","blue"), beside=TRUE) Alternatively, barplot( y1, ylim = c(0, max(y1,y2)), col="blue", beside=FALSE) par(new=TRUE) barplot( y2, ylim = c(0, max(y1,y2)), col="red", beside=FALSE, names.arg=as.character(x)) Mama ----- Mama Attiglah, PhD Advanced Research Center Quantitative Research Analyst State Street Bank +44(0)20 7698 6290 (Direct Line) +44 (0)207 004 2968 (Direct Fax) Please visit our Web site at www.ssga.com -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Gustaf Granath Sent: 29 January 2008 16:15 To: r-help at r-project.org Subject: [R] How to get two y-axises in a bar plot? Hi, I have measured two response variables (y1, y2) at each treatment level (x = 0, 1.5 or 3). Now I would like to show the y1 and y2 against x in a bar plot. However, y1 and y2 differ in scale so I need two y-axises, one on the left side and one on the right side (and I dont want to standardize my responses). This is fairly easy if you want to show points,lines etc, but gets more complicated with bars.Although these kind of bar graphs are quite common, I have found very limited information about how to do them in R. I have been struggling with the barplot() command. My problem is that the bars for y1 and y2 end up at the same place (blocking each other) and not beside each other when I use par(new=TRUE). Is there a way to separate them so y1 and y2 are placed beside each other at each x level, or is this easier to do this with lattice?? I would also like to add error bars but I guess that should not be a problem. For code and data, see below. Cheers, Gustaf Granath, phd student My code so far: #Creating data c(6.34,13.38,17.87)->y1 c(0.85,1.88,2.33)->y2 c(0,1.5,3)->x cbind(y1,y2,x)->mydata data.frame(mydata)->mydata with(mydata, tapply(y1,x,mean))->mean.y1 with(mydata, tapply(y2,x,mean))->mean.y2 #Barplot par(mar=c(5,4,4,4)+0,1) barplot(mean.y1,density=1,las=1) par(new=T) barplot(mean.y2,density=3,angle=3,axes=F) axis(4,las=1) ______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Gustaf Granath wrote:> Hi, > I have measured two response variables (y1, y2) at each treatment level > (x = 0, 1.5 or 3). Now I would like to show the y1 and y2 against x in a > bar plot. However, y1 and y2 differ in scale so I need two y-axises, one > on the left side and one on the right side (and I dont want to > standardize my responses). This is fairly easy if you want to show > points,lines etc, but gets more complicated with bars.Although these > kind of bar graphs are quite common, I have found very limited > information about how to do them in R. I have been struggling with the > barplot() command. My problem is that the bars for y1 and y2 end up at > the same place (blocking each other) and not beside each other when I > use par(new=TRUE). Is there a way to separate them so y1 and y2 are > placed beside each other at each x level, or is this easier to do this > with lattice?? I would also like to add error bars but I guess that > should not be a problem. >Hi Gustaf, You can get a reasonable display like this: par(mar=c(5,4,4,4)) barp(rbind(y1,y2),names.arg=c(0,1.5,3),col=2:3, legend.lab=c("y1","y2")) If you really want to have two y axes, you could add: axis(4) If you want to inflate your y2 values so that they look comparable to the y1 values, you could do this: barp(rbind(y1,y2*5),names.arg=c(0,1.5,3),col=2:3, legend.lab=c("y1","y2")) axis(2,col=2) axis(4,at=seq(0,15,by=3),labels=0:5,col=3) but as others have mentioned, it is not a very good idea. Jim