[R] Spacing of text does not match spacing of bars in barplot

Marc Schwartz marc_schwartz at me.com
Fri Mar 2 21:01:03 CET 2012

On Mar 2, 2012, at 1:52 PM, jon waterhouse wrote:

> I have a very standard barplot. My labels are too long to be printed
> horizontally under each bar, so I am using text to put the labels on a 45
> degree slant. 
> However, the labels are spaced more narrowly than the bars, so on an 8
> vertical bar plot, the end of the eighth label is lined up with the seventh
> bar.
> Preferably I don't want to do every text label separately (I'm having this
> problem on all the graphs where I'm using text()
> Using Windows and version 2.14.1
> X2sum <- c(42.6,  3.6,  1.8,  3.9, 12.1, 14.3, 14.6 ,28.4)
> X2.labels <- c("No earnings", "Less than $5000/year", "$5K to $10K" , "$10K
> to $15K"  , "$ 15K to $20K" ,       "$20K to $25K"  ,       "$25K to $30K"  
> ,      "Over $30K"   )      
> barplot(X2sum)
> text(1:8, par("usr")[3] - 0.5, srt = 45, adj = 1, labels =X2.labels, xpd =
> Thanks,
> Jon

Read ?barplot and take note of the Value section:

A numeric vector (or matrix, when beside = TRUE), say mp, giving the coordinates of all the bar midpoints drawn, useful for adding to the graph.

If beside is true, use colMeans(mp) for the midpoints of each group of bars, see example.


  mp <- barplot(X2sum)

  text(mp, ...)

You correctly read the R FAQ on the matter, but that example uses plot() rather than barplot(). The midpoints of the bars are not at integer values.


Marc Schwartz

More information about the R-help mailing list