[R] add information above bars of a barplot()
wdunlap at tibco.com
Mon Mar 22 17:30:57 CET 2010
> -----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()
> 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, 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.
Spotfire, TIBCO Software
> thanks for any suggestions!
> R-help at r-project.org mailing list
> PLEASE do read the posting guide
> and provide commented, minimal, self-contained, reproducible code.
More information about the R-help