[R] Rotate x-axis label on log scale

Tim Elwell-Sutton tesutton at hku.hk
Wed Aug 25 10:47:57 CEST 2010


Yes, I do want the labels on the x-axis but the problem arises when the
y-axis is logarithmic presumably because the position labels on the x-axis
still needs to be defined in terms of x and y coordinates. 

The simplified examples below should make the problem clearer.
I can't see a way of altering the staxlab code to fix this but would be very
grateful for any suggestions.


#Example 1
#plotting on a linear scale the labels are rotated as desired
library(plotrix)
x <- y <- 1:4
labels= c('label1', 'label2', 'label3', 'label4')
plot(x,y, xaxt='n')
staxlab(side=1,at=1:4,labels=labels, srt=45)


#Example 2
#plotting on a y-axis log scale the labels don't appear
plot(x,y, xaxt='n', log='y')
staxlab(side=1,at=1:4,labels=labels, srt=45)


-----Original Message-----
From: David Winsemius [mailto:dwinsemius at comcast.net] 
Sent: Wednesday, August 25, 2010 11:46 AM
To: tesutton
Cc: 'Jim Lemon'; r-help at r-project.org
Subject: Re: [R] Rotate x-axis label on log scale


On Aug 24, 2010, at 11: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.

What part of:

"If srt is not NA, the labels will be rotated srt degrees and placed  
below the plot. This method will only place labels at the bottom."

... is unclear? You did say you wanted the rotation to be on the x- 
axis, did you not?

You could, of course, look at Lemon's code and hack it to do the y-axis:

else {
         xylim <- par("usr")
         ypos <- xylim[3] - ticklen * (xylim[4] - xylim[3])
         par(xpd = TRUE)
         text(at, ypos, labels, srt = srt, adj = 1, ...)
         par(xpd = FALSE)
     }


-- 
david.


> "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
>
> Thanks very much
> Tim
>
> #Create plot with log-scale on the y-axis
> par(mar = c(7, 4, 4, 2) + 0.1)
> plot(1, type='n', bty='n',
>        xlab="",
>        ylab='Odds Ratio',
>        xlim= c(0.5,4.5),
>        ylim= c(0.75, 2),
>        cex=2, xaxt='n', yaxt='n', cex.lab=1.3,
>        log='y')
>
> #Estimates and confidence intervals
> points(c(1:4),c(1.1,1.32,1.14,1.36), pch=17, cex=1.5, col='blue')
> segments (c(1:4),c(0.93,1.11,0.94,1.15),c(1:4),c(1.3,1.58,1.37,1.61),
>          col='blue', lwd=2)
>
> #Add x- and y-axes
> axis(1,c(1:4), labels= F)
> axis(2, at=seq(0.75,2, by=0.25), labels=seq(0.75,2, by=0.25), las=1)
>
> # Add x-axis labels
> labels <- paste("Label", 1:4, sep = " ")
> staxlab(side=1, at=1:4, labels, srt=45)
>
> -----Original Message-----
> From: Jim Lemon [mailto:jim at bitwrit.com.au]
> Sent: Tuesday, August 24, 2010 7:48 PM
> To: tesutton
> Cc: r-help at r-project.org
> Subject: Re: [R] Rotate x-axis label on log scale
>
> On 08/24/2010 11:44 AM, Tim Elwell-Sutton wrote:
>> Hi
>>
>> I'd appreciate some help with plotting odds ratios. I want to  
>> rotate the
>> labels on the x-axis by 45 degrees.
>>
>> The usual way of doing this, using text - e.g. text(1,
> par('usr')[3]-2.25..)
>> -  gives no result when the y-axis is a log scale.
>>
>> I guess this is because, as the par help says, for a logarithmic y- 
>> axis:
>> y-limits will be 10 ^ par("usr")[3:4]
>>
> Hi Tim,
> If you know where to put the labels, try:
>
> library(plotrix)
> staxlab(1,at=...,labels=...,srt=45)
>
> Jim
-- 
David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list