[R] disappointed with x-axes in hist and density plots

Martin Maechler maechler at stat.math.ethz.ch
Thu Jun 17 19:27:23 CEST 2004


>>>>> "TL" == Thomas Lumley <tlumley at u.washington.edu>
>>>>>     on Thu, 17 Jun 2004 09:53:33 -0700 (PDT) writes:

    TL> On Thu, 17 Jun 2004, Rishi Ganti wrote:

    >> I've got a few issues with the x-axes in the histogram
    >> and density plots.  First, often the default x-axis
    >> doesn't even extend to the length of my data. R often
    >> draws histogram bars (or density lines) farther than the
    >> drawn x-axis extends. For example, I might have a
    >> histogram bar at -15,000. But I wouldn't know that,
    >> because the most negative number on the x-axis is
    >> -10,000.  The second issue is the use of scientific
    >> notation. Yes I can read it, but I don't prefer it. Is
    >> there any way for R just to print out 1000000 and not
    >> 1e+6 on these charts?  Thanks for your help.  Rishi
    >> 

    TL> You can use the axis() function to draw axes with any set of labels you
    TL> want.

and I've recently written the following to show someone how to 
"beautify" the situation (when "1e<n>" labels appear: BTW, under
	  windows there's an etra space in there which IMO makes
	  the labels much uglier) :


###----------------- Do "a 10^k" labeling instead of "a e<k>" ---
x <- 1e7*(-10:50)
y <- dnorm(x, m=10e7, s=20e7)
plot(x,y)

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)
}

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)

----------

I hope this decreases your deception..
Further note that you can always call  box() after hist()
which may also improve the picture to your eyes.

Regards,
Martin Maechler




More information about the R-help mailing list