I've computed and plotted gaussian kernel density estimates using the KernSmooth package as follows:
x <- MyData$MyNumericVector
h <- dpik(x)
est <- bkde(x, bandwidth=h)
plot(est, type='l')
This is the method described in KernSmooth's documentation. Note that dpik()
finds the optimal bandwidth and bkde()
uses this bandwidth to fit the kernel density estimate. It's important that I use this method instead of the basic density()
function.
How do I layer these plots on top of one another?
I cannot use the basic density()
function that geom_density()
from ggplot2
relies upon, as bandwidths and kernel density estimates are best optimized using the KernSmooth package (see Deng & Wickham, 2011 here: http://vita.had.co.nz/papers/density-estimation.pdf). Since Wickham wrote ggplot2
and the above review of kernel density estimation packages, it would make sense that there's a way to use ggplot2
to layer densities that aren't reliant on the basic density()
function, but I'm not sure.
Can I use ggplot2
for this even if I don't wish to use the basic density()
function? What about lattice
?
You could do it with geom_line
:
m <- ggplot(NULL, aes(x=bkde(movies$votes)$x,y=bkde(movies$votes)$y)) + geom_line()
print(m)
If you were doing t with lattice::densityplot, you could probably add some of the values to the drags-list:
darg
list of arguments to be passed to the density function. Typically, this should be a list with zero or more of the following components : bw, adjust, kernel, window, width, give.Rkern, n, from, to, cut, na.rm (see density for details)