Get startedGet started for free

Choropleth maps

1. Choropleth maps

Now that you have explored a few aspects of the election datasets using familiar tools, let's consider a new approach: mapping the results.

2. Voter turnout example

Arguably, the most common way to display election results is with a choropleth map. Choropleth maps are created by coloring regions of the map to display a variable of interest. In this example, we are using color to display the voter turnout in the United States. From this map, it is easy to see that Minnesota had the highest turnout in the 2018 midterm election.

3. Population density example

Choropleth maps are used for more than election results. For example, this map displays population density per square kilometer. This display allows the high-density regions, such as Southeast Asia, to quickly pop out at the reader.

4. Choropleth maps in plotly

Now that you know how to read a choropleth map, let's dig into their construction. plotly supports native choropleth maps which we'll explore in this lesson to recreate the voter turnout map. First, recall that the turnout dataset contains columns for the full state name, the state abbreviation, and the turnout in 2018.

5. Choropleth maps in plotly

To create the voter turnout map we begin by piping in the dataset. Next, we create the plotting canvas using plot_geo() rather than plot_ly(). plot_geo() maps longitude and latitude to the x- and y-aesthetics, respectively. Within plot_geo() we specify that we wish to map the United States by adding locationmode = 'USA-States'. We'll explore locationmode in more detail soon. Once we have the plotting canvas, we add filled polygons representing the states using add_trace(). To do this, we map turnout to z and the state abbreviations to locations. Note that z specifies the variable used to color the polygons, and locations is the variable defining the regions of the choropleth map. Finally, we can pass a list of mapping options to the geo argument of layout(). Here we restrict the map only to the United States by specifying scope = 'usa'.

6. Limitation of plot_geo()

While plot_geo() is convenient, it's important to note that not all world regions are available in plotly via plot_geo(). Currently, you are only able to define regions by US State, ISO-3 country code, or country name. If this doesn't match your needs, then stay tuned for the next lesson where you'll see how to define your on polygons on choropleth maps.

7. Mapping options

As you already saw, mapping options can be passed to the geo layout. There are too many options to go over now, but common options include: Restricting the scope of the map, such as focusing only on Asia or North America. Adjusting the projection of the map. There are 22 map projections supported by plotly, including commonly used projections such as conic conformal, mercator, and stereographic. Setting the scale of the map to zoom in or out on the map view. Scale = 1 fits the range of lat and long in the dataset, and larger values result in tighter zoom. Setting the center point of the map. This is especially useful in tandem with scale, to have fine control over the map when you zoom in. Combining these geo layouts will help you polish your maps, and are useful for more than just choropleth maps, so be sure to explore them!

8. Let's get mapping!

Now that you know how to create a choropleth map of the United States, let's use these skills to explore a few more aspects of the 2018 midterm election results.