[R] barplot and missing values?

Marc Schwartz MSchwartz at mn.rr.com
Sat Jun 4 16:19:53 CEST 2005


On Sat, 2005-06-04 at 14:50 +0100, Dan Bolser wrote:

<snip>

> This must be because of the "log='y'" option that I am using here.
> 
> y <- c(2,4,6,8,NA,NA,NA,NA,18)
> 
> barplot2(y,log='y')
> 
> Above fails.
> 
> 
> I appreciate that what I am trying to do is somewhat artificial (handle
> zero values on a log scale), but it does reflect the data I have.
> 
> I tried plot(..., type='h'), but that dosn't do the "beside=T" stuff that
> I want to do.
> 
> I am now trying things like...
> 
> barplot2(
>   dat.y.plot + 0.11, # Dirty hack
>   offset=-0.1,       #
>   xpd=F,             #
>   log='y',
>   beside=T
> )
> 
> Which looks messy. 
> 
> Any way to cleanly handle NA values with barplot2 on a log scale
> (log='y')?

<snip>

Dan,

You are actually close in the above example, using the 'offset'
argument.

In this case, you still cannot use "NA"s, since their value is unknown
and so must set these elements to zero. Then using a small offset value,
you can adjust the base value of the y axis so that it is "just above"
zero. This should result in a minimal shift of the bar values above
their actual values and should not materially affect the plot's
representation of the data.

Something like the following "should" work:

  > y <- c(2, 4, 6, 8, NA, NA, NA, NA, 18)
  > y
  [1]  2  4  6  8 NA NA NA NA 18
   
  > y[is.na(y)] <- 0
  > y
  [1]  2  4  6  8  0  0  0  0 18


  barplot2(y, log = "y", offset = 0.01, las = 2)

Note also that if you follow the above with:

  box()

The residual bars from the (0 + 0.01) values are covered with the plot
region box, if that is an issue for you.

This is still something of a "hack", but it is a little cleaner. The key
of course is to avoid the use of a bar value of log(x), where x <= 0.
Selecting the proper offset value based upon your actual data is
important so as to minimally affect the values visually.

HTH,

Marc Schwartz




More information about the R-help mailing list