Map Election Results
This tutorial describes the basic functionality for connecting to a dataset and building a thematic map of electoral data, using the following workflow:
- Locate electoral data and connect the dataset to CARTO
- Visualize data from the dataset. For this tutorial, the average democrat share
- Customize a thematic map of results
- Apply advanced styling of your electoral data using CartoCSS
Locating Electoral Data
For this tutorial, we are going to build a map with data from the the Harvard Election Data Archive website, which contains data from United States elections. In particular:
- In the Search my dataverse… input field, search for New Hampshire Data Files
- Download the
nh_shapefile.zipdataset from the results list
To understand the data, it is often helpful to view its metadata. In this case, go back to the Harvard Election Data Archive, search for and download the
NH-Notes.rtf file, and open the metadata with a text-editor. The metadata describes all the columns and information contained in the downloaded file. For example, the column
p_08 is the Democratic vote share for the presidential race in 2008. This is what we will use to create an interesting map.
Map the Average Democrat Share
nh_shapefile.zip file is downloaded, connect the dataset to CARTO. When the dataset is successfully imported, it displays in the DATA VIEW. Scroll across the Data View and note the column
p_08, that we identified as the column that contains the Democratic vote share.
Click on MAP VIEW to visualize the data. You will see basic outlines of the New Hampshire counties. Change the Basemap.
Thematic Map of Results
Create a choropleth map of this data.
- Click the wizards option from the CARTO sidebar
- Select CHOROPLETH as the thematic map type
- Change the visualized Column data to display
5 Bucketsfor the Buckets option
- Optionally, change the color ramp. Note how your may styling is changing appearancef
Customizing Thematic Maps
Although CARTO makes it simple to create thematic maps, we also give you the flexibility to customize styles for select data. The map we have created looks okay, but a linear color ramp is not normal for this type of data. The electoral data is showing results greater than 0.5 (from the
p_08 column) as primarily Democrat votes. Data containing less than 0.5, indicates mostly Republican votes. You can apply a diverging scale (using Red/Blue) to represent the more familiar U.S. electoral party colors.
Tip: A recommended source for basic color scales, and good mapping colors, is Color Brewer 2. For example, select a diverging ramp of 5 colors from red to blue,
Manually Editing CartoCSS
You can apply advanced CartoCSS styling with the CartoCSS Editor. CartoCSS colors are coded in hexidecimal, so
#FFFFFF is white,
#000000 is black, and so on. For the purposes of this tutorial, we will apply the five new colors we identified from ColorBrewer (in the previous section). You can copy and paste hexidecimal colors into the CartoCSS Editor. Click Apply Style to view the results.
Note: We have incorrect bins. Notice that our colors do not include the far ends of the spectrum, near 1. Edit the bins to be
<=1, <=0.6, <=0.52, <=0.48 and <=0.4 (most red), and click Apply Style.
The data is not perfect yet. Note that the white, in the areas that are split, is too bright compared to the rest. Change the color after
Great, now you have a nice looking election map!