[R] Re: Bar plot with variable width (down a drill hole) - now missing intervals?

Marc Schwartz MSchwartz at medanalytics.com
Thu Mar 27 03:56:01 CET 2003


Phillip J. Allen wrote:
> Hi again,
> 
> Thanks Ted and Marc its works.  But of course after pulling in in some 
> real life data I discoverd one hitch.  Often there are missing 
> intervals.  For example:
> from <- c(0, 1.2, 4.0, 4.2, 5.0, 25.0, 30.1, 45)
> to <- c(1.2, 4.0, 4.2, 5.0, 25, 30.1, 36.2, 50)
> intensity <- c(0, 1, 3, 2, 1, 0, 2, 5)
> barplot(intensity, width = -(to - from),
>        space = 0, horiz = TRUE, ylim = c(-40, 0))
> 
> And it appears a barplot() is just stacking bars one on top the other 
> and the help page doesn't indicate anyway to position the bar along the 
> y-axis.  So does anyone have a nifty trick to to fill in blank gaps?  At 
> least my database can check for "overlaping" intervals before I get the 
> data into R.
> 
> Thanks,
> 
> Phillip J. Allen
> Consulting Geochemist/Geologist
> Lima Peru
> e-mail: paallen at attglobal.net


Phillip,

Sorry for the delay in replying to this e-mail. I was busy much of the 
day catching up on a project for a client and finally had a chance for a 
breather this evening.

I took note of Ted's reply using NA in the intensity vector which is the 
right approach. I thought you might want an automated approach to 
dealing with missing intervals from dataset to dataset. The code below 
should work, with the assumption that at least the first interval is OK 
and should handle multiple missing intervals in one set. I added a 
second missing interval in your example above to the code below. You 
should test this a bit more to be sure.

What this does is to scan the vectors for a break in the sequence and 
inserts (using insert() )the missing interval(s) with an NA value.

Note that a "0" interval value will have a line in the interval, whereas 
a missing interval with an NA will show nothing.

Hope that this helps.

Marc Schwartz

------------------------

from <- c(0, 1.2, 4.0, 4.2,  5.0, 25.0, 30.1, 45, 55)
to <-   c(1.2, 4.0, 4.2, 5.0, 25, 30.1, 36.2, 50, 63)
intensity <- c(0, 1, 3, 2, 1, 0, 2, 5, 7)

insert <- function(old.vec, pos, val)
{
   left <- old.vec[1:pos]
   right <- old.vec[-(1:pos)]

   new.vec <- c(left, val, right)

   invisible(new.vec)
}

i <- 1

repeat
{
   if (to[i] != from[i + 1])
   {
     from <- insert(from, i, to[i])
     to <- insert(to, i, from[i + 2])
     intensity <- insert(intensity, i, NA)

     i <- i + 1
   }

   i <- i + 1

   if (i >= length(from)) break
}

barplot(intensity, width = -(to - from),
         space = 0, horiz = TRUE,
         ylab = "Depth (m)", ylim = c(-63, 0))

barplot(intensity, width = -(to - from),
         space = 0, horiz = TRUE,
         ylab = "Depth (m)", ylim = c(-63, 0))

axis(2, labels = seq(60, 0, -10), las = 2)
box()

 > from
  [1]  0.0  1.2  4.0  4.2  5.0 25.0 30.1 36.2 45.0 50.0 55.0
 > to
  [1]  1.2  4.0  4.2  5.0 25.0 30.1 36.2 45.0 50.0 55.0 63.0
 > intensity
  [1]  0  1  3  2  1  0  2 NA  5 NA  7
 >



More information about the R-help mailing list