[R] labelling barplot

Marc Schwartz MSchwartz at MedAnalytics.com
Sat Nov 27 16:22:36 CET 2004


On Sat, 2004-11-27 at 08:59 +0000, Ross Clement wrote:
> Hi. I'd like to produce a barplot where only the lowest value and the 
> highest value are labelled on the x-axis. E.g. I might have a list
> of numbers and frequencies:
> 
> barplot( c( 2, 2, 0, 4, 2 ), names.arg=c( 1, 2, 3, 4, 5 ) )
> 
> where the data is a set of counts for some values between 1 and 5. I'd
> like to have a barplot where only the extremes 1 and 5 are labelled.
> 
> How do I do this?
> 
> Thanks in anticipation,
> 
> Ross Clement


Draw your barplot, saving the bar midpoints in 'mp':

mp <- barplot( c( 2, 2, 0, 4, 2 ))


Now using axis(), label the x axis. Set 'at' to be the first and last
values in the 'mp' vector:

axis(1, labels = c(1, 5), at = c(mp[1], mp[length(mp)]))


Another alternative is to use mtext() instead of axis() like this:

mtext(side = 1, text = c(1, 5), at = c(mp[1], mp[length(mp)]), line = 1)


HTH,

Marc Schwartz




More information about the R-help mailing list