[R] format for tick labels

Ross Ihaka ihaka at stat.auckland.ac.nz
Wed May 30 00:15:37 CEST 2001


"Martin Henry H. Stevens" wrote:
> 
> Running R 1.2.3, Windows 98
> 
> I checked the archives, and I couldn't find anything pertaining to this:
> 
> How do I control format (scientific notation versus decimal, e.g.) on tick
> labels?

                                                             3
Are you asking about seeing tick labels of the form: 1.2 x 10  ?

If so, here is a quick little function called "sci.axis" (and a couple
of examples).  There are a couple of weaknesses -- the main one being
that the handling of log scales overly simple (the strategy I used
was just to put tickmarks at every power of 10).  You can override
the default choice by using the "at" argument.

-- 
Ross Ihaka                         Email:  ihaka at stat.auckland.ac.nz
Department of Statistics           Phone:  (64-9) 373-7599 x 5054
University of Auckland             Fax:    (64-9) 373-7018
Private Bag 92019, Auckland
New Zealand
-------------- next part --------------
sci.axis <- function(side, at = NULL, labels = NULL, ...) 
{
  atvalue <-
    function(axp, log) {
      if (log)
        at <- 10^seq(log10(axp[1]), log10(axp[2]), by = 1)
      else
        at <- seq(axp[1], axp[2], length = axp[3] + 1)
    }
  apar <- par(c("xlog", "ylog", "xaxp", "yaxp"))
  if (length(at) == 0) {
    if (side == 1 || side == 3)
      at <- atvalue(apar$xaxp, apar$xlog)
    else
      at <- atvalue(apar$yaxp, apar$ylog)
  }
  if (length(labels) == 0) {
    power <- floor(log10(at))
    base <- at/10^power
    labels <- vector("expression", length(at))
    for(i in 1:length(at))
      labels[[i]] <- substitute(base %*% 10^power,
                                list(base = base[i],
                                     power = power[i]))
  }
  axis(side, at, labels = labels, ...)
}

#  Trivial examples

y <- 10^(0:7)

par(las = 0)
plot(y, axes=F, log = "y")
axis(1)
sci.axis(2)
box()

par(las = 1)
plot(y, axes=F, log = "y")
axis(1)
sci.axis(2)
box()



More information about the R-help mailing list