13

I want to generate a density plot of observed temperatures that is scaled by the number of events observed for each temperature data point. My data contains two columns: Temperature and Number [of observations].

Right now, I have a density plot that only incorporates the Temperature frequency according to:

plot(density(Temperature, na.rm=T), type="l", bty="n")

How do I scale this density to account for the Number of observations at each temperature? For example, I want to be able to see the temperature density plot scaled to show if there are greater/fewer observations for each temperature at higher/lower temperatures.

I think I'm looking for something that could weight the temperatures?

2 Answers 2

19

I think you can get what you want by passing a weights argument to density. Here's an example using ggplot

dat <- data.frame(Temperature = sort(runif(10)), Number = 1:10)
ggplot(dat, aes(Temperature)) + geom_density(aes(weights=Number/sum(Number)))
2
  • 15
    2017 Update: the aesthetic has been renamed weight without s, so that the plotting instruction becomes: ggplot(dat, aes(x = Temperature, weight = Number/sum(Number))) + geom_density(). Oct 10, 2017 at 7:31
  • 1
    Also, I don't think it is necessary to scale the weights to 1 yourself, ggplto2 seems to do it, can just use weight = Number
    – Matifou
    Apr 20, 2020 at 22:23
9

And to do this in base (using DanM's data):

plot(density(dat$Temperature,weights=dat$Number/sum(dat$Number),na.rm=T),type='l',bty='n')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.