Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

heatmap on ggmap error and best practice

I want to plot a heatmap on a ggmap.

  library(ggmap)
  turku<-get_map('turku', zoom=13)
  turkumap<-ggmap(turku, extent="device", legend="topleft")
  turkumap
  turkumap+geom_density2d(mapping=aes(x = lon, y = lat),data = test, )

the error i get is:

 Error in (function (x, y, h, n = 25, lims = c(range(x), range(y)))  : 
 bandwidths must be strictly positive

The test variable is:

  test
         lon     lat var1.pred
  1  22.25320 60.4314 -67.04862
  2  22.25332 60.4314 -67.07793
  3  22.25344 60.4314 -67.11007
  4  22.25356 60.4314 -67.14517
  5  22.25368 60.4314 -67.18336
  6  22.25379 60.4314 -67.22478 
  7  22.25391 60.4314 -67.26956
  8  22.25403 60.4314 -67.31783
  9  22.25415 60.4314 -67.36973
  10 22.25427 60.4314 -67.42537

Suggestions? The variable test has many more entries, What i want to plot is the result of kriging, obtained through the function krige in the gstat library.

Is there a better way to do it?

I am open to very different solutions

like image 460
Irene Avatar asked Sep 19 '13 06:09

Irene


1 Answers

The issue you have is that the lat values are all the same. This means that the variance in the lat direction is zero, so a bandwidth for the kernel density estimate can't be calculated

You can hard code a bandwidth,

turkumap + geom_density2d(mapping=aes(x = lon, y = lat),
                 data = test, h=0.01)

but in your case I would suggest not using geom_density2d for this particular data set. Perhaps just plotting the points?

like image 73
csgillespie Avatar answered Sep 19 '22 06:09

csgillespie