Purple America Solved

35.00 $

Click Category Button to View Your Next Assignment | Homework

You'll get a download link with a: . zip solution files instantly, after Payment


5/5 - (1 vote)

Write a program to visualize U.S. presidential election results.

Historical context. During coverage of the 2000 presidential election, Tim Russert coined the political terms red states and blue states to refer to states that predominantly vote for the Republican presidential candidate (red) or the Democratic presidential candidate (blue). The news media use red-state blue-state maps, such as the one below, to display election results.

Tim Russert, red-states, blue-states

The problem. On this assignment, you will create more refined (and less polarizing) choropleth maps by mashing up two sources of data: geographic boundary data and election return data.

Geographic data. We supply geographic data (sourced from the U.S. Census) that describes the boundary of each state and county in the United States.

  • The first line consists of four real numbers, representing the bounding box of the region. The first two numbers are the minimum longitude and latitude values; the second two numbers are the maximum longitude and latitude values.
  • The next line is an integer that specifies the number of subregions.
  • There is one block for each subregion (with a blank line separates blocks):
    • The first line of a block is an integer N that specifies the number of points in the polygon describing the subregion.
    • The second line of a block is a string that is the name of the subregion.
    • The third line of a block is a string that is the name of the region.
    • The remaining N lines of the block describe the polygonal boundary, given as N pairs of real numbers, representing the longitude and latitude coordinates.
% more USA.txt                    
-124.731216   24.544102
 -66.980385   49.384365

 -88.200027   34.995548
 -88.202919   35.007942
 -87.984886   35.005848
 -88.153519   34.921185
 -88.176064   34.962433
 -88.187088   34.974182


-111.048203   44.474144
-111.054558   44.666336
-111.054420   45.001392
-111.043846   43.315800
-111.044724   43.501213
-111.046272   43.983456
% more NJ.txt                      
 -75.560143   38.928589
 -73.894402   41.357330

 -74.877563   39.608414
 -74.736694   39.729721
 -74.676102   39.691162
 -74.857353   39.420528
 -74.856087   39.424465
 -74.985443   39.514725


 -75.120819   40.968208
 -75.122986   40.970055
 -75.131744   40.969185
 -75.095901   40.924057
 -75.112061   40.948017
 -75.118141   40.952927
% more USA-county.txt
-124.731216   24.544102
 -66.980385   49.384365

 -86.916969   32.664028
 -86.816589   32.659988
 -86.713409   32.661602
 -86.916809   32.649662
 -86.917458   32.653877
 -86.921387   32.655415


-105.078743   44.176205
-104.375000   44.181641
-104.054001   44.180401
-105.081238   43.592144
-105.078255   43.827049
-105.080872   43.826954

We note that the number of subregions in USA.txt is not 50 for two reasons: first, we do not include either Alaska or Hawaii; second, we include an entry for each polygonal subregion—some states (such as Michigan, Florida, and California) comprise several polygonal subregions.

Election return data. We supply election return data (sourced from Dave Leip’s Atlas of U.S. Presidential Elections) that describes the results for each presidential election, by state and county. Each row consists of four fields, separated by commas: the name of a subregion, the number of votes for the Republican candidate, the number of votes for the Democratic candidate, and the number of votes for the Independent (or third party) candidate.

% more USA2012.txt                
West Virginia,417655,238269,14743,
% more NJ2012.txt                  
Cape May,25781,21657,655,

You can download all of the geometric and election return data files collectively as purple-america-data.zip.

Part 1. Write a program White.java that takes the name of a region as the command-line argument and produces an outline map, as in the examples below:

% java White USA

Red-States, Blue-States

% java White NJ          

Red-Counties, Blue-Counties

% java White USA-county

Red-Counties, Blue-Counties

For simplicity, draw the point with longitude x and latitude y at location (xy) in the plane. Use the bounding box of the region to determine the part of the plane to display in the drawing window and rescale the coordinates accordingly.

Part 2. Write a program RedBlue.java that takes two command-line arguments (the name of the region and the year of the election) and produces a red-state blue-state map, as in the examples below:

% java RedBlue USA 2012

Red-States, Blue-States

% java RedBlue NJ 2012   

Red-Counties, Blue-Counties

% java RedBlue USA-county 2012

Red-States, Blue-States

Part 3. A more refined visualization reveals that the United States is not as polarized by geography as suggested in the above visualizations. In 2000, Bob Vanderbei created a Purple America map, in which each region is colored in a shade of red, green, and blue, according to the proportion of votes received by each candidate. Specifically, if the Republican, Independent, and Democratic candidates receive a1a2, and a3 votes, respectively, then we draw the subregion using the following formula:

Shade of red, green, and blue
Write a program Purple.java that takes two command-line arguments (the name of the map and the year of the election) and produces a Purple-America map, as in the examples below:

% java Purple USA

Purple States

% java Purple NJ         

Purple Counties

% java Purple USA-county

Purple America

Extra credit. There are limitless opportunities for creativity, enrichment, and inspiration.

  • Write a program to screen scrape the election return data from Dave Leip’s Atlas of U.S. Presidential Elections. Pay careful attention to name clashes between Dave Leip’s site and the U.S. Census (e.g., LaSalle vs. La Salle, Kings County vs. Brooklyn).
  • Modify your program to include Hawaii and Alaska.
  • Use a map projection (such as Mercator, azimuthal, Albers, or Gall-Peters) to transform longitude and latitude coordinates into points in the plane.
  • Explore a different color palette (with 5-7 color categories) for coloring the subregions. Here is one example.
  • Write the state name in the appropriate place. For large states, draw it in the centroid of the polygon describing the state.
  • Create an interactive GUI which displays the election returns for a county as the user hovers over it. You will probably need to add a method to your polygon data type to determine whether a point is inside the polygon.
  • Visualize the gradient or change in votes from one election to the next.
  • Visualize a different data set by county, e.g., poverty rate, access to Internet, and average price of health care. Or collect data for elections in another country.

This assignment was created by Kevin Wayne, with inspiration from Bob Vanderbei.

Copyright © 2007–2014.
  • PurpleAmerica.zip