# Re: [R] Axis/Ticks/Scale

From: Marc Schwartz (via MN) <mschwartz_at_mn.rr.com>
Date: Fri 30 Dec 2005 - 08:57:47 EST

On Thu, 2005-12-29 at 22:06 +0100, Martin Maechler wrote:
> >>>>> "Marc" == Marc Schwartz (via MN) <mschwartz@mn.rr.com>
> >>>>> on Wed, 28 Dec 2005 15:46:37 -0600 writes:
>
> Marc> On Wed, 2005-12-28 at 20:15 +0000,
> Marc> R.C.GILL@soton.ac.uk wrote:
> >> Dear All,
> >>
> >> Apologies for this simple question and thanks in advance
> >> for any help given.
> >>
> >> Suppose I wanted to plot 1 million observations and
> >> produce the command
> >>
> >> plot(rnorm(1000000))
> >>
> >> The labels of the xaxis are 0, e+00 2 e+05 etc. These are
> >> clearly not very attractive (The plots are for a
> >> PhD. thesis).
> >>
> >> I would like the axes to be 0,2,4,6,8,10 with a *10^5 on
> >> the right hand side.
> >>
> >> Is there a simple command for this?

> >>
> >> Best Wishes
> >>
> >> Roger
>
>
> Marc> See ?plotmath for some additional examples and there
> Marc> are some others in the list archives.
>
> Yes, I think this one is there too:
> It has the "* 10^k" after each number;
> the nice thing about it is that it works for all kind of data
> -- and of course, in principle it could be built into R ...
>
>
>
> ###----------------- Do "a 10^k" labeling instead of "a e<k>" ---
>
> 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)
> }
>
>
> x <- 1e7*(-10:50)
> y <- dnorm(x, m=10e7, s=20e7)
> plot(x,y)
> ## ^^^^^^ not so nice; ok, try
>
> 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)

Nice Martin!

I do like that. I also like the handling of zero, which I realized after sending my reply, thus should have used:

x <- rnorm(1000000)
plot(x, xaxt = "n")
x.at <- seq(0, 10, 2) * 10 ^ 5

# Handle the zero here this time
x.lab <- parse(text = paste(seq(2, 10, 2), "%*% 10^5"))   axis(1, at = x.at, labels = c(0, x.lab))

BTW, on your approach, it was here:

and more recently, here:

:-)

Best regards and Happy New Year,

Marc

R-help@stat.math.ethz.ch mailing list