One way to show data distribution on a map is with a choropleth, a thematic map in which areas are shaded based on a particular value. In this guide, you will use Mapbox Studio and Mapbox GL JS to make a map of US states showing population density. (If you are familiar with Leaflet, this map may look familiar!)
This tutorial is part of a series. In this part, Part 1: Create a style, you will walk through creating a map with a choropleth style.
The next section, Part 2: Add Interactivity, you will walk through adding interactivity to your map, such as hovering on regions to view their data.
To follow along with this guide you'll need:
GeoJSON
file, which includes spatial data on population density across US states, by clicking the download button below.
The following dependencies are not needed for this part of the tutorial, but will be necessary for Part 2: Add Interactivity.
To add the population density data to a style in Mapbox Studio, you need to upload it to your account. Follow these steps to upload the data:
GeoJSON
, which includes spatial data on population density across US states by clicking the download button below.stateData.geojson
you downloaded.Now you will be able to inspect the data as seen in the next step.
When you upload vector data to your Mapbox account, our servers convert it to a vector tileset so it can be rendered quickly and efficiently in the Mapbox Studio style editor and with Mapbox GL JS. The Tileset explorer shows some useful information about the tileset that was created from your uploaded data:
GeoJSON
file: density and name. The uploaded tileset maintains the properties from the original data file, which you can use when adding style rules for the tileset.Now that you've inspected your dataset, you can move onto creating a style in the next step.
To put your data on a map, you'll first need to create a style that will reference the data. To create a style, follow these steps:
Excellent! Welcome to the Mapbox Studio style editor. This is where you will make changes to your new map style.
If you would like to learn more about the style editor, read the Mapbox Studio Manual.
Now you will add and style the population density data, by adding a new layer to the map:
statedata
source you created.statedata
.Your new layer will now be highlighted on the x-ray map.
Click the Style tab and the map will switch back to style mode displaying your new layer. You will see the state data on the map with a default style (black with 100% opacity).
On the original Leaflet map, the data is styled based on the population density of each state:
In the Mapbox Studio style editor, you can assign a color to each state based on its population density by following these steps:
statedata
layer and make sure you're on the Style tab.density
.
Now you can start breaking down the population density into ranges in the next step.
Now you can add stops and colors to break states up into groups with similar population densities. By default this will start with two stops (the max and min values of the data), but you can add more stops between them.
HSL
dropdown and select HEX
.#FFEDA0
into the color value.10
.#FFEDA0
as well.20
: #FED976
50
: #FEB24C
100
: #FD8D3C
200
: #FC4E2A
500
: #E31A1C
1000
: #BD0026
0-10
will be assigned the color #FFEDA0
, all states with population density from 10-20
will be assigned the color #FED976
, and so on.0.6
.1px stroke
style property to #FFFFFF
.Now that your data is colored, you can add labels to your data in the next step.
After you've created and styled your layer, your map should look something like this:
Everything looks good, except for one thing - the labels are under the data layer. The Mapbox Studio style editor allows you to reorder layers on the map and put country labels, city labels , state labels, etc., on top of your data by following these steps:
Drag your statedata
layer below the Place labels, place labels group of layers.
Now that the labels are on top of the population density data, when you zoom in, city labels are appearing above the more important state labels making the view noisy. To clean up the map, you can turn off the city labels like so:
You can change the projection of your map to several options, including Mercator, which will show a flat map as opposed to a globe.
To change the map's projection, follow these steps:
Your map will now appear as a flat, 2D map in the Mercator style.
Now that you have your map looking good, it's time to publish! Click the Publish button in the top right of the screen, then click Publish again on the next prompt.
Your style is now accessible and can be used in your applications or websites! If you go back to your Styles page, you will see your new style at the top of the list.
Congratulation! You've finished Part 1 of this tutorial series.
Head to part 2 to learn how add interactive elements to your map and publish it to the web with Mapbox GL JS.