Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scaled/weighted density plot

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?

like image 783
struggleBus Avatar asked Sep 27 '12 15:09

struggleBus


People also ask

What is a scaled density plot?

"Scaled Density." If you insist on using a non-density function that imitates the shape of the density function, you can make a frequency histogram with the same bins as the plot above, then use the vertical scale to decide what constant multiple of the KDE or the population density gives the effect you want.

How do you describe a density graph?

A density curve is a graph that shows probability. The area under the curve is equal to 100 percent of all probabilities. As we usually use decimals in probabilities you can also say that the area is equal to 1 (because 100% as a decimal is 1). The above density curve is a graph of how body weights are distributed.

Is a density plot the same as a histogram?

A Density Plot visualises the distribution of data over a continuous interval or time period. This chart is a variation of a Histogram that uses kernel smoothing to plot values, allowing for smoother distributions by smoothing out the noise.

What does density plot mean in R?

A density plot is a representation of the distribution of a numeric variable that uses a kernel density estimate to show the probability density function of the variable. In R Language we use the density() function which helps to compute kernel density estimates.


2 Answers

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)))
like image 90
Dan M. Avatar answered Oct 19 '22 11:10

Dan M.


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')
like image 32
thequerist Avatar answered Oct 19 '22 10:10

thequerist