[R] Diagonal Labels on "Beside" Bars in Barplot

Jason Rupert jasonkrupert at yahoo.com
Tue Dec 15 16:52:33 CET 2009


Marc, 

Thanks a ton.  That was it.

Evidently I missed that the first time through the barplot.   I will be more diligent in reading the manuals. 

Thanks again, 
Jason 


 


----- Original Message ----
From: Marc Schwartz <marc_schwartz at me.com>
To: Jason Rupert <jasonkrupert at yahoo.com>
Cc: R Project Help <R-help at r-project.org>
Sent: Tue, December 15, 2009 7:48:35 AM
Subject: Re: [R] Diagonal Labels on "Beside" Bars in Barplot


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