Dear R users, while I enjoy the built-in log argument to the plot() function, I wished it would be as easy to create more general custom transformed axes such as sqrt(), logit, etc... for example, instead of plot(x=exp(rnorm(10)), y=(1:10)^4, log = "xy"), sth. along the lines of plot(x=exp(rnorm(10)), y=(1:10)^4, trans = list(x = log, y = sqrt)) to encode the desired transfomation. This involves just transforming the xy values and creating nice tick marks at the appropriate positions. Before trying to write my own function, I wanted to see if that functionality already exists in another package ? Thanks! Markus . [[alternative HTML version deleted]]
On 5/5/2009 1:05 PM, Markus Loecher wrote:> Dear R users, > while I enjoy the built-in log argument to the plot() function, I wished it > would be as easy to create more general custom transformed axes such as > sqrt(), logit, etc... > > for example, instead of > plot(x=exp(rnorm(10)), y=(1:10)^4, log = "xy"), sth. along the lines of > plot(x=exp(rnorm(10)), y=(1:10)^4, trans = list(x = log, y = sqrt)) > to encode the desired transfomation. > > This involves just transforming the xy values and creating nice tick marks > at the appropriate positions. > Before trying to write my own function, I wanted to see if that > functionality already exists in another package ?I don't know of such a thing, but it may well exist. If you do write your own, the hardest part will be picking the nice tick marks. They should be approximately evenly spaced, but at nice round values of the original variable: that's hard to do in general. R has the pretty() function for the linear scale, and doesn't do too badly on log axes, but you'll need to work out your own rules for the sqrt or other scales. Duncan Murdoch
Markus Loecher wrote:> Dear R users, > while I enjoy the built-in log argument to the plot() function, I wished it > would be as easy to create more general custom transformed axes such as > sqrt(), logit, etc... > > for example, instead of > plot(x=exp(rnorm(10)), y=(1:10)^4, log = "xy"), sth. along the lines of > plot(x=exp(rnorm(10)), y=(1:10)^4, trans = list(x = log, y = sqrt)) > to encode the desired transfomation. > > This involves just transforming the xy values and creating nice tick marks > at the appropriate positions. > Before trying to write my own function, I wanted to see if that > functionality already exists in another package ? > >Hi Markus, The axis.mult function in plotrix does this for linear multipliers, but a more general transformation is a bit harder. I'll have a look at the log axis transformation and scratch my head a bit. Jim
Here's a Q&D bit of code I wrote up a while back. Dunno if it'll be of any use in setting up your tickmarks. # x, n same meaning as in pretty() # update: check for x neg or zero # ... is for other args to pretty() prettylog<- function(x,n=10,base=10,...) { x<-unlist(x) if (min(x)<=0) { print("Warning: nonpositive values removed") x<-x[c(x>0)] # just to avoid a single-value fed to pretty() if(length(x)==1) x<-c(1,x) } return((base^(pretty(log(x,base=base),n=n,...)))) }