Create a histogram for weighted values

sds picture sds · Nov 7, 2013 · Viewed 15.5k times · Source

If I have a vector (e.g., v<-runif(1000)), I can plot its histogram (which will look, more or less, as a horizontal line because v is a sample from the uniform distribution).

However, suppose I have a vector and its associated weights (e.g., w<-seq(1,1000) in addition to v<-sort(runif(1000))). E.g., this is the result of table() on a much larger data set.

How do I plot the new histogram? (it should look more of less like the y=x line in this example).

I guess I could reverse the effects of table by using rep (hist(rep(v,w))) but this "solution" seems ugly and resource-heavy (creates an intermediate vector of size sum(w)), and it only supports integer weights.

Answer

Jake Burkhead picture Jake Burkhead · Nov 7, 2013
library(ggplot2)
w <- seq(1,1000)
v <- sort(runif(1000))

foo <- data.frame(v, w)

ggplot(foo, aes(v, weight = w)) + geom_histogram()

enter image description here