Get startedGet started for free

From polygons to maps

1. From polygons to maps

In the last lesson you were introduced to choropleth maps using plotly's built-in options, but not all regions are available. In this lesson, you'll learn how to build a choropleth map from scratch, allowing you to create choropleth maps for any region that has defined boundaries.

2. Boundaries

To use externally-defined boundaries you'll need to create a data frame suitable for use with plotly. For example, the data frame us_states contains the necessary boundaries to recreate the choropleth map of voter turnout from the previous lesson. us_states contains the lat/long coordinates used to trace the boundaries of each state. Additionally, the data frame contains a few more key variables: group specifies the order to plot the state polygons, order specifies the order that the coordinates for each state boundary should be connected, and region contains a meaningful group ID for each state that can be used to combine these boundaries with the voter turnout data.

3. Joining data frames

Now that we have the state boundaries in us_states, we need to add a column containing the voter turnout. To achieve this, we merge the us_states and turnout by state name. However, there are two complications: first, state names are capitalized in turnout but not in us_states; second, the column names defining the state names are not named identically.

4. Joining data frames

To address the first issue, we use mutate and tolower() to transform the state names to lowercase. To merge the datasets we use left_join() from dplyr. Here, we specify that we are adding the data contained in turnout, the dataset on the right, to us_states, the dataset on the left. Since the two datasets use different column names we pass a vector mapping the column on the left to the column on the right, "region" = "state". The resulting data frame has the same number of rows as us_states, but with the additional information contained in turnout, which is exactly what we wanted to see.

5. Creating the map

Now that we have a dataset containing both the geographic boundaries and the variable we want to plot, we can start mapping. To begin, we group the states_map data frame by group, so that the polygon for each state is treated as a separate entity. Next, we use plot_ly() to define the canvas. Here, we map long to x, lat to y, turnout2018 to color, and region to split. When using polygons, color corresponds to the fill color. Also, we use the split aesthetic parameter to specify that a unique polygon should be drawn for each state. Finally, we pipe the canvas into add_polygons, and reduce the width of the boundary lines by adding line = list(width = 0-point-4), and hide the legend by adding showlegend = FALSE. Why do we hide the legend? Because plotly adds both a colorbar for turnout and a legend specifying the color for each state. Hiding the legend removes this redundant information.

6. The unpolished choropleth map

Here's the result. Overall, things look very similar to our previous choropleth map, with the omission of Hawaii and Alaska. To finish polishing the map we need to remove the x- and y-axis labels, tick marks, and grid without losing information.

7. Polishing your map

To remove the axis label, tick marks, and grid from each axis we pass the xaxis and yaxis arguments a list with title equals an empty string, showgrid = FALSE, zeroline = FALSE, and showticklabels = FALSE. Finally, we can polish the colorbar title by adding the layer colorbar(title = "Turnout").

8. The polished map

The result now looks like a proper choropleth map.

9. Let's practice!

Now, you'll practice these skills by creating a choropleth map displaying the Senate results nationally and by county Florida.