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?
Thanks for your help.
Larry Gould
Notice: This e-mail message, together with any attachme...{{dropped:14}}
Hi Larry, If I understand correctly, your barplot() call dispatches to the method function barplot.default() to do the work. Looking at the definition of that function and your specific call, it seems that around line 51 of barplot.default(), the value of the width argument is truncated: width <- rep(width, length.out = NR) where NR <- nrow(height) is defined a bit earlier around line 44. So in the execution width takes on the value [1] 417 153 which seems to explain the same width pieces across pairs. Just quick and dirty I copied the function barplot.default in the workspace to an editor, renamed it as mod.barplot.default() and then commented out line 51 and added the line there: width <- width which seems at though it could actually be left out as long as beside=TRUE is kept in the call. Then I created mod.barplot.default() as a working function, and this call mod.barplot.default(yy[,2*1:5], las=1, width=yy[,(2*1:5)-1], space=c(.1,.5) ,beside=TRUE) looks like it might provide what you wanted. Hope that helps. Bill ------------------------- Bill Pikounis http://billpikounis.net/ On Tue, Jan 25, 2011 at 10:47, Gould, A. Lawrence <larry_gould at merck.com> wrote:> 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? > > Thanks for your help. > > Larry Gould > > > Notice: ?This e-mail message, together with any attachme...{{dropped:14}} > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >
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?
Thanks for your help.
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
your system.
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: barplot.txt
URL:
<https://stat.ethz.ch/pipermail/r-help/attachments/20110131/a10c7679/attachment.txt>