[R] Logarithmic axis -- now in new version of 'sfsmisc'

Martin Maechler maechler at stat.math.ethz.ch
Wed Nov 21 14:38:54 CET 2007


>>>>> "GaGr" == Gabor Grothendieck <ggrothendieck at gmail.com>
>>>>>     on Tue, 20 Nov 2007 13:35:34 -0500 writes:

    GaGr> See:
    GaGr> http://finzi.psych.upenn.edu/R/Rhelp02a/archive/57255.html

Also, incidentally several weeks ago, I had added the following
to the 'ChangeLog' of my package  "sfsmisc" :

2007-10-13  Martin Maechler  <maechler at stat.math.ethz.ch>

	* R/prettylab.R (eaxis): new function for nice (log) axis
	labeling. (pretty10exp): drop.1: -10^k instead of -1*10^k

Here is the code -- which also needs package sfsmisc.

eaxis <- function(side, at = axTicks(side, log=log), labels = NULL, log = NULL,
                  f.smalltcl = 3/5, at.small = NULL, small.mult = NULL,
                  outer.at = TRUE, drop.1 = TRUE)
{
    ## Purpose: "E"xtended, "E"ngineer-like (log-)axis
    ## ----------------------------------------------------------------------
    ## Author: Martin Maechler, Date: 13 Oct 2007

 stopifnot(require("sfsmisc")) ## <<<--- just for the post to R-help

    is.x <- side%%2 == 1
    if(is.null(log)) {
        XY <- function(ch) paste(if (is.x) "x" else "y", ch, sep = "")
        log <- par(XY("log"))
    }
    ## use expression (i.e. plotmath) if 'log' or exponential format:
    use.expr <- log || format.info(as.numeric(at), digits=7)[3] > 0
    if(is.null(labels))
	labels <- if(use.expr) pretty10exp(at, drop.1=drop.1) else TRUE
    else if(is.na(labels)) # no 'plotmath'
	labels <- TRUE
    axis(side, at = at, labels = labels)
    if(is.null(at.small)) { ## create smart default, using small.mult
        at.small <-
            if(log) {
                if(is.null(small.mult)) small.mult <- 9
                at. <- at[log10(at) %% 1 < 1e-3] ##  the 10^k ones:
                if(length(at.))
                    outer(2:small.mult, c(if(outer.at) at.[1]/10, at.))
            } else {
                ## assumes that 'at' is equidistant
                d <- diff(at <- sort(at))
                if(any(abs(diff(d)) > 1e-3 * (dd <- mean(d))))
                    stop("'at' is not equidistant")
                if(is.null(small.mult)) {
                    ## look at 'dd' , e.g. in {5, 50, 0.05, 0.02 ..}
                    d. <- dd / 10^floor(log10(dd))
                    small.mult <- {
                        if(d. %% 5 == 0) 5
                        else if(d. %% 4 == 0) 4
                        else if(d. %% 2 == 0) 2
                        else if(d. %% 3 == 0) 3
                        else if(d. %% 0.5 == 0) 5
                        else 2 }
                }
                outer(1:(small.mult-1)/small.mult * dd,
                      c(if(outer.at) at[1]-dd, at), "+")
            }
        ##
        if(outer.at) { # make sure 'at.small' remain inside "usr"
            p.u <- sort(par("usr")[if(is.x) 1:2 else 3:4])
            if(log) p.u <- 10^p.u
            at.small <- at.small[p.u[1] <= at.small & at.small <= p.u[2]]
        }
    }
    axis(side, at = at.small, label = FALSE,
         tcl = f.smalltcl * par("tcl"))
}

A few example calls [ from  example(eaxis)  once you've got the
      	      	      new version of package 'sfsmisc' ]:

x <- lseq(1e-10, 0.1, length = 201)
plot(x, pt(x, df=3), type = "l", xaxt = "n", log = "x")
eaxis(1)

## If you like the ticks, but prefer traditional (non-"plotmath") labels:
plot(x,  gamma(x),   type = "l", log = "x")
eaxis(1, labels=NA)

x <- lseq(.001, 0.1, length = 1000)
plot(x, sin(1/x)*x, type = "l", xaxt = "n", log = "x")
eaxis(1)

## non- log-scale : draw small ticks, but no "10^k" if not needed:
x <- seq(-100, 100, length = 1000)
plot(x, sin(x)/x, type = "l", xaxt = "n")
eaxis(1)

----------------

Given the interest, I've now made sfsmisc ready for the next
release, version 0.96-01  
and uploaded that to CRAN.
Feedback is highly appreciated.

The unpatient can it also get from 
ftp://ftp.stat.math.ethz.ch/U/maechler/R/
both as source (*.tar.gz)  or Windows-binary package (*.zip).




    GaGr> On Nov 20, 2007 1:21 PM, John Wiedenhoeft <john at nurfuerspam.de> wrote:
    >> Hi there,
    >> 
    >> I guess this must be a standard issue, but I'm starting to go crazy with it. I
    >> simply want a plot with the x axis being logarithmic, having labels 1, 10,
    >> 100..., and ten unlabelled ticks between each of them - just as they
    >> introduce logarithmic axis at school. I've played around a bit with log="x",
    >> xlog=T (where exactly is the difference here?), xaxp, and xaxt (unfortunately
    >> xaxt="l" isn't implemented). The best I get is a plot with an axis having a
    >> single 100 and nothing else...
    >> 
    >> here is what I've tried:
    >> 
    >> pdf(file="kennlinien.pdf");
    >> par(log="x", xlog=TRUE);
    >> kennlinie1 <-  c(8.0746909, 3.9916973, 9.9789444, 19.962869);
    >> kennlinie2 <-  c(6.0994206, 8.9661081, 19.924883, 31.879496);
    >> reizstaerke <- c(76, 92, 108, 124);
    >> #plot(reizstaerke, kennlinie1, ylim=c(0, max(kennlinie1, kennlinie2)),
    >> xlim=c(0, max(reizstaerke)), log="x", xlog=TRUE, xaxp=c(1, 2, 1), type="b");
    >> #plot(reizstaerke, kennlinie1, type="b", log="x", xlog=TRUE, xaxp=c(1, 2, 3));
    >> plot(reizstaerke, kennlinie1, type="b",usr=c(min(reizstaerke),
    >> max(reizstaerke), min(kennlinie1, kennlinie2), max(kennlinie1, kennlinie2)),
    >> log="x", xlog=TRUE, xaxp=c(1, 2, 3));
    >> #points(reizstaerke, kennlinie2, xlog=TRUE, xaxp=c(1, 3, 3), type="b");
    >> dev.off();
    >> 
    >> Certainly I've missed something, but I can't figure it out.
    >> 
    >> Any help appreciated,
    >> Cheers,
    >> John
    >> 
    >> 
    >> 
    >> platform       i486-pc-linux-gnu
    >> arch           i486
    >> os             linux-gnu
    >> system         i486, linux-gnu
    >> status
    >> major          2
    >> minor          4.1
    >> year           2006
    >> month          12
    >> day            18
    >> svn rev        40228
    >> language       R
    >> version.string R version 2.4.1 (2006-12-18)
    >> 
    >> ______________________________________________
    >> R-help at 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.
    >> 

    GaGr> ______________________________________________
    GaGr> R-help at r-project.org mailing list
    GaGr> https://stat.ethz.ch/mailman/listinfo/r-help
    GaGr> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
    GaGr> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list