heatmap on ggmap error and best practice
Asked Answered
M

1

6

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

Mccreary answered 19/9, 2013 at 6:55 Comment(0)
B
6

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?

Bate answered 19/9, 2013 at 8:33 Comment(3)
I edited the question because my point did not fit in the comment space. Hope it is ok.Mccreary
@Mccreary I would remove the comment and ask another separate question about this specific issue.Bate
Please see #18892422Mccreary

© 2022 - 2024 — McMap. All rights reserved.