Search code examples
rggplot2kernel-density

Plot output from provenance package in ggplot2


I would like to plot the output from the provenance package using ggplot2. Specifically, the output from the function KDE() which results in a class KDE. (It uses an adaptive bandwidth for the KDE, which is why I cannot use the kde estimate from ggplot2)

install.packages("provenance")
library(provenance)

data(Namib)
samp <- Namib$DZ$x[['N1']]
dens <- KDE(samp,0,3000,kernel="epanechnikov")

I would like to plot the output in dens using ggplot2, however the class KDE is not supported by ggplot2.

Is there a way to extract the information needed to plot the density estimate in ggplot2? I tried to look at the structure of the class KDE, however, I cannot figure out how or what to extract. dens$x, I think, is where the x-component is stored, and the y-component in dens$y.


Solution

  • Yes you can access the pieces you need with '$'. Just combine those to into a data frame and have that be your ggplot data.

    ggplot(data.frame(x = dens$x,y =  dens$y), aes(x = x, y = y)) + 
      geom_point()