Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

stat_density2d: removed rows containing non-finite values

Tags:

r

ggplot2

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!

like image 803
YJZ Avatar asked Feb 03 '15 15:02

YJZ


2 Answers

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)
like image 140
GregF Avatar answered Oct 19 '22 11:10

GregF


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.

like image 33
Loic Ulrich Avatar answered Oct 19 '22 13:10

Loic Ulrich