I have coordinate data in R, and I would like to determine a distribution of where my points lie. The entire space of points is a square of side length 100.
I\'d li
If you're willing to use ggplot2
, there are some nice options.
ggplot(data.frame(x,y), aes(x,y)) + geom_bin2d()
ggplot(data.frame(x,y), aes(x,y)) + stat_density2d(aes(fill = ..level..), geom = "polygon")
Update: To calculate the 2d binning, you could use a 2d (bivariate) normal kernel density smoothing:
library(KernSmooth)
bins <- bkde2D(as.matrix(data.frame(x, y)), bandwidth = c(2, 2), gridsize = c(25L, 25L))
which can also be plotted as
library(reshape2)
ggplot(melt(bins$fhat), aes(Var1, Var2, fill = value)) + geom_raster()
The bins
object contains the x
and y
values and normalised density fhat
. Play with the gridsize (number of grid points in each direction) and bandwidth (smoothing scale) to get what you're after.