19

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.

Luuklag
  • 3,897
  • 11
  • 38
  • 57
sds
  • 58,617
  • 29
  • 161
  • 278

3 Answers3

35
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

Jake Burkhead
  • 6,435
  • 2
  • 21
  • 32
  • 2
    This is fantastic; so easy to do weighted histograms - and it works! But "weight" is not listed in the documentation (ggplot 3.1.1) as an aesthetic for geom_histogram(...). How did you know about it? – jlhoward Jun 07 '19 at 19:34
  • 2
    It's used in the examples for [`geom_histogram`](https://ggplot2.tidyverse.org/reference/geom_histogram.html#examples). Not sure how I knew about it 5+ years ago – Jake Burkhead Jun 07 '19 at 20:39
15

Package plotrix has a function weighted.hist which does what you want:

w<-seq(1,1000)
v<-sort(runif(1000))
weighted.hist(v, w)

Example of <code>weighted.hist</code>

musically_ut
  • 34,028
  • 8
  • 94
  • 106
  • 2
    Just fyi, the link is dead. Here is info about the package http://cran.r-project.org/web/packages/plotrix/index.html – Adam_G Jun 15 '15 at 14:48
  • @Adam_G Thanks! Updated the link in the answer. – musically_ut Jun 15 '15 at 14:50
  • The function is bugged: when I try to add a curve to it, it does not plot it at the right place... Try for instance ` weighted.hist(rnorm(T),rep(1,T)/T,col="wheat");curve(dnorm(x),add=TRUE)` – Xi'an ні війні Nov 25 '15 at 15:09
  • Why do the lines go below the x axis? or rather how can you stop the lines going below the x axis? – dpel Mar 22 '16 at 12:12
  • @DavidPell Those are ticks on the x-axis. This may help in getting rid of those: http://stackoverflow.com/questions/10393076/suppress-ticks-in-plot-in-r – musically_ut Mar 22 '16 at 13:05
  • @musically_ut ok fair enough - just a question of style. I was able to remove them with: `weighted.hist(v,w, xaxis=FALSE)` – dpel Mar 22 '16 at 14:28
6

An alternative from the weights package is wtd.hist()

w<-seq(1,1000) v<-sort(runif(1000)) wtd.hist(x=v,weight=w) enter image description here

dpel
  • 1,954
  • 1
  • 21
  • 31