Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
573 views
in Technique[技术] by (71.8m points)

graphics - How to label histogram bars with data values or percents in R

I'd like to label each bar of a histogram with either the number of counts in that bin or the percent of total counts that are in that bin. I'm sure there must be a way to do this, but I haven't been able to find it. This page has a couple of pictures of SAS histograms that do basically what I'm trying to do (but the site doesn't seem to have R versions): http://www.ats.ucla.edu/stat/sas/faq/histogram_anno.htm

If possible, it would also be nice to have the flexibility to put the labels above or somewhere inside the bars, as desired.

I'm trying to do this with the base R plotting facilities, but I'd be interested in methods to do this in ggplot2 and lattice as well.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

To include the number of counts, you can just set labels=TRUE.

The example below is just slightly adapted from one on the hist() help page:

hist(islands, col="gray", labels = TRUE, ylim=c(0, 45))

enter image description here

Getting percentages is a bit more involved. The only way I know to do that it to directly manipulate the object returned by a call to hist(), as described in a bit more detail in my answer to this similar question:

histPercent <- function(x, ...) {
   H <- hist(x, plot = FALSE)
   H$density <- with(H, 100 * density* diff(breaks)[1])
   labs <- paste(round(H$density), "%", sep="")
   plot(H, freq = FALSE, labels = labs, ylim=c(0, 1.08*max(H$density)),...)
}

histPercent(islands, col="gray")

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...