[R] Diagonal Labels on "Beside" Bars in Barplot
Marc Schwartz
marc_schwartz at me.com
Tue Dec 15 14:48:35 CET 2009
On Dec 14, 2009, at 10:34 PM, Jason Rupert wrote:
> My question is based on an example provided in the following:
> Referencing:
> Statistics with R
> Vincent Zoonekynd
> <zoonek at math.jussieu.fr>
> 6th January 2007
>
> URL:
> http://zoonek2.free.fr/UNIX/48_R/all.html
>
> data(HairEyeColor)
> a <- as.table( apply(HairEyeColor, c(1,2), sum) )
>
> # Provided Example
> barplot(a, beside = TRUE,
> legend.text = attr(a, "dimnames")$Hair)
>
>
>
> # I would like to make the labels on the x-axis diagonal, so I tried
> the following:
> barplot_reference<-barplot(a, beside = TRUE,
> legend.text = attr(a, "dimnames")$Hair,
> xaxt = "n", xlab = "")
>
> text(barplot_reference, par("usr")[3] - 0.09, srt = 45, adj = 1,
> labels = as.character(colnames(a)), xpd = TRUE, offset = 1,
> col = "black")
>
> # The labels are diagonal, but unfortunately the eye color labels
> are now applied to every bar and then repeat.
>
> # Is there any way to correct this problem, so that the diagonal
> labels are only the following:
> Brown, Blue, Hazel, Green
>
> # Those labels should not be repeated, so any help and insight is
> greatly appreciated.
Pay attention to the information provided in the Value section of ?
barplot:
"If beside is true, use colMeans(mp) for the midpoints of each group
of bars, see example."
Thus:
barplot_reference <- barplot(a, beside = TRUE,
legend.text = attr(a, "dimnames")$Hair,
xaxt = "n", xlab = "")
text(colMeans(barplot_reference), par("usr")[3] - 0.09, srt = 45, adj
= 1,
labels = as.character(colnames(a)), xpd = TRUE, offset = 1,
col = "black")
HTH,
Marc Schwartz
More information about the R-help
mailing list