Data - The Shapefile
The shapefile is the most popular form of vector spatial data. Technically, it refers only to the .shp file extension (which contains the actual points or polygons), but it typically refers to the whole collection of the .shp and related files. When you download a shapefile package and open it up, you'll often see a few files like so:
The .shp file is a collection of either points, polygons, or polylines. Points can be polling places, polygons can be precincts, and polylines can be rivers. In creating choropleth maps you will most likely be using polygon shapefiles. The data about those polygons is contained in the .dbf file that is always bundled with the .shp.
Before you manipulate the DBF file, I recommend creating a backup. Open the .dbf in Excel. It'll look like this:
Every DBF will be different, but most follow a similar structure. Each row corresponds to one of the polygons in the shapefile.
Area and Perimeter - These are generated when the shapefile is generated and are irrelevant.
CO25_D00 and CO25_D00_I - These are sequentially numbered columns that tell you what order the shapefile rows are in. These are VERY IMPORTANT. IF YOUR DBF DOES NOT HAVE A SEQUENTIALLY NUMBERED COLUMN, YOU WILL NEED TO CREATE ONE. In a new column, type "1" in the first row and go to Edit -> Fill -> Series to create a sequentially numbered column. Sortation order in the corresponding .shp file is ABSOLUTE, so you MUST place the DBF rows in the original order before you save it!
State - When working with government-issued shapefiles, we will often encounter FIPS codes. Each geographic unit in this country is assigned a FIPS code-"25" is the FIPS code for Massachusetts and "51" is for Virginia.
County - FIPS code for the county.
Name - County name.
LSAD - Geographic type identifier-06 is county.
LSAD_TRANS - Explains the LSAD column.
Now we sort the DBF file by county name so we can paste in the election margins. If you are working with percentages, it is important you leave them in decimal format. I subtracted the Republican results from the Democratic results--Democrat is positive, Republican is negative:
Notice how some of the counties in the DBF are duplicated-in this example, Bristol, Dukes, and Norfolk. Islands are their own polygon, so they get their own row-single counties can comprise of multiple polygons. You will have to duplicate that data like so:
Delete the duplicate names column and sort the file by the sequence column:
Save this file as a CSV file. .dbf is a very finicky format, and if you save directly to a DBF using Excel your edits will be lost (don't ask why, I don't know). Close the file.
Open the CSV, resize the columns (if your columns are too narrow, it will truncate the columns in the DBF). Make sure your data column is in a consistent format:
Save this file as a DBF. Overwrite the old file-it is very important your DBF and SHP have 1) the same name and 2) are in the same directory. Now we are ready to map.
Map - Creating the Choropleth
Open qGIS and add a layer.
Each shapefile serves as its own "layer"-you can overlay them on top of each other. For example, if you are mapping by precinct you can overlay congressional district boundaries. We'll keep this example simple and have only one layer.
Hit "Browse" in the popup dialog and find your .shp file. Bam! Massachusetts!
This, however, is boring. Go to the layers panel on the left, right-click the layer and go to Properties, and go to the Symbology tab. Under "Legend Type", select "Graduated Symbol".
Under "Classification Field", select what you called your data column, and under number of classes input the number of colors you want. Then hit "Classify."
Double-click each class on the left to change its boundaries. Since this is a margin column, negative numbers are Republican counties and positive numbers are Democratic counties. Let's split them up by counties won by party, and then whether the county was won by 10 or more points. Clicking on "Fill color" will give you a panel of colors to choose from for each class. Don't forget your data is in decimal format! Hit "OK".
And you'll see this:
Go to File - > Save as image, and you'll have an image you can import into your chosen image editor before you share your creation with the world.
Questions? Email me. If I have time I will try to help you-writing instruction guides isn't my strong suit, so you are encouraged to leave a comment here or email me with suggestions. We have only scratched the surface of what we can do with this very simple example. Comment or email with your experiences and what you've put out.
Coming up later: 3-D maps using Earth, and other fun tricks for refining your maps. Stay tuned! |