[R] "10^k" axis labels {was ".. (log scale on y-axis)"}

Gabor Grothendieck ggrothendieck at gmail.com
Fri Jul 1 15:23:45 CEST 2005


On 7/1/05, Martin Maechler <maechler at stat.math.ethz.ch> wrote:
> >>>>> "Gabor" == Gabor Grothendieck <ggrothendieck at gmail.com>
> >>>>>     on Thu, 30 Jun 2005 07:28:30 -0400 writes:
> 
>    Gabor> On 6/29/05, Jing Shen <jshen6 at gmail.com> wrote:
> 
>    >> I am planning to plot my data on log scale (y-axis). There is a
>    >> parameter in plot function, which is
>    >> plot( ..., log="y", ...)
>    >> While, the problem is that it is with base of e. Is there a way to let
>    >> me change it to 10 instead of e?
>    >>
> 
>    Gabor> Is your question how to get the axis labels to be powers of 10?
>    Gabor> In that case,
> 
>    Gabor> plot(1:100, log = "y", yaxt = "n")  # do not show y axis
>    Gabor> axis(2, c(1,10,100))  # draw y axis with required labels
> 
> and if you're there, you might be interested in the following
> which provides a somewhat automated way to show
> "a * 10 ^ k" tick-labels instead of the scientific "a e k" ones.
> { For some time, I had wanted that something like this could
>  become an easy option for builtin axis(*), but then I also know
>  that we should rather strive to build future-proof tools, which
>  "hence" should we applicable to 'grid' as well as to old-style
>  'graphics'  and all this got me stuck in the process ...
> }
> 
> Martin Maechler, ETH Zurich
> 
> ---------------
> 
> axTexpr <- function(side, at = axTicks(side, axp=axp, usr=usr, log=log),
>                    axp = NULL, usr = NULL, log = NULL)
> {
>    ## Purpose: Do "a 10^k" labeling instead of "a e<k>"
>    ##        this auxiliary should return 'at' and 'label' (expression)
>    ## ----------------------------------------------------------------------
>    ## Arguments: as for axTicks()
>    ## ----------------------------------------------------------------------
>    ## Author: Martin Maechler, Date:  7 May 2004, 18:01
>    eT <- floor(log10(abs(at)))# at == 0 case is dealt with below
>    mT <- at / 10^eT
>    ss <- lapply(seq(along = at),
>                 function(i) if(at[i] == 0) quote(0) else
>                 substitute(A %*% 10^E, list(A=mT[i], E=eT[i])))
>    do.call("expression", ss)
> }
> 
> par(mar=.1+c(5,5,4,1))## << For the horizontal y-axis labels, need more space
> plot(x,y, axes= FALSE, frame=TRUE)
> aX <- axTicks(1); axis(1, at=aX, label= axTexpr(1, aX))
> if(FALSE) # rather the next one
> { aY <- axTicks(2); axis(2, at=aY, label= axTexpr(2, aY))}
> ## or rather (horizontal labels on y-axis):
> aY <- axTicks(2); axis(2, at=aY, label= axTexpr(2, aY), las=2)

This may not be as good as what you have (although its arguably
prettier in the specific example below) and may suffice in many,
though possibly not all, cases -- I mention it since its very simple
and, in fact, requires no auxilliary routines.  It uses your idea of 
employing axTicks.  The key trick is to use axTicks twice in axis:

 x <- 10 ^ seq(-2,10) # test data

plot(x, log = "y", yaxt = "n")
axis(2, axTicks(2), axTicks(2))




More information about the R-help mailing list