I have a dataset with close to a million records (coordinates). While plotting that in a map using R, when I use 'geom_point' to plot the data, everything works fine. Then When I plot a density plot using 'stat_density2d', I hardly get any values plotted on the map for the same data set.
What I can think of is the number of bins I'm defining, as the more bins I use, the map fills up more. Yet when the bins increase, the number of dense areas disappear, and the same color to represent low density is used represent all areas
The code looks like this
ggmap(the_map_of_the_location)
+ stat_density2d(aes(x =all_longitudes, y =all_lattitudes, fill = ..level..), size=2, bins= 1000, geom = "polygon", data=my_df)
+ labs(x="Longitude", y="Latitude") + scale_fill_gradient(low = "green", high = "blue")
If the issue is the number of bins, is there a measure as to how I can decide on the number of bins (Like a mathematical formula)?
If that is not the issue, what am I missing?