[R] adding labels above bars in a barplot

Antonio Olinto aolinto_r at bignet.com.br
Fri Sep 10 16:59:16 CEST 2010


Hello,

I want to make a general routine to draw barplots with numbers plotted  
above each bar. See the example below.

I could not place the numbers on the middle of each bar because I  
could not calculate the right position of each x-axis tick. axTicks(1)  
indicated a unitary step, but it does not seem work.

I appreciate any help or suggestions.

Best regards,

Antonio Olinto

======================

CAT	VAR1	VAR2
Category 01	17.5	9
Category 02	15.2	20
Category 03	10.3	500
Category 04	8.4	150
Category 05	20.3	5000

# Coping data from a spreadsheet

dat.data <- read.delim("clipboard",header=T)

summary(dat.data)
           CAT         VAR1            VAR2
  Category 01:1   Min.   : 8.40   Min.   :   9
  Category 02:1   1st Qu.:10.30   1st Qu.:  20
  Category 03:1   Median :15.20   Median : 150
  Category 04:1   Mean   :14.34   Mean   :1136
  Category 05:1   3rd Qu.:17.50   3rd Qu.: 500
                  Max.   :20.30   Max.   :5000

dat.bar <- data.frame(dat.data[,c(2)])
row.names(dat.bar)<-dat.data[,1]
names(dat.bar)<-c("VAR1")
dat.bar
             VAR1
Category 01 17.5
Category 02 15.2
Category 03 10.3
Category 04  8.4
Category 05 20.3

par(mar=c(12,6,3,2),cex.axis=1.2,cex.lab=1.4)
barplot(t(as.matrix(dat.bar)),ylim=c(0,max(dat.data[,2]*1.1)),las=2,ylab="Y  
label text",col="orange")
box()

up <- max(dat.data$VAR1)*0.1

for (i in c(0:nrow(dat.data))) {
legend(0.25+i,dat.bar[1+i,1]+up,dat.data[i+1,3],col="blue",bty="n")
}


----------------------------------------------------------------
Webmail - iBCMG Internet
http://www.ibcmg.com.br



More information about the R-help mailing list