0

I have some data as of below:

mydata <- rbind(c(5,4,3,7,6),
                c(1,4,5,2,2))
colnames(mydata) <- c("Ali","Bala","Chun Li","Danny","Esther")

And I would like to do a grouped bar plot of my data:

x <- barplot(mydata, beside=T, las=2,
            col=c("yellow2", "brown2"),
            main="Chocolates Eaten by Students",
            ylab="No. of chocolates")
legend("topright", cex=0.7,
       legend = c("White", "Dark"), 
       fill = c("yellow2", "brown2"))

enter image description here

How can I rotate the x axis labels 45 degrees clockwise (from the current positions)?


Note: Using the following did not give me the output I want.

text(cex=1, x=x+0.5, y=0, colnames(mydata), xpd=TRUE, srt=45, pos=2)
remykarem
  • 2,251
  • 22
  • 28
  • 1
    @CathG `x <- barplot()` I guess OP didn't copy the right line. But this question is clearly a duplicate. –  Jul 06 '15 at 07:01
  • 3
    just do `x <- barplot(mydata, beside=T, las=2,col=c("yellow2", "brown2"),main="Chocolates Eaten by Students",ylab="No. of chocolates", names=rep("", ncol(mydata))) ; legend("topright", cex=0.7, legend = c("White", "Dark"), fill = c("yellow2", "brown2")); text(cex=1, x=x[1,]+0.5, y=0, colnames(mydata), xpd=TRUE, srt=45, adj=1)` – Cath Jul 06 '15 at 07:11
  • 1
    @CathG You need `colMeans(x)+0.5`. –  Jul 06 '15 at 07:13
  • @Pascal, no I don't, there is only 2 lines, first line is giving the position of first bars, second line is giving position of 2nd bars, those 2 postions are separated by 1 unit so first position + 0.5 gives the middle of the 2 bars. (If there were more than 2 bars, then colMeans would give the result. (But just colMeans, no `+0.5`) – Cath Jul 06 '15 at 07:16
  • @CathG Yes. But I prefer the `colmneans` solution, anyway. (without +0.5) –  Jul 06 '15 at 07:19
  • @Pascal I agree, I proposed this because it was just a tiny modification from the OP's line – Cath Jul 06 '15 at 07:26
  • @Pascal Sorry about the duplicate guys and thanks for the inputs! – remykarem Jul 06 '15 at 11:47

0 Answers0