Skip to content
Prev 82785 / 398506 Next

Labeling a range of bars in barplot?

Marc Schwartz (via MN) wrote:
Thanks all for help on this question, including those who emailed me off 
list.

I went with the suggestion of Marc above, because I could follow through 
how to implement the code (other more complete solutions were hard for 
me to 'reverse engineer').

Here is my solution in full, which I feel gives rather nice output :)

## Approximate my data for you to try
x <- sort((runif(70)*100)^3,decreasing=T)

## Plot the barplot
mp <-
   barplot(x,
           # Remove default label names
           names.arg=rep('',70)
           )

## Break data range, and count bars per break
my.hist <-
   hist(x,plot=F,
        ## Pick the (approximate) number of labels
        ## NB: using quantiles is incorrect here
        breaks=4
        )

## Check for sanity
## points(mp[length(mp)],x[length(mp)],col=2)

## Counts become new 'breaks'
my.new.breaks <-
   my.hist$counts

## Some formating stuff
my.names <-
   sprintf("%.1d",my.hist$breaks)

# Prepare to add labels
op<-par(xpd=TRUE)

i <- length(mp)             # Note we label from right to left
q <- 1
#
for(j in my.new.breaks){
   st <- i                   #
   en <- i-j+1               #
   ##
   segments(mp[st],-50000,
            mp[en],-50000,lwd=2,col=2)
   ##
   text(mean(mp[st:en]),-100000,pos=1,
        paste(paste(my.names[q],"-",sep=" "),
              my.names[q+1],sep="\n"),cex=0.6)
   ##
   i <- i-j                  #
   q <- q+1
}


You should see that the density of labels corresponds to the range of 
data (hopefully not too dense), giving more labels to regions of the 
plot with bigger ranges.
Cheers,
Dan.