Positions and text length of bar plot labels

16,521

The digits issue can be solved easily by using round(tmp). As @rawr suggests, use the output of barplot to position the labels. Lastly, if you want to draw numbers above the bar, add xpd=NA to allow the number of the highest bar to be drawn outside the plot region.

bp = barplot(tmp, names=c("site 1", "site 2", "site 3", "site 4") )
# numbers above bars
text(x=bp, y=tmp, labels=round(tmp,0), pos=3, xpd=NA)
# numbers within bars
text(x=bp, y=tmp, labels=round(tmp,0), pos=1)
Share:
16,521
Chris Heckman
Author by

Chris Heckman

Updated on July 08, 2022

Comments

  • Chris Heckman
    Chris Heckman almost 2 years

    I am having trouble with some labels. I am doing bar plots in r and I am a little shocked there isn't an easy command to just place the values at the top. Anyway, I want these labels to be more centered in the bars and to shorten the significant digits so they will fit within the bar. I would also appreciate any suggestions on how to streamline this.

    I have tried options(digits=5) and this did not work for the labels. I have used text(plot.name, tmp, labels= c(tmp) but wanted to try and not use this to make it simpler. I have to remake a lot a plots.

    tmp = c(mean(1.0000001:100),mean(100.0000001:200), mean(200.0000001:300), mean(300.0000001:400))
    
    barplot(tmp, names=c("site 1", "site 2", "site 3", "site 4") )
    
    text(1:4, tmp, label=tmp, pos=2, srt=90)