[R] Bar plots with bars made of stacked text

Jim Lemon jim at bitwrit.com.au
Thu Apr 1 01:41:58 CEST 2010


On 04/01/2010 05:09 AM, Thomas Levine wrote:
> I would like to make bar plots where the bars are composed of text like this:
> http://www.thomaslevine.com/lowres/text_bars.png
>
> Is there a package that will help me with this? Thanks
>
Hi Tom,
Suppose you have a data frame like this:

zoodat
   names morning noon evening
1   Amy       0    0       1
2 Billy       1    1       0
3 Jonny       1    0       1
4  Katy       1    0       0
5 Patty       0    1       0
6 Sally       0    0       1
7 Suzie       0    1       0
8   Tom       0    1       0
9 Vicky       0    1       0

You can get something like your example with this:

textBarPlot<-function(labels,flags,main="",cex=1) {
  nbars<-length(flags)
  showlabels<-sapply(flags,function(x) x>0)
  plot(0,xlim=c(0,nbars+1),ylim=c(0,max(colSums(showlabels))),
   xlab="",ylab="",type="n",axes=FALSE)
  labelheight<-strheight("Oy",cex=cex)*2
  for(stack in 1:nbars) {
   ypos<-(1:sum(showlabels[,stack])-0.5)*labelheight
   text(stack,ypos,labels[showlabels[,stack]],cex=cex)
  }
  axis(1,at=1:nbars,labels=names(flags))
  box()
}

textBarPlot(zoodat[,1],zoodat[,2:4],
  main="People at the zoo",cex=1.5)

but you will probably want to fool around with the plot dimensions to 
make it neater.

Jim



More information about the R-help mailing list