[R] Rotate x-axis label on log scale

Jim Lemon jim at bitwrit.com.au
Wed Aug 25 12:35:51 CEST 2010


On 08/25/2010 01:37 PM, Tim Elwell-Sutton wrote:
> Hi Jim
> Thanks for this. The staxlab function seems very useful. Unfortunately, the
> rotation option doesn't seem to work for me when the y-axis is on a log
> scale. It will stagger the labels but not rotate them. There's no error
> message. On a linear axis the rotation works nicely. Any ideas?
> The example below works if you omit log='y' or srt=45
>
Hi Tim,
Thanks for letting me know about this problem. I never did get around to 
making staxlab work with log axes, but I think this new version:

staxlab<-function(side=1,at,labels,nlines=2,top.line=0.5,
  line.spacing=0.8,srt=NA,ticklen=0.03,...) {

  if(missing(labels)) labels<-at
  nlabels<-length(labels)
  if(missing(at)) at<-1:nlabels
  if(is.na(srt)) {
   linepos<-rep(top.line,nlines)
   for(i in 2:nlines) linepos[i]<-linepos[i-1]+line.spacing
   linepos<-rep(linepos,ceiling(nlabels/nlines))[1:nlabels]
   axis(side=side,at=at,labels=rep("",nlabels))
   mtext(text=labels,side=side,line=linepos,at=at,...)
  }
  else {
   xylim<-par("usr")
   if(side == 1) {
    xpos<-at
    if(par("ylog")) ypos<-10^(xylim[3]-ticklen*(xylim[4]-xylim[3]))
    else ypos<-xylim[3]-ticklen*(xylim[4]-xylim[3])
   }
   else {
    ypos<-at
    if(par("xlog")) xpos<-10^(xylim[1]-ticklen*(xylim[2]-xylim[1]))
    else xpos<-xylim[1]-ticklen*(xylim[2]-xylim[1])
   }
   par(xpd=TRUE)
   text(xpos,ypos,labels,srt=srt,adj=1,...)
   par(xpd=FALSE)
  }
}

will do what you want.

Jim



More information about the R-help mailing list