[R] barplot2, gap.barplot

Marc Schwartz marc_schwartz at comcast.net
Fri Mar 2 16:37:50 CET 2007


On Fri, 2007-03-02 at 08:53 -0600, hadley wickham wrote:
> > 3. Depending on the nature of your data, if the extreme value is
> > representative of an important marked difference relative to the other
> > values, then I don't particularly find the 'look' of the plot to be
> > overly problematic. It does appropriately emphasize the large
> > difference.
> >
> > On the other hand, you might want to consider using a log scale on the y
> > axis as an alternative to an axis gap. This would be a reasonable
> > approach to plotting values that have a notable difference in range.  If
> > you do this, note that you would need to ensure that all y values are >0
> > (ie. y axis range minimum, lower bounds of CI's, etc.) since:
> >
> > > log10(0)
> > [1] -Inf
> >
> >
> 
> Of course, you can't do this with a bar plot, because bars should be
> anchored at 0.

Both barplot() and barplot2() support log scaling for both x and y axes.

In both functions, the default axis minimum for the 'height' axis (y by
default, x if 'horizontal = TRUE') will be 0.9 * min(height) to avert
log10(0) related issues. Errors will be issued otherwise if any values
of 'height' are <= 0 or 'ylim'/'xlim' args are similarly set.

Using a function that Martin had posted some time ago, to nicely format
the axis labels:

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 <- 10 ^ (0:10) 
  barplot(x, log = "y", yaxt = "n")
  axis(2, at = x, labels = axTexpr(2), las = 2)
  box()
  

HTH,

Marc



More information about the R-help mailing list