[R] ylab on secondary y axis

Marc Schwartz MSchwartz at mn.rr.com
Thu Jun 9 18:48:44 CEST 2005


On Thu, 2005-06-09 at 09:26 -0700, Hai Lin wrote:
> Dear R users:
> 
> I am trying to add a label on axis(4) but I don't find
> any reference in R archives. Could you please help me
> out?
> 
> Thanks in advance.
> 
> The followings are the data set and what I have done:
> 
> Sample data:
> 
> time	 y1 y2
> 2	9 24.396
> 4	7.667 19.082	
> 10	4.667 73.984
> 
> Commands:
> plot(time,y1,type='l',col='red',ylab="Relative
> levels", xlab="Age of Mice",ylim=c(0,20))
> points(time,y1 col="red", pch=19)
> 
> op <-par(new=T)
> plot(time,y2,type='l',
> col='green',axes=F,xlab="",ylab="", ylim=c(0,80))
> points(time,y2,col='green',pch=24)
> axis(4)

There are two options:

First, use:

par(mar = c(5, 4, 4, 5))

before your first plot to increase the margin on the right hand side for
the second Y axis label. You can further adjust these as you may
require. See ?par for more information.


Then, after the second plot:

1. Use mtext() as follows:

  mtext(4, text = "This is Y Axis 2", line = 3)

Note however that the text is facing outward. Adjust the 'line' argument
to move the text in or out as you need.



2. Use text() as follows:

  text(11.5, 40, "This is Y Axis 2", srt = 270, xpd = TRUE)

This enables you to place the text outside the plot region (xpd = TRUE)
and rotate it so that it is facing inward. Adjust the x and y positions
as you need.

See ?mtext and ?text for more information.

HTH,

Marc Schwartz




More information about the R-help mailing list