Hi~ I saw some discussion about this but there seems to be not much answer. So I plot a density/heatmap on a city map.
df = data.frame(lon=rnorm(1000, mean=-87.62, sd=0.01),
lat=rnorm(1000, mean=41.88, sd=0.01))
map = get_googlemap('chicago', zoom=14, color='bw')
ggmap(map, extent='device') +
stat_density2d(
aes(x=lon, y=lat, fill=..level.., alpha =..level..),
size = 2, bins = 4,
data = df,
geom = "polygon"
) +
scale_fill_gradient(low='green', high='red') +
scale_alpha(guide=FALSE)
It plots alright but shows:
Warning message:
Removed 96 rows containing non-finite values (stat_density2d).
Using real city crime data, it actually reported removing most rows. So it worries me maybe the plot doesn't reflect the real distribution.
What does it mean? Should I worry that the plot doesn't reflect the data? Thanks!
I believe it means that some of your data are outside the range of the map.
After running your code above, this number matches the number of rows excluded in the warning from the map for me.
bb <- attr(map, "bb")
sum(df$lat < bb$ll.lat |
df$lat > bb$ur.lat |
df$lon < bb$ll.lon |
df$lon > bb$ur.lon)
It could also be due to the fact that you have some missing values on the variables used in your plot. A check with is.na(dataframe$variable) will help.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With