1. Data Management

##   fips state_po county_name demovotes repvotes     winner totalvotes
## 1 1001       AL     AUTAUGA      7503    19838 REPUBLICAN      27770
## 2 1003       AL     BALDWIN     24578    83544 REPUBLICAN     109679
## 3 1005       AL     BARBOUR      4816     5622 REPUBLICAN      10518
## 4 1007       AL        BIBB      1986     7525 REPUBLICAN       9595
## 5 1009       AL      BLOUNT      2640    24711 REPUBLICAN      27588
## 6 1011       AL     BULLOCK      3446     1146   DEMOCRAT       4613
##   percentvotes         county state       lon      lat rMapState rMapCounty
## 1     71.43680 Autauga County    AL -86.64565 32.54009   alabama    autauga
## 2     76.17137 Baldwin County    AL -87.72627 30.73831   alabama    baldwin
## 3     53.45123 Barbour County    AL -85.39733 31.87403   alabama    barbour
## 4     78.42626    Bibb County    AL -87.12526 32.99902   alabama       bibb
## 5     89.57155  Blount County    AL -86.56271 33.99044   alabama     blount
## 6     74.70193 Bullock County    AL -85.71680 32.10634   alabama    bullock

The data has been extracted for 2020 year to include the democratic and Republican Parties. The total votes per county by each of the winner is calculated and is shown in the PercentVotes column.We are also calculating the Winner for each of the county. We see that in 508 counties the winner party was Democrat . However Republican was winner in 2566 counties.

Create an interactive choropleth map to display the presidential election results at county level using two different colors to represent the two parties. You can choose any R library to accomplish this part of the assignment.

#library(plotly)
#library(plotly)
#library(rjson)
#library("RColorBrewer")  # brewer.pal.info for list of color scales

url <- "https://github.com/pengdsci/sta553/raw/main/data/geojson-counties-fips.json"  # contains geocode to define county boundaries in the choropleth map
counties <- rjson::fromJSON(file=url)  

# geo styling
g <- list(      scope = 'usa',
           projection = list(type = 'albers usa'),
             showland = TRUE,
 landcolor="white",
   countrywidth = 0.5,
         subunitwidth = 0.5
      
       )
###
fig <- plot_geo(df, lat = ~lat, lon = ~lon) %>% 
  add_markers( text = ~paste(county_name, winner,
                             paste("Winner:", winner), 
                             sep = "<br>"),
            color=~winner,
              symbol = "circle", 
              size = ~percentvotes , 
             colorscale = "RnBu",  
              hoverinfo = "text")   %>% 
  colorbar(title = "Parties")  %>% 
  layout( title = '2020 Presidential election Results', 
          geo = g )
          
fig
#library(plotly)
#library(rjson)
#library("RColorBrewer")  # brewer.pal.info for list of color scales

Another representation using Choropleth Map:

fig <- plot_ly()  %>% 
  add_trace( type = "choropleth",
          geojson = counties,
        locations = df$county_fips,
                z = df$percentvotes,
           colorscale = "TealGrn",   
             zmin = 0,
             zmax = 100,
         text = ~paste("<br>County: ", df$county_name,
                           "<br>Party won: ", df$winner,
                           "<br>Winner votes percent: ", df$percentvotes,
                           "<br>Total votes: ", df$totalvotes,
                           "<br>State: ", df$state_po),

        hoverinfo = "text",
           marker = list(line=list(width=0.3))) %>% 
  colorbar(title = "Winner Votes percent(%)") %>% 
  layout( title = list(text = "<b>US Presidential elections 2020 by County</b>",
                       font = list(size = 20,
                                   color = "darkred")),
          margin = list( b = 15, l = 25, t = 85, r = 25),
          geo = g)
fig

An interesting thing to note is that this view is even more heavily dominated by the Republican. Less densely populated counties tend to vote republican, while higher density, typically smaller counties tend to vote for democrats. The result is interesting because overall the winner was Democrat Party.

Using Tableau to create the map

  • The link for the visualization created in Tableau public is: click here