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

Jason Rupert jasonkrupert at yahoo.com
Tue Dec 15 05:34:59 CET 2009


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.




More information about the R-help mailing list