I have some spatial data points and I want to cluster them. Therefore I use the heatmap function of ggplot2. I get a nice looking heat map but I wan't to do some further analysis with the created areas and to be able to do this I want to extract the created polygons.
How can I do this? Or do I have to use another heatmap function and if so which one shall I use then?
I tried to produce a reproducible example. Here it is:
library(ggplot2)
lon<-rnorm(10000,mean = 15,sd=1)
lat<-rnorm(10000,mean=45,sd=1)
data <-cbind.data.frame(lon,lat)
heatmap <- ggplot(data,aes(x=lon,y=lat))+ stat_density2d(data=data,
aes(x=lon, y=lat, fill=..level..,
alpha=..level..), geom="polygon")
heatmap