Anna.Richards at csiro.au
2010-Nov-17 04:06 UTC
[R] changing the limits of a secondary y-axis in a barplot
Hi, I hope this is a simple question. I am having trouble changing the scale of a secondary y-axis on a barplot. When I run the code below the limits set for the first axis are always applied to the second axis as well. I am using the latest R version 2.12.0. For example, if I have 3 vectors (Y1, Y2, Y3), and Y1 and Y2 need to be plotted on the left axis and Y3 on the right axis: Y1<-c(7,10) Y2<-c(8,11) Y3<-c(1,2.5) par(mfrow=c(1,1), mai=c(1.0,1.0,1.0,1.0)) Plot1<-barplot(rbind(Y1,Y2), beside=T, axes=T, names.arg=c("a","b"), ylim=c(0,15), xlim=c(1,9), space=c(0,1), col=c("darkgray","white")) Plot2<-barplot(Y3, add=T, beside=T, names.arg="c", col=c("darkgray","white"), ylim=c(0,5), space=c(0,7), width=1) axis(side=4) How can I change the barplot so that the left hand axis scales from 0 to 15 and the right hand axis from 0 to 5? Thank you, Anna
Mark Difford
2010-Nov-17 08:31 UTC
[R] changing the limits of a secondary y-axis in a barplot
Hi Anna,>> How can I change the barplot so that the left hand axis scales from 0 to >> 15 and the right hand >> axis from 0 to 5?Try this: par(mfrow=c(1,1), mai=c(1.0,1.0,1.0,1.0)) Plot1<-barplot(rbind(Y1,Y2), beside=T, axes=T, names.arg=c("a","b"), ylim=c(0,15), xlim=c(1,9), space=c(0,1), col=c("darkgray","white"), yaxt="n") Plot2<-barplot(Y3, add=T, beside=T, names.arg="c", col=c("darkgray","white"), ylim=c(0,5), space=c(0,7), width=1, yaxt="n") axis(side=2, at=seq(0,15,3), labels=seq(0,15,3)) axis(side=4, at=seq(0,15,3), labels=seq(0,5,1)) Regards, Mark. -- View this message in context: http://r.789695.n4.nabble.com/changing-the-limits-of-a-secondary-y-axis-in-a-barplot-tp3046117p3046283.html Sent from the R help mailing list archive at Nabble.com.
Jim Lemon
2010-Nov-17 10:55 UTC
[R] changing the limits of a secondary y-axis in a barplot
On 11/17/2010 03:06 PM, Anna.Richards at csiro.au wrote:> Hi, > > I hope this is a simple question. I am having trouble changing the scale of a secondary y-axis on a barplot. When I run the code below the limits set for the first axis are always applied to the second axis as well. I am using the latest R version 2.12.0. > > > For example, if I have 3 vectors (Y1, Y2, Y3), and Y1 and Y2 need to be plotted on the left axis and Y3 on the right axis: > Y1<-c(7,10) > Y2<-c(8,11) > Y3<-c(1,2.5) > > par(mfrow=c(1,1), mai=c(1.0,1.0,1.0,1.0)) > Plot1<-barplot(rbind(Y1,Y2), beside=T, axes=T, names.arg=c("a","b"), > ylim=c(0,15), xlim=c(1,9), space=c(0,1), col=c("darkgray","white")) > Plot2<-barplot(Y3, add=T, beside=T, names.arg="c", > col=c("darkgray","white"), ylim=c(0,5), space=c(0,7), width=1) > axis(side=4) > > How can I change the barplot so that the left hand axis scales from 0 to 15 and the right hand axis from 0 to 5? >Hi Anna, Have a look at the twoord.plot function in the plotrix package. This does exactly what you describe above. Jim