Scaled / Weighted Density Graph

I want to create a graph of the density of the observed temperatures, which is scaled by the number of events observed for each point of the temperature data. My data contains two columns: temperature and number of [observations].

Right now I have a density graph that includes only the temperature frequency according to:

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

How can I scale this density to account for the number of observations at each temperature? For example, I want to be able to see a graph of the temperature density on a scale to show if there are more / less observations for each temperature at higher / lower temperatures.

I think I'm looking for something that could weigh the temperature?

+8
source share
2 answers

And do it in the database (using DanM data):

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

I think you can get what you want by passing the weights argument to density . Here is 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))) 
+9
source

Source: https://habr.com/ru/post/926421/


All Articles