Contents
Is it easy to make a choropleth with leaflet?
Making choropleths with leaflet is easy. In this example, we’ll duplicate the step-by-step choropleth tutorial from the Leaflet.js website. The final result will look like this (scroll to the end to see the completed code):
How to create interactive choropleth map with GeoJSON?
This is a case study of creating a colorful interactive choropleth map of US States Population Density with the help of GeoJSON and some custom controls (that will hopefully convince all the remaining major news and government websites that do not use Leaflet yet to start doing so).
How to make a map in R-choropleth?
As you can see, we now have a SpatialPolygonsDataFrame with name (state name) and density (population density in people/mi 2) columns from the GeoJSON. Next, let’s make a basic map with just the outline of the states.
How do you highlight polygons in leaflet.js?
The next thing we’ll want is to make the polygons highlight as the mouse passes over them. The addPolygon function has a highlight argument that makes this simple. (The Leaflet.js tutorial also adds an event handler that zooms into a state when it’s clicked.
How to map data to colors in leaflet.js?
You have various options for mapping data to colors; for this example we’ll match the Leaflet.js tutorial by mapping a specific set of bins into RColorBrewer colors. First, we’ll define the bins. This is a numeric vector that defines the boundaries between intervals ( (0,10], (10,20], and so on).
How do I generate labels for a leaflet?
We’ll generate the labels by handcrafting some HTML, and passing it to lapply (htmltools::HTML) so that Leaflet knows to treat each label as HTML instead of as plain text. We’ll also set some label options to improve the style of the label element itself.
How to create a population density leaflet in JavaScript?
See this example stand-alone. We’ll be creating a visualization of population density per US state. As the amount of data (state shapes and the density value for each state) is not very big, the most convenient and simple way to store and then display it is GeoJSON.