Hello all, I have this simple barplot code: ifn <- "id.dat" dat <- read.table(ifn) ofn <- "id.png" bitmap(ofn, type = "png256", width = 30, height = 30, pointsize = 30, bg "white",res=50) par(mar=c(5, 5, 3, 2),lwd=5) par(cex.main=1.6,cex.lab=1.6,cex.axis=1.6) names(dat)<-c("NumberOfPeople","Average") Graph<-barplot(dat$Average) dev.off() and here is the data (id.dat): 15 0.08 6 0.09 7 0.37 I want to write down the ""NumberOfPeople" on top of each of the bars. Can anybody help me on this? Thanks, Mohsen [[alternative HTML version deleted]]
The only thing you're missing is the midpoints of the bars. Since you specified> Graph <- barplot(dat$Average)You can get the midpoints from the Graph object. So to put the number on top of each bar you might use something like:> text(Graph, dat$Average, dat$Average)-----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Mohsen Jafarikia Sent: Monday, July 27, 2009 10:02 AM To: R-help at stat.math.ethz.ch Subject: [R] numbers on barplot Hello all, I have this simple barplot code: ifn <- "id.dat" dat <- read.table(ifn) ofn <- "id.png" bitmap(ofn, type = "png256", width = 30, height = 30, pointsize = 30, bg "white",res=50) par(mar=c(5, 5, 3, 2),lwd=5) par(cex.main=1.6,cex.lab=1.6,cex.axis=1.6) names(dat)<-c("NumberOfPeople","Average") Graph<-barplot(dat$Average) dev.off() and here is the data (id.dat): 15 0.08 6 0.09 7 0.37 I want to write down the ""NumberOfPeople" on top of each of the bars. Can anybody help me on this? Thanks, Mohsen [[alternative HTML version deleted]] ______________________________________________ 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. ================================== P Please consider the environment before printing this e-mail Cleveland Clinic is ranked one of the top hospitals in America by U.S. News & World Report (2008). Visit us online at http://www.clevelandclinic.org for a complete listing of our services, staff and locations. Confidentiality Note: This message is intended for use\...{{dropped:13}}
names(dat)<-c("NumberOfPeople","Average") Graph<-barplot(dat$Average) barplot(dat$Average, ylim=c(0,max(dat[,2]+.2))) text(Graph, dat[,2], dat[,1], pos=3) The reason for the ylim is so that the number for the righthand bar does not go outside the plot area. --- On Mon, 7/27/09, Mohsen Jafarikia <jafarikia at gmail.com> wrote:> From: Mohsen Jafarikia <jafarikia at gmail.com> > Subject: [R] numbers on barplot > To: R-help at stat.math.ethz.ch > Received: Monday, July 27, 2009, 10:01 AM > Hello all, > I have this simple barplot code: > > ifn <- "id.dat" > dat <- read.table(ifn) > ofn <- "id.png" > > bitmap(ofn, type = "png256", width = 30, height = 30, > pointsize = 30, bg > "white",res=50) > par(mar=c(5, 5, 3, 2),lwd=5) > par(cex.main=1.6,cex.lab=1.6,cex.axis=1.6) > > names(dat)<-c("NumberOfPeople","Average") > Graph<-barplot(dat$Average) > dev.off() > > and here is the data (id.dat): > > 15? ? 0.08 > 6? ? 0.09 > 7? ? 0.37 > > I want to write down the ""NumberOfPeople" on top of each > of the bars. Can > anybody help me on this? > > Thanks, > Mohsen > > ??? [[alternative HTML version deleted]] > > ______________________________________________ > 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. >__________________________________________________________________ Looking for the perfect gift? Give the gift of Flickr! http://www.flickr.com/gift/
Unless you are intentionally trying to distort your data and make the graph harder to read (you don't want to do that), it is better to put the numbers in the margin rather than at the top of the bars. Try the following line after the barplot:> mtext( dat$NumberOfPeople, side=1, line=1.5, at=Graph, cex=1.6 )-- 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 Mohsen Jafarikia > Sent: Monday, July 27, 2009 8:02 AM > To: R-help at stat.math.ethz.ch > Subject: [R] numbers on barplot > > Hello all, > I have this simple barplot code: > > ifn <- "id.dat" > dat <- read.table(ifn) > ofn <- "id.png" > > bitmap(ofn, type = "png256", width = 30, height = 30, pointsize = 30, > bg > "white",res=50) > par(mar=c(5, 5, 3, 2),lwd=5) > par(cex.main=1.6,cex.lab=1.6,cex.axis=1.6) > > names(dat)<-c("NumberOfPeople","Average") > Graph<-barplot(dat$Average) > dev.off() > > and here is the data (id.dat): > > 15 0.08 > 6 0.09 > 7 0.37 > > I want to write down the ""NumberOfPeople" on top of each of the bars. > Can > anybody help me on this? > > Thanks, > Mohsen > > [[alternative HTML version deleted]] > > ______________________________________________ > 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.