hi, I have a barplot with six clusters of four bars each. Now I would like to add the exact value of each bar as a number above the bar. I hoped to get some tips here. I could simply add text at the different positions, but I don't understand how the margins on the x-axis are calculated (how can I get / calculate the x-ticks of a barplot?). Also I would like to code this flexible enough so that it still works when I have more bars in each cluster. thanks for any suggestions!
-----Mensaje original----- De: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] En nombre de Martin Batholdy Enviado el: lunes, 22 de marzo de 2010 15:53 Para: r help Asunto: [R] add information above bars of a barplot() hi, I have a barplot with six clusters of four bars each. Now I would like to add the exact value of each bar as a number above the bar. I hoped to get some tips here. I could simply add text at the different positions, but I don't understand how the margins on the x-axis are calculated (how can I get / calculate the x-ticks of a barplot?). Also I would like to code this flexible enough so that it still works when I have more bars in each cluster. thanks for any suggestions! -------- If you are barplotting x barplot(x) text(x=barplot(x),y=x,label=format(x),po=3) should get you closer to what you want. HTH Rub?n
> -----Original Message----- > From: r-help-bounces at r-project.org > [mailto:r-help-bounces at r-project.org] On Behalf Of Martin Batholdy > Sent: Monday, March 22, 2010 7:53 AM > To: r help > Subject: [R] add information above bars of a barplot() > > hi, > > > I have a barplot with six clusters of four bars each. > Now I would like to add the exact value of each bar as a > number above the bar. > > I hoped to get some tips here. > I could simply add text at the different positions, but I > don't understand how the margins on the x-axis are calculated > (how can I get / calculate the x-ticks of a barplot?). > > Also I would like to code this flexible enough so that it > still works when I have more bars in each cluster.You didn't say how you made the original barplot, but here is one way use barplot()'s return value (the x coordinates of the bar centers) to add text a little above the top of each bar: z <- rbind(log2(1:10), sqrt(1:10), (1:10)/3) # data matrix barX <- barplot(z, beside=TRUE) text(cex=.5, x=barX, y=z+par("cxy")[2]/2, round(z,2), xpd=TRUE) The xpd=TRUE means to not plot the text even if it is outside of the plot area and par("cxy") gives the size of a typical character in the current user coordinate system. Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com> > > > thanks for any suggestions! > > ______________________________________________ > 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. >
Adding text at the top of the bars will tend to distort the perception of their heights. It is better to place numbers (if they are even needed) in the margins. Switching to a dotplot instead of a barplot may be more meaningful as well. -- 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 Martin Batholdy > Sent: Monday, March 22, 2010 8:53 AM > To: r help > Subject: [R] add information above bars of a barplot() > > hi, > > > I have a barplot with six clusters of four bars each. > Now I would like to add the exact value of each bar as a number above > the bar. > > I hoped to get some tips here. > I could simply add text at the different positions, but I don't > understand how the margins on the x-axis are calculated > (how can I get / calculate the x-ticks of a barplot?). > > Also I would like to code this flexible enough so that it still works > when I have more bars in each cluster. > > > > thanks for any suggestions! > > ______________________________________________ > 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.