[R] Spacing of text does not match spacing of bars in barplot
Waterhouse, Jon D.
jonwaterhouse at gov.nl.ca
Mon Mar 5 13:32:03 CET 2012
Thanks guys.
I figured R would have an elegant and simple solution. I spent a bunch
of (fruitless) time going down the par(mar) and par("usr") road, so I
thought it was time to ask.
Jon
-----Original Message-----
From: Marc Schwartz [mailto:marc_schwartz at me.com]
Sent: Friday, March 02, 2012 4:31 PM
To: Waterhouse, Jon D.
Cc: r-help at r-project.org
Subject: Re: [R] Spacing of text does not match spacing of bars in
barplot
On Mar 2, 2012, at 1:52 PM, jon waterhouse wrote:
> I have a very standard barplot. My labels are too long to be printed
> horizontally under each bar, so I am using text to put the labels on a
45
> degree slant.
>
> However, the labels are spaced more narrowly than the bars, so on an 8
> vertical bar plot, the end of the eighth label is lined up with the
seventh
> bar.
>
> Preferably I don't want to do every text label separately (I'm having
this
> problem on all the graphs where I'm using text()
>
> Using Windows and version 2.14.1
>
> X2sum <- c(42.6, 3.6, 1.8, 3.9, 12.1, 14.3, 14.6 ,28.4)
> X2.labels <- c("No earnings", "Less than $5000/year", "$5K to $10K" ,
"$10K
> to $15K" , "$ 15K to $20K" , "$20K to $25K" , "$25K to
$30K"
> , "Over $30K" )
>
> barplot(X2sum)
> text(1:8, par("usr")[3] - 0.5, srt = 45, adj = 1, labels =X2.labels,
xpd =
> TRUE)
>
> Thanks,
>
> Jon
Read ?barplot and take note of the Value section:
A numeric vector (or matrix, when beside = TRUE), say mp, giving the
coordinates of all the bar midpoints drawn, useful for adding to the
graph.
If beside is true, use colMeans(mp) for the midpoints of each group of
bars, see example.
Thus:
mp <- barplot(X2sum)
text(mp, ...)
You correctly read the R FAQ on the matter, but that example uses plot()
rather than barplot(). The midpoints of the bars are not at integer
values.
HTH,
Marc Schwartz
“This email and any attached files are intended for the sole use of the primary and copied addressee(s) and may contain privileged and/or confidential information. Any distribution, use or copying by any means of this information is strictly prohibited. If you received this email in error, please delete it immediately and notify the sender.”
More information about the R-help
mailing list