[R] How can I fit the secondary y axis legend on my graph?

Gabor Grothendieck ggrothendieck at gmail.com
Mon Sep 4 04:47:31 CEST 2006

There is an example here:


(Scroll down until you see the graphic with the two y axes.  The
code is in the box just above it.)

On 9/3/06, Chris Grobler <bjorn.grobler at zoo.ox.ac.uk> wrote:
> Dear All,
>   Having a bit of trouble with plotting two y variables on the same
> graph. I cannot manage to get the secondary y axis label on to the right
> of the axis - it gets plotted beyond the graphic window I assume?! The
> way I constructed the graph is thus:
> plot(data[,3],data[,2],axes=F, type="b")     ## plots my data from two
> data colums without axes - fine
> axis(1, at=data[,3])                                     ## adds my
> primary axis - fine
> mtext("Year", side=1, line=2)                     ## Adds the title
> "Year" - fine
> axis(2)
> mtext("Index of population size", side=2, line=2)    ## Puts the primary
> y axis on and names it correctly
> points(data[,3],data[,1],pch=16)                 ## puts my secondary y
> points on - also fine
> axis(4)
> mtext("Number of sun hours in April", side=4, line=2) ## if I enter
> "line=1" it writes over my inex numbers but if I put
>        "line=2" it disappears.
> I assume that perhaps the graph automatically fills the space and does
> not leave any for 'odd' axes. My questions therefore are: how can I get
> the legend on that axis, how do I remove ylab and xlab to free the space
> that they take (as they spaced by default at "line=3" and thus waste
> space - I would like to set them to "line=2") and perhaps how do I
> scrunch up the x-axis so that it leaves more space at either side?
> Sorry for the ignorance!
> Chris
> ______________________________________________
> R-help at stat.math.ethz.ch 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.

More information about the R-help mailing list