Phillip J. Allen
2003-Mar-25 21:48 UTC
[R] Re: Bar plot with variable width (down a drill hole) - now missing intervals?
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, How about this, which will generate a vertical barplot, a y axis from 0 down to 40 meters and bar widths and colors for each segment varied: from <- c(0, 1.2, 4.0, 4.2, 5.0, 25.0, 30.1) to <- c(1.2, 4.0, 4.2, 5.0, 25, 30.1, 36.2) intensity <- c(0, 1, 3, 2, 1, 0, 2) barplot(intensity, width = -(to - from), space = 0, horiz = TRUE, ylim = c(-40, 0)) axis(2, las = 2, labels = c(40, 30, 20, 10, 0)) mtext(side = 2, text = "Hole Depth (m)", line = 3) Hope that helps, Marc Schwartz ********************************************** It looks as though it should do: see in particular the arguments height, width, and space, to the function as described in help(barplot). For example: x0<-c(0, 1.2, 4.0, 4.2, 5.0, 25.0, 30.1) x1<-c(1.2, 4.0, 4.2, 5.0, 25.0, 30.1, 36.2) y<-c(0, 1, 3, 2, 1, 0, 2) w<-x1-x0 y[y==0]<-0.001 barplot(height=y,width=w,space=0) (the extra 0.001 gives a thickened baseline where y=0, to avoid the impression that there is no bar at such points) Ted. ****************************** original message Hi all, I am trying to make a bar plot of observations along a line or specifically a drill hole with the bars widths representing the interval of the observation and the length of the bar the actual data. My data is in the following format: from(m) to(m) Intensity of silicification 0 1.2 0 1.2 4.0 1 4.0 4.2 3 4.2 5.0 2 5.0 25 1 25.0 30.1 0 30.1 36.2 2 ...... Is the barplot() function what I really want to use? Thanks for any help. Phillip J. Allen Consulting Geochemist/Geologist Lima Peru e-mail: paallen at attglobal.net
(Ted Harding)
2003-Mar-26 09:59 UTC
[R] Re: Bar plot with variable width (down a drill hole) - n
On 25-Mar-03 Phillip J. Allen wrote:> 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.Well, I have constructed "by hand" for the above example a way of doing it. First, I have used the "add a small bit to true zeros" trick to make these stand out from the line of the axis; secondly, I have filled in the gap where there are no data by inserting the width of the gap into "width" (which I call "w"), and inserting a true zero into "intensity" (which I call "y"). This one-off code is as follows (starting with your 3 lines which define the data): 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) y<-intensity; y[y==0]<-0.005; y<-c(y[1:7],0,y[8]) w<-(-(to - from)) w<-c(w[1:7],-(45-36.2),w[8]) barplot(y, w, space = 0, horiz = TRUE, ylim = c(-50, 0)) This does produce a plot which represents the full situation: positive intensities get a proper "bar"; gaps with missing data are represented by the line of the vertical axis; intervals with intensity=0 are represented by a thickened segment of the vertical axis. However, this kind of situation needs thought about alternative ways of representing it. One possibility might be to have the vertical axis invisible, so that gaps in the data are represented by gaps in the axis (and then there would be no need for "adding a bit to true zeros"). Possibly this can be arranged by some setting of "par", though I can't seem to achieve it with barplot. Ideas, anyone? (It looks as though "axis" in barplot means the scale which is plotted below or on the side, and not the lines which bound the barplot itself). Other thoughts are based on how I might approach this plotting problem outside R. In particular, I might draw this kind of graph using the 'pic' program in troff (groff for GNU people). With this, it would be straightforward to draw the vertical axis dotted, or dashed, or blank, wherever there is an interval with missing data; otherwise, as an unbroken line. Best wishes, Ted. -------------------------------------------------------------------- E-Mail: (Ted Harding) <Ted.Harding at nessie.mcc.ac.uk> Fax-to-email: +44 (0)870 167 1972 Date: 26-Mar-03 Time: 09:59:23 ------------------------------ XFMail ------------------------------
Marc Schwartz
2003-Mar-27 02:56 UTC
[R] Re: Bar plot with variable width (down a drill hole) - now missing intervals?
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.netPhillip, 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 >
Apparently Analagous Threads
- How to center a horizontal legend with specified 'text.width'
- slow to drill into directories
- Variable Selection for data reduction and discriminant anlaysis
- Missing documentation for find_in_collection? Trying to drill down in a 4 level has_many association fails
- R, geochemistry, ternary diagrams