[R] changing the limits of a secondary y-axis in a barplot

Jim Lemon jim at bitwrit.com.au
Wed Nov 17 11:55:26 CET 2010


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



More information about the R-help mailing list