[R] Drawing a rectangle around a barplot()

Marc Schwartz MSchwartz at MedAnalytics.com
Thu Dec 2 14:09:39 CET 2004


On Thu, 2004-12-02 at 11:07 +0000, Barry Rowlingson wrote:
> michael watson (IAH-C) wrote:
> > Hi
> > 
> >  I can't use box() as I don't
> > want to draw a box round my entire plot, I just want to draw a box as
> > the background to certain subsets of the bars.
> 
>    One idea. Edit the barplot.default function so it doesnt call plot.new.
> 
>   do:
> 
>   myBarplot = barplot.default
> 
>   then edit the myBarplot function. Add an extra parameter to the 
> argument list 'add', and make it False by default:
> 
>              cex.names = par("cex.axis"), inside = TRUE, plot = TRUE,
>              axis.lty = 0, add=F, ...)
> 
>   then find plot.new and wrap it in a condition:
> 
>      if(!add)plot.new()
> 
>   Now try:
> 
>   tN <- table(Ni <- rpois(100, lambda=5))
>   myBarplot(tN)
>   rect(2,1,6.5,8.5)
> 
>   - this puts the rectangle on top of the bars, which you dont want, so 
> you call myBarplot with add=T so that plot.new isnt called and the 
> barplot appears over the rectangle:
> 
>   myBarplot(tN,add=T)
> 
> works for me, if I understand your problem correctly!
> 
> Baz


In follow up to Baz' post, the 'add' argument is already in the 
barplot2() function, which is in the gregmisc bundle (gplots package) on
CRAN.

I have also been looking at adding a 'panel.first' and 'panel.last'
argument to barplot2, to enable functionality similar to that of
plot.default(). This would provide the ability to add additional plot
components before and/or after the bars are drawn. I'll get to that as
soon as time permits.

HTH,

Marc Schwartz




More information about the R-help mailing list