Leaflet Heatmap

Hello, I wanted to see if anyone here had any advise or could share how you created a heatmap using leaflet. I saw this example online but I feel like there could be other ways of creating something similar. I am currently using the cluster options in leaflet to show where points are the highest which is good, but not great. (image below) If you've made a map and would be willing to share I'd appreciate it! I do have a shapefile but don't want to use it for mapping at the moment.



This will depend on what you want to achieve. In general leaflet is more of a presentation than data mungling tool - this is best handled in preparatory phase, with only the final result displayed via {leaflet}.

If your definition of a heatmap is something like this - Kde se v Čechách nejvíc krade? · Jindra Lacko - you can safely ignore the text, which is in a language I don't expect you to be familiar with, and focus on the maps (count of crimes per grid cells, with counts mapped to color scale) I would be happy to help.

1 Like

Thanks for the response! The reason I decided on using leaflet is because it is free and I am not able to use ggmap since it requires a google api key. I use arc pro for my mapping but because this will be on shiny, I'm not able to do density or heat maps similar to arc pro. My goal is to do something similar in R and the link I posted caught my eye. The data points will be basic and only uses Longitude and Latitude.

The web link you sent is very cool! I'm not sure if it is using a shapefile or if it is just the points on a map. Either way, very cool and I didn't know that was possible.

My recollection is that when using Leaflet for heatmaps, how they appeared was dependent on the scale of the map - not a good thing. I ended up making density maps as described here:

1 Like

You may be mixing a few things together:

  • leaflet is a presentation tool, it gets rendered client side - meaning you need to get all your data to your user's browser, where it gets processed and presented. This means that it is not always a good choice for heatmap kind of situations, when a lot of point data has to be transferred and rendered client side.
  • the (in)famous Google API key used in {ggmap} is not as hard a showstopper as may seem. First of all it can be obtained relatively easily, and the free plan is generous. Secondly the Google Maps basemap, while certainly most popular and eponymous with the package, is not the only one supported.
  • if interactivity is desired you may consider applying a grid of sort over your area of interest, and render the heatmap not directly from your point data, but from count of points over grid (which is what I did in the Czech page linked earlier) - this is more efficient in terms of data transferred, and you have more control over the end product with stuff like resolution or what not.

Anyhow, regarding the "free" aspect of ggmap: consider this piece of code, building on earlier post about Galapagos Islands, but updated for the fact that Stamen Maps no longer live on Stamen but on Stadia (it is a long story). Running it requires no payment nor registration, and it may be enough for your use case.


# download the basemap from Stadia
galapagos <- get_stadiamap(bbox = c(left = - 91, 
                                    bottom = -1.0, 
                                    right = -89.8, 
                                    top = -0.18),
                           zoom = 10, 
                           maptype = "stamen_toner")

points <- data.frame(x = c(-90, -90.5, -90.25), # some random points...
                     y = c(-0.5, -0.8, -0.3))

ggmap(galapagos) + # the base map
  geom_point(data = points, aes(x = x, y = y), 
             color = "red") # standard ggplot2 syntax

You can use leaflet-heatmap plugin to create heatmap