[R] adding labels above bars in a barplot

Greg Snow Greg.Snow at imail.org
Fri Sep 10 19:44:10 CEST 2010


See this message and the replies to it (and the replies to the replies, etc.):

http://tolstoy.newcastle.edu.au/R/e2/help/07/08/22858.html

In there is a discussion of why you don't really want to do that along with better alternatives and examples of the improved plots.

-- 
Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
greg.snow at imail.org
801.408.8111


> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of Antonio Olinto
> Sent: Friday, September 10, 2010 8:59 AM
> To: R-help
> Subject: [R] adding labels above bars in a barplot
> 
> 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,yla
> b="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
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-
> guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list