0

I would simply like to add the data values on top of every bar of the charts. How can I best do this?

library("RColorBrewer")

HOLD <- matrix(c(0.80, 0.70, 0.70, 0.65, 0.56, 0.56, 0.78, 0.54, 0.40, 0.76, 0.23, 0.45), ncol=6, nrow=2)

colnames(HOLD) <- c("Infolettre", "Promotion", "Abonne", "Comédie", "Drame", "Suspense")
rownames(HOLD) <- c("D.T.", "F.T.")

HOLD

SEQUENTIAL <- brewer.pal(2, "PuBu")

par(mfrow=c(1,3))
barplot(HOLD[,1] * 100, main="Infolettre", ylab="%", ylim=c(0, 100), las=1,  col=SEQUENTIAL)
barplot(HOLD[,2] * 100, main="Promotion", ylab="%", ylim=c(0,100), las=1, col=SEQUENTIAL)
barplot(HOLD[,3] * 100, main="Abonne", ylab="%", ylim=c(0,100), las=1, col=SEQUENTIAL)

Thanks

Xavier
  • 303
  • 6
  • 14

1 Answers1

1

You just need to add the right text statements.

par(mfrow=c(1,3))
BP = barplot(HOLD[,1] * 100, main="Infolettre", ylab="%", ylim=c(0, 100), las=1,  col=SEQUENTIAL)
text(BP, HOLD[,1]*100, labels=HOLD[,1]*100, pos=3)

BP = barplot(HOLD[,2] * 100, main="Promotion", ylab="%", ylim=c(0,100), las=1, col=SEQUENTIAL)
text(BP, HOLD[,2]*100, labels=HOLD[,2]*100, pos=3)

BP = barplot(HOLD[,3] * 100, main="Abonne", ylab="%", ylim=c(0,100), las=1, col=SEQUENTIAL)
text(BP, HOLD[,3]*100, labels=HOLD[,3]*100, pos=3)
G5W
  • 36,531
  • 10
  • 47
  • 80
  • Thank you for your answer, is there any way to center the x coordinate of the text function? – Xavier Mar 25 '17 at 23:57
  • How does R knows how to center the text with xy coords? Thansl – Xavier Mar 26 '17 at 00:19
  • The first two arguments to `text` are the x and y coordinates. boxplot returns the x coordinates of the centers of the boxes. That is what I captured in BP. You created the y-coordinates, so we knew those. But using those directly would put the labels _on_ the line. pos=3 makes it just above the line. – G5W Mar 26 '17 at 00:22
  • Okok thank you so much! – Xavier Mar 26 '17 at 00:24