plot - R graphics: add labels to clustered/dodged bar chart -
this basic plotting question:
i need add labels clustered/dodged bar chart. have looked @ several examples using text()
, cannot seem position labels correctly.
teachers <- c("a", "b","c", "d", "e") mean_pre_scores <- c(10, 11, 12, 10,9) mean_post_scores <- c(12,15,17,13,12) pre_post <- data.frame(mean_pre_scores, mean_post_scores) pre_post <- as.matrix(pre_post) barplot((t(pre_post)), beside = t, names = teachers, legend = c("pre", "post"), ylim = c(0,20), args.legend = list(x="bottomright"), axes = t, main = "unit 1 test", col=c(26,51))
i want modify plot values displayed above bars. helpful know how show values inside bars.
i think you're after:
z <- barplot((t(pre_post)), beside = t, names = teachers, legend = c("pre", "post"), ylim = c(0,20), args.legend = list(x="topright"), axes = t, main = "unit 1 content pre test", col=c(26,51)) text(cex=1, x=c(z[1, ], z[2, ]), y=c(pre_post) + par("cxy")[2]/2, c(pre_post), xpd=true)
to move text inside bars use subtractions in:
text(cex=1, x=c(z[1, ], z[2, ]), y=c(pre_post) - par("cxy")[2]/2, c(pre_post), xpd=true)
Comments
Post a Comment