Re: [R] Rotate x-axis label on log scale
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:dwinsem...@comcast.net] Sent: Wednesday, August 25, 2010 11:46 AM To: tesutton Cc: 'Jim Lemon'; r-help@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:j...@bitwrit.com.au] Sent: Tuesday, August 24, 2010 7:48 PM To: tesutton Cc: r-help@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 __ R-help@r-project.org 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.
Re: [R] Rotate x-axis label on log scale
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 __ R-help@r-project.org 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.
Re: [R] Rotate x-axis label on log scale
Hi Jim Thanks so much for this. The updated staxlab function works perfectly! Tim -Original Message- From: Jim Lemon [mailto:j...@bitwrit.com.au] Sent: Wednesday, August 25, 2010 6:36 PM To: tesutton Cc: r-help@r-project.org Subject: Re: [R] Rotate x-axis label on log scale 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 __ R-help@r-project.org 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.
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 __ R-help@r-project.org 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.
Re: [R] Rotate x-axis label on log scale
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 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:j...@bitwrit.com.au] Sent: Tuesday, August 24, 2010 7:48 PM To: tesutton Cc: r-help@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 __ R-help@r-project.org 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.
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:j...@bitwrit.com.au] Sent: Tuesday, August 24, 2010 7:48 PM To: tesutton Cc: r-help@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 __ R-help@r-project.org 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.