# [R] barplot with varaible-width bars

Gould, A. Lawrence larry_gould at merck.com
Mon Jan 31 20:42:28 CET 2011

```Bill Pikounis provided a clever and elegant solution:  in the program barplot.default, replace the statement

width <- rep(width, NR)

that occours around line 51 ( NR = nrow(height) )  with the statement   width <- width.  I renamed the program

barplotX.fn

and attached it to this email.  The attachment also includes a function called   mulbarX.fn  that mimics the
behavior of the original SPlus function    mulbar.  The call

mulbarX.fn(yy[,2*1:5],yy[,2*1:5-1],xlab="Baseline Category",ylab="Incidence",main="Main Title",sub="Low Volume",legendtxt=c("P","F"),ylim=c(0,0.15),
+ categlabs=c("Stratum 1","Stratum 2","Stratum 3","Stratum 4","Stratum 5"),legendinset=0.1,labcex=1.2)

produces a result that is very close to what the SPlus function produces.  This approach uses only basic R
graphics.  I imagine that it could be extended/incorporated into lattice or ggplot2 graphics.

Larry Gould

______________________________________________
From:   Gould, A. Lawrence
Sent:   Tuesday, January 25, 2011 10:48 AM
To:     'r-help at r-project.org'
Subject:        barplot with varaible-width bars

I would like to produce a bar plot with varying-width bars.  Here is an example to illustrate:

ww <- c(417,153,0.0216,0.0065,556,256,0.0162,0.0117,
+  726,379,0.0358,0.0501,786,502,0.0496,0.0837,
+  892,591,0.0785,0.0795)
yy<-t(t(array(ww,c(2,10))))

barplot(yy[,2*1:5],las=1,space=c(.1,.5),beside=T)

produces a barplot of 5 pairs of bars that are of equal width

barplot(yy[,2*1:5],las=1,width=c(yy[,(2*1:5)-1]),space=c(.1,.5),beside=T)

makes the bars in each pair of unequal width, but the two widths do not vary from pair to pair

I would like the width of each bar to be proportional to its corresponding value in the width statement of this last call of barplot, like what I think could be done with the mulbar function of SPlus.  Can I do this with barplot itself, or is this something for which lattice or ggplot 2 is needed?  And, if so, what would typical code look like?

Larry Gould

Notice:  This e-mail message, together with any attachments, contains
information of Merck & Co., Inc. (One Merck Drive, Whitehouse Station,
New Jersey, USA 08889), and/or its affiliates Direct contact information
for affiliates is available at
http://www.merck.com/contact/contacts.html) that may be confidential,
proprietary copyrighted and/or legally privileged. It is intended solely
for the use of the individual or entity named on this message. If you are
not the intended recipient, and have received this message in error,
please notify us immediately by reply e-mail and then delete it from