s-news
[Top] [All Lists]

Bar chart breaks - solution

To: s-news@lists.biostat.wustl.edu
Subject: Bar chart breaks - solution
From: "Jim Daley" <jgdaley@gw.dec.state.ny.us>
Date: Mon, 4 Feb 2008 08:16:03 -0500
A while back I posted the following question:
 
I am running S-PLUS 8.0 and have a bar chart question.  Most of my bars are under a certain height, say 20, but one goes all the way up to 70.  I would like to have the Y-axis stop at 20 so that most of the bars are not compressed.  Is there a way to break the one tall bar to indicate that it goes off the scale?  The attached image shows what I am trying to do.  Any ideas out there?
 
Bill Dunlap at Insightful provided the following solution, which worked for me.  Being new to SPlus programming, I added some comments to the code to keep track of what was going on.  Thanks Bill.
 
******************************
 
x<-c(21,23,25,21,76,79,10)
my.barplot(x, ylim=c(0,30))
 
my.barplot <- function(x, ..., ylim = c(0,max(x))) {
 b <- barplot(pmin(x,ylim[2]) , ..., ylim = ylim)
 #pmin above truncates the higher values, in this case to 30
 big <- x > ylim[2] #Gives a list of T & F values, where T = bigger than 30
 if(any(big)) {
  # label tops of truncated bars
  # b contains the coordinates of the center of the x-axis of the bars.
  # The first two arguments below are the x and y coordinates for the text.
  # par("usr") gives the max and min coords for the plot, for the x & y axis; [4] is the y max
  # par("cxy") gives the width and height if characters; [2] is the height
  # xpd = True allow stuff to be drawn outside the plot
  text(b[big], rep(par("usr")[4], sum(b)) + par("cxy")[2], as.character(x[big]), xpd = TRUE)
  # overwrite truncated bars with parallelogram
  # with given half height and half width.
  halfheight <- par("cxy")[2] * 0.7
  halfwidth <- (b[2] - b[1])/2 * 0.95 # if 1 bar, use mean(par("usr")[1:2])*.8?
  ybreak <- 0.8 * ylim[2]
  for(i in which(big)) {
   bi <- b[i]
   polygon(col = par("bg"), border=FALSE, x = c(bi - halfwidth, bi + halfwidth, bi +
    halfwidth, bi - halfwidth), y = c(ybreak - halfheight,
    ybreak, ybreak + 2 * halfheight, ybreak + halfheight))
   segments(c(bi - halfwidth, bi - halfwidth), c(ybreak - halfheight,
    ybreak + halfheight), c(bi + halfwidth, bi + halfwidth),
    c(ybreak, ybreak + 2 * halfheight))
  }
 }
}
 
******************************
 
Jim Daley
Coldwater Fisheries Unit Leader
NYSDEC Bureau of Fisheries
625 Broadway, 5th Floor
Albany, NY 12233-4753
jgdaley@gw.dec.state.ny.us
 
 
 
 
<Prev in Thread] Current Thread [Next in Thread>
  • Bar chart breaks - solution, Jim Daley <=