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.
Package plotrix
has a function weighted.hist
which does what you want:
w<-seq(1,1000)
v<-sort(runif(1000))
weighted.hist(v, w)
library(ggplot2)
w <- seq(1,1000)
v <- sort(runif(1000))
foo <- data.frame(v, w)
ggplot(foo, aes(v, weight = w)) + geom_histogram()
来源:https://stackoverflow.com/questions/19841204/create-a-histogram-for-weighted-values