1

I use the ggmap package and I want to plot a heat map according to a value and not the records count.

With the help of others posts about same topics, I can approach this goal but the plot is not really a heat map.

MyPlot

I precise that it's not a duplicate of Heatmap plot by value using ggmap and ggmap Heatmap with value because I don't want to facet my plot nor have a choropleth map.

Here is the code and an extract of the data.

The code :

library(ggmap)

CentreCarte <- c(lon = 2.35, lat = 48.85) 
Carte <- get_map(location = CentreCarte, source = "google", maptype =     "terrain", zoom = 9, color = "bw")

ggmap(Carte) %+% MyData + 
  aes(x = lon, y = lat, z = Satisfaction) +
  stat_summary2d(fun = median, alpha = 0.5)

The data :

structure(list(lon = c(2.336018, 2.336018, 2.5221629, 2.246237, 
2.3259213, 2.16647, 2.1203554, 1.735384, 1.940096, 1.765638, 
2.2899811, 2.165493, 2.110156, 2.378493, 2.3716529, 2.31002, 
2.3827967, 2.3625879, 2.3529867, 2.3719424, 2.4270832, 2.897243, 
2.656802, 2.6880829, 2.504403, 2.458625, 3.299203, 2.2785604), 
    lat = c(48.7022059, 48.7022059, 48.7708784, 48.589172, 48.6408416, 
    48.6792779, 48.8048649, 48.992181, 48.571308, 48.805027, 
    48.9162844, 48.990006, 48.880777, 48.997347, 49.093886, 48.796696, 
    48.8387005, 48.9259577, 48.891305, 48.8111854, 48.9156278, 
    48.928386, 48.7904005, 48.868659, 48.90852, 48.892489, 48.560149, 
    48.8399261), Satisfaction = c(5, 4, 1, 4, 3, 3, 2, 3, 5, 
    1, 4, 3, 1, 5, 3, 3, 3, 2, 3, 4, 2, 3, 3, 1, 2, 2, 5, 4)), .Names = c("lon", 
"lat", "Satisfaction"), row.names = c(560L, 561L, 565L, 566L, 
569L, 570L, 573L, 574L, 576L, 581L, 594L, 597L, 599L, 600L, 601L, 
605L, 606L, 607L, 608L, 609L, 611L, 612L, 615L, 616L, 619L, 622L, 
623L, 624L), class = "data.frame")
Kumpelka
  • 869
  • 3
  • 11
  • 33
  • I just found your question now. If you use `stat_summary_2d()`, you are asking the function to create bins (i.e., colored squares on the map) by applying a function in it. In your case, you use `median()`. So `stat_summary_2d()` defines how many bins you need (by default it is 30), and it will find median value for `Satisfaction` in each bin. The map you have in your question is a heat map in this sense. If you want to sum up the values of Satisfaction, you would use `sum` as a function in `stat_summary_2d()`. – jazzurro Dec 10 '17 at 02:03
  • Thanks for your response. But my main problem is that my code display a 'discrete' map with a point for each value and not a 'continuous' map (i.e. a heat map). – Kumpelka Dec 11 '17 at 08:53
  • Isn't that because you use median? Given you have a small amount of data, you may see the "discrete" map. – jazzurro Dec 11 '17 at 12:11

1 Answers1

0

You are using stat_summary2d(), which plots a 3D surface into 2D rectangular bins. The data you posted only has 28 observations. If you would like to make something looking like a heatmap you can use stat_density2d(), which estimates the density based on your data.

Code

ggmap(Carte) + 
    stat_density2d(data = MyData,
                   aes(x = lon, y = lat, fill = ..level.., alpha = ..level..),
                   geom = "polygon") + 
  scale_alpha(range = c(0, 1), guide = FALSE) + 
  scale_fill_gradient(low = "green", high = "red")

Map

enter image description here

Roman
  • 4,744
  • 2
  • 16
  • 58