[R] automatic SI prefixes as ticklabels on axis

Rui Barradas ruipbarradas at sapo.pt
Fri Jan 6 13:14:55 CET 2012

Ben's solution is much better, I was using only Jonas'  code.

There might be "typo" with large values of 'x',

> getSIstring(1e309)
[1] "Inf Y"

I don't believe this will happen, the range upper limit is much smaller.
Anyway, it can be easily solved.
(Ben's code, slightly changed.)

getSIstring <- function(x){

  lut <- rev(c(1e24,1e21,1e18,1e15,1e12,1e9,1e6,1e3,1e0,
  pre <- rev(c("Y", "Z", "E", "P", "T", "G", "M", "k", " ",
                             "m", "u", "n", "p", "f", "a", "z", "y"))

  ix <- findInterval(x, lut)

  ifelse(length(ix) > 0, sistring <- paste(x/lut[ix], pre[ix]),
                         sistring <- as.character(x))

  sistring[which(sistring ==  "Inf Y")] <- "Inf"   # make it look better


getSIstring(c(4.2e-3, 2e7))

x1 <- .Machine$double.xmax
x2 <- x1 + 10^(308 - 16)
getSIstring(c(x1, x2))

Rui Barradas

View this message in context: http://r.789695.n4.nabble.com/automatic-SI-prefixes-as-ticklabels-on-axis-tp4266141p4269182.html
Sent from the R help mailing list archive at Nabble.com.

More information about the R-help mailing list