Mapping & Web-scraping the 2016 Presidential Election Results in Connecticut

[This article was first published on Jasmine Dumas' R Blog, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Understanding my Neighbors with R and Leaflet –

I, like many others across the country have been in total shock and mental disarray at the results from the election this past Tuesday. I’m not sure what to make of my fellow Americans voting for an outright racist, sexist, homophobic and xenophobic egomaniac and what that will mean for the United States and frankly the World.

This was in response to David Smith’s original tweet about election forecasting

From the initial announcement of Donald Drumpf gaining the magic 270 electoral votes, I have been curious about the election data and eager to explore how my home state fared at the county level. I found a great visualization blog post from fellow useR, Julia Silge of her home state Utah which gave me ideas and starter code and county-level election data from Mike Kearny!

Here is the analysis and approaches that I took:

<span class="n">library</span><span class="p">(</span><span class="n">readr</span><span class="p">)</span><span class="w">
</span><span class="n">library</span><span class="p">(</span><span class="n">dplyr</span><span class="p">)</span><span class="w">
</span><span class="n">library</span><span class="p">(</span><span class="n">leaflet</span><span class="p">)</span><span class="w">
</span><span class="n">library</span><span class="p">(</span><span class="n">rvest</span><span class="p">)</span><span class="w">
</span><span class="n">library</span><span class="p">(</span><span class="n">ggplot2</span><span class="p">)</span><span class="w">
</span><span class="n">library</span><span class="p">(</span><span class="n">rgdal</span><span class="p">)</span><span class="w">
</span><span class="n">library</span><span class="p">(</span><span class="n">tidyr</span><span class="p">)</span><span class="w">
</span><span class="n">all_results</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">read_csv</span><span class="p">(</span><span class="s2">"https://raw.githubusercontent.com/mkearney/presidential_election_county_results_2016/master/pres16results.csv"</span><span class="p">)</span><span class="w">

</span><span class="c1"># what is in here?
</span><span class="n">head</span><span class="p">(</span><span class="n">all_results</span><span class="p">)</span><span class="w">
</span>
## # A tibble: 6 × 8
##      X1 cand_id       cand_name    votes     total state  fips         pct
##   <int>   <chr>           <chr>    <int>     <int> <chr> <chr>       <dbl>
## 1     1  US8639    Donald Drumpf 59821874 126061003    US    US 0.474547025
## 2     2  US1746 Hillary Clinton 60122876 126061003    US    US 0.476934774
## 3     3 US31708    Gary Johnson  4087972 126061003    US    US 0.032428522
## 4     4   US895      Jill Stein  1223828 126061003    US    US 0.009708220
## 5     5 US65775   Evan McMullin   425991 126061003    US    US 0.003379245
## 6     6 US59414  Darrell Castle   175956 126061003    US    US 0.001395800

I ended up not doing as much exploratory data analysis as intended. The data set is coded with FIPS numbers. FIPS state codes are numeric codes defined in U.S. Federal Information Processing Standard Publication to identify U.S. states. Connecticut FIPS codes start with 09 – so I grabbed those out from the entire data set to explore.

<span class="n">head</span><span class="p">(</span><span class="n">levels</span><span class="p">(</span><span class="n">factor</span><span class="p">(</span><span class="n">all_results</span><span class="o">$</span><span class="n">fips</span><span class="p">)))</span><span class="w">
</span>
## [1] "01001" "01003" "01005" "01007" "01009" "01011"
<span class="n">ct_results</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">all_results</span><span class="p">[</span><span class="n">grep</span><span class="p">(</span><span class="s2">"^09"</span><span class="p">,</span><span class="w"> </span><span class="n">all_results</span><span class="o">$</span><span class="n">fips</span><span class="p">),</span><span class="w"> </span><span class="p">]</span><span class="w">
</span><span class="n">head</span><span class="p">(</span><span class="n">ct_results</span><span class="p">)</span><span class="w">
</span>
## # A tibble: 6 × 8
##      X1 cand_id       cand_name  votes  total state  fips        pct
##   <int>   <chr>           <chr>  <int>  <int> <chr> <chr>      <dbl>
## 1  2718 CT20519 Hillary Clinton  38767  77380    CT 09013 0.50099509
## 2  2719 CT20520    Donald Drumpf  33983  77380    CT 09013 0.43917033
## 3  2720 CT20553    Gary Johnson   3179  77380    CT 09013 0.04108297
## 4  2721 CT20552      Jill Stein   1451  77380    CT 09013 0.01875162
## 5  2722 CT20519 Hillary Clinton 201578 372277    CT 09009 0.54147315
## 6  2723 CT20520    Donald Drumpf 157067 372277    CT 09009 0.42190895

To make things more readable, I added the county names to the data.frame and since Connecticut only has 8 counties, I can complete this step in an iterative approach by matching up the fips numbers.

<span class="c1"># add the county names (8 in CT)
</span><span class="n">ct_results</span><span class="o">$</span><span class="n">county</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="kc">NA</span><span class="w">
</span><span class="c1"># these are the full fips numbers associated with each county
</span><span class="n">fairfield</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="s2">"09001"</span><span class="w"> 
</span><span class="n">hartford</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="s2">"09003"</span><span class="w">
</span><span class="n">litchfield</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="s2">"09005"</span><span class="w">
</span><span class="n">middlesex</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="s2">"09007"</span><span class="w">
</span><span class="n">newhaven</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="s2">"09009"</span><span class="w">
</span><span class="n">newlondon</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="s2">"09011"</span><span class="w">
</span><span class="n">tolland</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="s2">"09013"</span><span class="w">
</span><span class="n">windham</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="s2">"09015"</span><span class="w">

</span><span class="n">ct_results</span><span class="o">$</span><span class="n">county</span><span class="p">[</span><span class="n">which</span><span class="p">(</span><span class="n">ct_results</span><span class="o">$</span><span class="n">fips</span><span class="w"> </span><span class="o">%in%</span><span class="w"> </span><span class="n">fairfield</span><span class="p">)]</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="s2">"Fairfield"</span><span class="w">
</span><span class="n">ct_results</span><span class="o">$</span><span class="n">county</span><span class="p">[</span><span class="n">which</span><span class="p">(</span><span class="n">ct_results</span><span class="o">$</span><span class="n">fips</span><span class="w"> </span><span class="o">%in%</span><span class="w"> </span><span class="n">hartford</span><span class="p">)]</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="s2">"Hartford"</span><span class="w">
</span><span class="n">ct_results</span><span class="o">$</span><span class="n">county</span><span class="p">[</span><span class="n">which</span><span class="p">(</span><span class="n">ct_results</span><span class="o">$</span><span class="n">fips</span><span class="w"> </span><span class="o">%in%</span><span class="w"> </span><span class="n">litchfield</span><span class="p">)]</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="s2">"Litchfield"</span><span class="w">
</span><span class="n">ct_results</span><span class="o">$</span><span class="n">county</span><span class="p">[</span><span class="n">which</span><span class="p">(</span><span class="n">ct_results</span><span class="o">$</span><span class="n">fips</span><span class="w"> </span><span class="o">%in%</span><span class="w"> </span><span class="n">middlesex</span><span class="p">)]</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="s2">"Middlesex"</span><span class="w">
</span><span class="n">ct_results</span><span class="o">$</span><span class="n">county</span><span class="p">[</span><span class="n">which</span><span class="p">(</span><span class="n">ct_results</span><span class="o">$</span><span class="n">fips</span><span class="w"> </span><span class="o">%in%</span><span class="w"> </span><span class="n">newhaven</span><span class="p">)]</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="s2">"New Haven"</span><span class="w">
</span><span class="n">ct_results</span><span class="o">$</span><span class="n">county</span><span class="p">[</span><span class="n">which</span><span class="p">(</span><span class="n">ct_results</span><span class="o">$</span><span class="n">fips</span><span class="w"> </span><span class="o">%in%</span><span class="w"> </span><span class="n">newlondon</span><span class="p">)]</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="s2">"New London"</span><span class="w">
</span><span class="n">ct_results</span><span class="o">$</span><span class="n">county</span><span class="p">[</span><span class="n">which</span><span class="p">(</span><span class="n">ct_results</span><span class="o">$</span><span class="n">fips</span><span class="w"> </span><span class="o">%in%</span><span class="w"> </span><span class="n">tolland</span><span class="p">)]</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="s2">"Tolland"</span><span class="w">
</span><span class="n">ct_results</span><span class="o">$</span><span class="n">county</span><span class="p">[</span><span class="n">which</span><span class="p">(</span><span class="n">ct_results</span><span class="o">$</span><span class="n">fips</span><span class="w"> </span><span class="o">%in%</span><span class="w"> </span><span class="n">windham</span><span class="p">)]</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="s2">"Windham"</span><span class="w">
</span>

Here is a bar chart using ggplot2 of the election results by candidate, votes and county. I may add to this section later!

<span class="n">ggplot</span><span class="p">(</span><span class="n">ct_results</span><span class="p">,</span><span class="w"> </span><span class="n">aes</span><span class="p">(</span><span class="n">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">cand_name</span><span class="p">,</span><span class="w"> </span><span class="n">y</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">votes</span><span class="p">,</span><span class="w"> </span><span class="n">fill</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">county</span><span class="p">))</span><span class="w"> </span><span class="o">+</span><span class="w">
  </span><span class="n">geom_bar</span><span class="p">(</span><span class="n">stat</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"identity"</span><span class="p">)</span><span class="w">
</span>

plot of chunk unnamed-chunk-4

Shape files by county were downloaded with rgdal.

<span class="c1"># CT level county shapefiles for 2015
</span><span class="n">tmp2</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">tempdir</span><span class="p">()</span><span class="w">
</span><span class="n">url2</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"http://www2.census.gov/geo/tiger/GENZ2015/shp/cb_2015_us_county_500k.zip"</span><span class="w">
</span><span class="n">file</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">basename</span><span class="p">(</span><span class="n">url2</span><span class="p">)</span><span class="w">
</span><span class="n">download.file</span><span class="p">(</span><span class="n">url2</span><span class="p">,</span><span class="w"> </span><span class="n">file</span><span class="p">)</span><span class="w">
</span><span class="n">unzip</span><span class="p">(</span><span class="n">file</span><span class="p">,</span><span class="w"> </span><span class="n">exdir</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">tmp2</span><span class="p">)</span><span class="w">
</span><span class="n">ct_shp</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">readOGR</span><span class="p">(</span><span class="n">dsn</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">tmp2</span><span class="p">,</span><span class="w">
                  </span><span class="n">layer</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"cb_2015_us_county_500k"</span><span class="p">,</span><span class="w"> </span><span class="n">encoding</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"UTF-8"</span><span class="p">)</span><span class="w">
</span>
## OGR data source with driver: ESRI Shapefile 
## Source: "/var/folders/nv/c92g05zj4tnbkwp_x6y9ly6w0000gn/T//RtmpDG6lwP", layer: "cb_2015_us_county_500k"
## with 3233 features
## It has 9 fields
<span class="nf">dim</span><span class="p">(</span><span class="n">ct_shp</span><span class="p">)</span><span class="w">
</span>
## [1] 3233    9
<span class="c1"># fix the FIPS number in the shape file for merging
</span><span class="n">ct_shp</span><span class="o">@</span><span class="n">data</span><span class="o">$</span><span class="n">fips</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">paste0</span><span class="p">(</span><span class="n">ct_shp</span><span class="o">@</span><span class="n">data</span><span class="o">$</span><span class="n">GEOID</span><span class="p">)</span><span class="w">
</span>

This election was fueled by discontent and ignorance of the “silent majority” around social and economic policies, so I thought it was worthwhile to gather some socio-economic data from https://datausa.io/ to enhance the election data. Data USA is a data viz tool that uses open public data from https://www.data.gov/ to share insights on occupations, industries and education. This data was web-scraped using rvest as the aggregated statistics by county are nestled in a web page.

<span class="c1"># create a empty df
</span><span class="n">ct_econ</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">data.frame</span><span class="p">(</span><span class="n">matrix</span><span class="p">(</span><span class="n">ncol</span><span class="o">=</span><span class="m">10</span><span class="p">,</span><span class="w"> </span><span class="n">nrow</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">0</span><span class="p">))</span><span class="w">

</span><span class="c1"># add some economic data from https://datausa.io/ for each county by web-scrapping
</span><span class="n">county_urls</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="nf">c</span><span class="p">(</span><span class="s2">"https://datausa.io/profile/geo/fairfield-county-ct/"</span><span class="p">,</span><span class="w"> 
                 </span><span class="s2">"https://datausa.io/profile/geo/hartford-county-ct/"</span><span class="p">,</span><span class="w"> 
                 </span><span class="s2">"https://datausa.io/profile/geo/litchfield-county-ct/"</span><span class="p">,</span><span class="w"> 
                 </span><span class="s2">"https://datausa.io/profile/geo/middlesex-county-ct/"</span><span class="p">,</span><span class="w"> 
                 </span><span class="s2">"https://datausa.io/profile/geo/new-haven-county-ct/"</span><span class="p">,</span><span class="w"> 
                 </span><span class="s2">"https://datausa.io/profile/geo/new-london-county-ct/"</span><span class="p">,</span><span class="w"> 
                 </span><span class="s2">"https://datausa.io/profile/geo/tolland-county-ct/"</span><span class="p">,</span><span class="w"> 
                 </span><span class="s2">"https://datausa.io/profile/geo/windham-county-ct/"</span><span class="p">)</span><span class="w">
</span><span class="c1">#  for loop for each of the counties
</span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="n">i</span><span class="w"> </span><span class="k">in</span><span class="w"> </span><span class="n">county_urls</span><span class="p">){</span><span class="w">
  </span><span class="n">table</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">read_html</span><span class="p">(</span><span class="n">i</span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
    </span><span class="n">html_nodes</span><span class="p">(</span><span class="s2">".stat-text+ .stat-value .stat-span"</span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
    </span><span class="n">html_text</span><span class="p">()</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
    </span><span class="n">data.frame</span><span class="p">()</span><span class="w">
  </span><span class="c1"># transpose into rows!
</span><span class="w">  </span><span class="n">table_keep</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">t</span><span class="p">(</span><span class="n">data.frame</span><span class="p">(</span><span class="n">table</span><span class="p">[</span><span class="nf">c</span><span class="p">(</span><span class="m">1</span><span class="o">:</span><span class="m">9</span><span class="p">,</span><span class="w"> </span><span class="m">17</span><span class="p">),</span><span class="w"> </span><span class="p">]))</span><span class="w">
  </span><span class="c1"># append to a master data frame
</span><span class="w">  </span><span class="n">ct_econ</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">rbind</span><span class="p">(</span><span class="n">ct_econ</span><span class="p">,</span><span class="w"> </span><span class="n">table_keep</span><span class="p">)</span><span class="w">
</span><span class="p">}</span><span class="w">
</span><span class="c1"># add column names / mentally choose which values to keep after looking on the website
</span><span class="n">colnames</span><span class="p">(</span><span class="n">ct_econ</span><span class="p">)</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="nf">c</span><span class="p">(</span><span class="s2">"med_house_income14"</span><span class="p">,</span><span class="w"> 
                       </span><span class="s2">"avg_male_income"</span><span class="p">,</span><span class="w"> 
                       </span><span class="s2">"avg_female_income"</span><span class="p">,</span><span class="w"> 
                       </span><span class="s2">"highest_income_race"</span><span class="p">,</span><span class="w"> 
                       </span><span class="s2">"wage_gini"</span><span class="p">,</span><span class="w"> 
                       </span><span class="s2">"largest_demo_poverty"</span><span class="p">,</span><span class="w"> 
                       </span><span class="s2">"largest_race_poverty"</span><span class="p">,</span><span class="w"> 
                       </span><span class="s2">"med_native_age"</span><span class="p">,</span><span class="w"> 
                       </span><span class="s2">"med_foreign_age"</span><span class="p">,</span><span class="w"> 
                       </span><span class="s2">"common_major"</span><span class="p">)</span><span class="w"> 
</span><span class="n">ct_econ</span><span class="o">$</span><span class="n">county</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="nf">c</span><span class="p">(</span><span class="s2">"Fairfield"</span><span class="p">,</span><span class="w"> </span><span class="s2">"Hartford"</span><span class="p">,</span><span class="w"> </span><span class="s2">"Litchfield"</span><span class="p">,</span><span class="w"> </span><span class="s2">"Middlesex"</span><span class="p">,</span><span class="w"> 
                    </span><span class="s2">"New Haven"</span><span class="p">,</span><span class="w"> </span><span class="s2">"New London"</span><span class="p">,</span><span class="w"> </span><span class="s2">"Tolland"</span><span class="p">,</span><span class="w"> </span><span class="s2">"Windham"</span><span class="p">)</span><span class="w">
</span>

At this point, I merged the election data with the socio-economic data after picking which stats I wanted to capture and created seperate shapefiles for each candidate.

<span class="c1"># merge this with the ct_results with the economic data
</span><span class="n">ct_join</span><span class="w"> </span><span class="o"><-</span><span class="w">  </span><span class="n">dplyr</span><span class="o">::</span><span class="n">full_join</span><span class="p">(</span><span class="n">ct_results</span><span class="p">,</span><span class="w"> </span><span class="n">ct_econ</span><span class="p">)</span><span class="w">

</span><span class="c1"># full join the data set 
</span><span class="n">ct_join</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">dplyr</span><span class="o">::</span><span class="n">full_join</span><span class="p">(</span><span class="n">ct_shp</span><span class="o">@</span><span class="n">data</span><span class="p">,</span><span class="w"> </span><span class="n">ct_join</span><span class="p">)</span><span class="w">

</span><span class="c1"># remove rows with NA's - i.e. remove everything except the choosen state
</span><span class="n">ct_clean</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">na.omit</span><span class="p">(</span><span class="n">ct_join</span><span class="p">)</span><span class="w">

</span><span class="c1"># merge this with the entire shapefile object
</span><span class="n">ct_shp2</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">ct_shp</span><span class="w">
</span><span class="n">ct_shp2</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">sp</span><span class="o">::</span><span class="n">merge</span><span class="p">(</span><span class="n">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">ct_shp2</span><span class="p">,</span><span class="w"> </span><span class="n">y</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">ct_clean</span><span class="p">,</span><span class="w"> 
                     </span><span class="n">by</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"fips"</span><span class="p">,</span><span class="w"> </span><span class="n">all.x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">F</span><span class="p">,</span><span class="w"> 
                     </span><span class="n">duplicateGeoms</span><span class="o">=</span><span class="kc">TRUE</span><span class="p">)</span><span class="w">
</span><span class="c1"># this is you're shapefile check
</span><span class="n">plot</span><span class="p">(</span><span class="n">ct_shp2</span><span class="p">)</span><span class="w">
</span>

plot of chunk unnamed-chunk-7

<span class="c1">## let's try a specific candidates for creating shape files - Hillary Clinton!!!
</span><span class="n">ct_clean_hrc</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">ct_clean</span><span class="p">[</span><span class="n">which</span><span class="p">(</span><span class="n">ct_clean</span><span class="o">$</span><span class="n">cand_name</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"Hillary Clinton"</span><span class="p">),]</span><span class="w">
</span><span class="n">ct_clean_hrc_join</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">dplyr</span><span class="o">::</span><span class="n">full_join</span><span class="p">(</span><span class="n">ct_shp</span><span class="o">@</span><span class="n">data</span><span class="p">,</span><span class="w"> </span><span class="n">ct_clean_hrc</span><span class="p">)</span><span class="w">
</span><span class="n">hrc_join</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">na.omit</span><span class="p">(</span><span class="n">ct_clean_hrc_join</span><span class="p">)</span><span class="w">
</span><span class="n">hrc_shp</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">sp</span><span class="o">::</span><span class="n">merge</span><span class="p">(</span><span class="n">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">ct_shp</span><span class="p">,</span><span class="w"> </span><span class="n">y</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">hrc_join</span><span class="p">,</span><span class="w"> 
                     </span><span class="n">by</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"fips"</span><span class="p">,</span><span class="w"> </span><span class="n">all.x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">F</span><span class="p">,</span><span class="w"> 
                     </span><span class="n">duplicateGeoms</span><span class="o">=</span><span class="nb">F</span><span class="p">)</span><span class="w">

</span><span class="c1">## Donald Drumpf shape file
</span><span class="n">ct_clean_dt</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">ct_clean</span><span class="p">[</span><span class="n">which</span><span class="p">(</span><span class="n">ct_clean</span><span class="o">$</span><span class="n">cand_name</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"Donald Drumpf"</span><span class="p">),]</span><span class="w">
</span><span class="n">ct_clean_dt_join</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">dplyr</span><span class="o">::</span><span class="n">full_join</span><span class="p">(</span><span class="n">ct_shp</span><span class="o">@</span><span class="n">data</span><span class="p">,</span><span class="w"> </span><span class="n">ct_clean_dt</span><span class="p">)</span><span class="w">
</span><span class="n">dt_join</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">na.omit</span><span class="p">(</span><span class="n">ct_clean_dt_join</span><span class="p">)</span><span class="w">
</span><span class="n">dt_shp</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">sp</span><span class="o">::</span><span class="n">merge</span><span class="p">(</span><span class="n">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">ct_shp</span><span class="p">,</span><span class="w"> </span><span class="n">y</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">dt_join</span><span class="p">,</span><span class="w"> 
                     </span><span class="n">by</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"fips"</span><span class="p">,</span><span class="w"> </span><span class="n">all.x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">F</span><span class="p">,</span><span class="w"> 
                     </span><span class="n">duplicateGeoms</span><span class="o">=</span><span class="nb">F</span><span class="p">)</span><span class="w">

</span><span class="c1">## Gary Johnson shape file
</span><span class="n">ct_clean_gj</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">ct_clean</span><span class="p">[</span><span class="n">which</span><span class="p">(</span><span class="n">ct_clean</span><span class="o">$</span><span class="n">cand_name</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"Gary Johnson"</span><span class="p">),]</span><span class="w">
</span><span class="n">ct_clean_gj_join</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">dplyr</span><span class="o">::</span><span class="n">full_join</span><span class="p">(</span><span class="n">ct_shp</span><span class="o">@</span><span class="n">data</span><span class="p">,</span><span class="w"> </span><span class="n">ct_clean_gj</span><span class="p">)</span><span class="w">
</span><span class="n">gj_join</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">na.omit</span><span class="p">(</span><span class="n">ct_clean_gj_join</span><span class="p">)</span><span class="w">
</span><span class="n">gj_shp</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">sp</span><span class="o">::</span><span class="n">merge</span><span class="p">(</span><span class="n">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">ct_shp</span><span class="p">,</span><span class="w"> </span><span class="n">y</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">gj_join</span><span class="p">,</span><span class="w"> 
                    </span><span class="n">by</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"fips"</span><span class="p">,</span><span class="w"> </span><span class="n">all.x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">F</span><span class="p">,</span><span class="w"> 
                    </span><span class="n">duplicateGeoms</span><span class="o">=</span><span class="nb">F</span><span class="p">)</span><span class="w">

</span><span class="c1">## Jill Stein shape file
</span><span class="n">ct_clean_jt</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">ct_clean</span><span class="p">[</span><span class="n">which</span><span class="p">(</span><span class="n">ct_clean</span><span class="o">$</span><span class="n">cand_name</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"Jill Stein"</span><span class="p">),]</span><span class="w">
</span><span class="n">ct_clean_jt_join</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">dplyr</span><span class="o">::</span><span class="n">full_join</span><span class="p">(</span><span class="n">ct_shp</span><span class="o">@</span><span class="n">data</span><span class="p">,</span><span class="w"> </span><span class="n">ct_clean_jt</span><span class="p">)</span><span class="w">
</span><span class="n">jt_join</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">na.omit</span><span class="p">(</span><span class="n">ct_clean_jt_join</span><span class="p">)</span><span class="w">
</span><span class="n">jt_shp</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">sp</span><span class="o">::</span><span class="n">merge</span><span class="p">(</span><span class="n">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">ct_shp</span><span class="p">,</span><span class="w"> </span><span class="n">y</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">jt_join</span><span class="p">,</span><span class="w"> 
                    </span><span class="n">by</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"fips"</span><span class="p">,</span><span class="w"> </span><span class="n">all.x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">F</span><span class="p">,</span><span class="w"> 
                    </span><span class="n">duplicateGeoms</span><span class="o">=</span><span class="nb">F</span><span class="p">)</span><span class="w">
</span>

Spatial Analysis and mapping are a great way to visualize and understand this type of data, so I used leaflet for its interactivity to compare candidates and how many votes they received. This is the ground work for defining the color, popup info, and creating partitions for each candidate as their own base group on the map.

<span class="c1"># create seperate color patterns for each candidate 
</span><span class="n">pal1</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">colorBin</span><span class="p">(</span><span class="n">palette</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"Blues"</span><span class="p">,</span><span class="w"> </span><span class="n">domain</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">hrc_shp</span><span class="o">$</span><span class="n">votes</span><span class="p">,</span><span class="w"> </span><span class="n">bins</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">8</span><span class="p">)</span><span class="w">
</span><span class="n">pal2</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">colorBin</span><span class="p">(</span><span class="n">palette</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"Reds"</span><span class="p">,</span><span class="w"> </span><span class="n">domain</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">dt_shp</span><span class="o">$</span><span class="n">votes</span><span class="p">,</span><span class="w"> </span><span class="n">bins</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">8</span><span class="p">)</span><span class="w">
</span><span class="n">pal3</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">colorBin</span><span class="p">(</span><span class="n">palette</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"YlOrRd"</span><span class="p">,</span><span class="w"> </span><span class="n">domain</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">gj_shp</span><span class="o">$</span><span class="n">votes</span><span class="p">,</span><span class="w"> </span><span class="n">bins</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">8</span><span class="p">)</span><span class="w">
</span><span class="n">pal4</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">colorBin</span><span class="p">(</span><span class="n">palette</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"Greens"</span><span class="p">,</span><span class="w"> </span><span class="n">domain</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">jt_shp</span><span class="o">$</span><span class="n">votes</span><span class="p">,</span><span class="w"> </span><span class="n">bins</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">8</span><span class="p">)</span><span class="w">

</span><span class="c1"># Populate statewide socio-economic values for popup!
</span><span class="n">state_popup1</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">paste0</span><span class="p">(</span><span class="s2">"<strong>County: </strong>"</span><span class="p">,</span><span class="w"> 
                      </span><span class="n">hrc_shp</span><span class="o">$</span><span class="n">county</span><span class="p">,</span><span class="w"> 
                      </span><span class="s2">"<br><strong>Total Amount of 2016 Voters: </strong>"</span><span class="p">,</span><span class="w"> 
                      </span><span class="n">hrc_shp</span><span class="o">$</span><span class="n">total</span><span class="p">,</span><span class="w">
                      </span><span class="s2">"<br><strong>Percentage of Earned Votes: </strong>"</span><span class="p">,</span><span class="w"> 
                      </span><span class="nf">round</span><span class="p">(</span><span class="n">hrc_shp</span><span class="o">$</span><span class="n">pct</span><span class="p">,</span><span class="w"> </span><span class="m">3</span><span class="p">)</span><span class="o">*</span><span class="w"> </span><span class="m">100</span><span class="p">,</span><span class="w"> </span><span class="s2">"%"</span><span class="p">,</span><span class="w">
                      </span><span class="s2">"<br><strong>Median Household Income: </strong>"</span><span class="p">,</span><span class="w"> 
                      </span><span class="n">hrc_shp</span><span class="o">$</span><span class="n">med_house_income14</span><span class="p">,</span><span class="w"> 
                      </span><span class="s2">"<br><strong>Average Female Income: </strong>"</span><span class="p">,</span><span class="w">
                      </span><span class="n">hrc_shp</span><span class="o">$</span><span class="n">avg_female_income</span><span class="p">,</span><span class="w"> 
                      </span><span class="s2">"<br><strong>Average Male Income: </strong>"</span><span class="p">,</span><span class="w">
                      </span><span class="n">hrc_shp</span><span class="o">$</span><span class="n">avg_male_income</span><span class="p">,</span><span class="w"> 
                      </span><span class="s2">"<br><strong>Wage Equality Index: </strong>"</span><span class="p">,</span><span class="w"> 
                      </span><span class="n">hrc_shp</span><span class="o">$</span><span class="n">wage_gini</span><span class="p">,</span><span class="w"> 
                      </span><span class="s2">"<br><strong>Largest Demographic in Poverty: </strong>"</span><span class="p">,</span><span class="w"> 
                      </span><span class="n">hrc_shp</span><span class="o">$</span><span class="n">largest_demo_poverty</span><span class="p">)</span><span class="w">

</span><span class="n">state_popup2</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">paste0</span><span class="p">(</span><span class="s2">"<strong>County: </strong>"</span><span class="p">,</span><span class="w"> 
                       </span><span class="n">dt_shp</span><span class="o">$</span><span class="n">county</span><span class="p">,</span><span class="w"> 
                       </span><span class="s2">"<br><strong>Total Amount of 2016 Voters: </strong>"</span><span class="p">,</span><span class="w"> 
                       </span><span class="n">dt_shp</span><span class="o">$</span><span class="n">total</span><span class="p">,</span><span class="w">
                       </span><span class="s2">"<br><strong>Percentage of Earned Votes: </strong>"</span><span class="p">,</span><span class="w"> 
                       </span><span class="nf">round</span><span class="p">(</span><span class="n">dt_shp</span><span class="o">$</span><span class="n">pct</span><span class="p">,</span><span class="w"> </span><span class="m">3</span><span class="p">)</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="m">100</span><span class="p">,</span><span class="w"> </span><span class="s2">"%"</span><span class="p">,</span><span class="w">
                       </span><span class="s2">"<br><strong>Median Household Income: </strong>"</span><span class="p">,</span><span class="w"> 
                       </span><span class="n">dt_shp</span><span class="o">$</span><span class="n">med_house_income14</span><span class="p">,</span><span class="w"> 
                       </span><span class="s2">"<br><strong>Average Female Income: </strong>"</span><span class="p">,</span><span class="w">
                       </span><span class="n">dt_shp</span><span class="o">$</span><span class="n">avg_female_income</span><span class="p">,</span><span class="w"> 
                       </span><span class="s2">"<br><strong>Average Male Income: </strong>"</span><span class="p">,</span><span class="w">
                       </span><span class="n">dt_shp</span><span class="o">$</span><span class="n">avg_male_income</span><span class="p">,</span><span class="w"> 
                       </span><span class="s2">"<br><strong>Wage Equality Index: </strong>"</span><span class="p">,</span><span class="w"> 
                       </span><span class="n">dt_shp</span><span class="o">$</span><span class="n">wage_gini</span><span class="p">,</span><span class="w"> 
                       </span><span class="s2">"<br><strong>Largest Demographic in Poverty: </strong>"</span><span class="p">,</span><span class="w"> 
                       </span><span class="n">dt_shp</span><span class="o">$</span><span class="n">largest_demo_poverty</span><span class="p">)</span><span class="w">

</span><span class="n">state_popup3</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">paste0</span><span class="p">(</span><span class="s2">"<strong>County: </strong>"</span><span class="p">,</span><span class="w"> 
                       </span><span class="n">gj_shp</span><span class="o">$</span><span class="n">county</span><span class="p">,</span><span class="w"> 
                       </span><span class="s2">"<br><strong>Total Amount of 2016 Voters: </strong>"</span><span class="p">,</span><span class="w"> 
                       </span><span class="n">gj_shp</span><span class="o">$</span><span class="n">total</span><span class="p">,</span><span class="w">
                       </span><span class="s2">"<br><strong>Percentage of Earned Votes: </strong>"</span><span class="p">,</span><span class="w"> 
                       </span><span class="nf">round</span><span class="p">(</span><span class="n">gj_shp</span><span class="o">$</span><span class="n">pct</span><span class="p">,</span><span class="w"> </span><span class="m">3</span><span class="p">)</span><span class="o">*</span><span class="w"> </span><span class="m">100</span><span class="p">,</span><span class="w"> </span><span class="s2">"%"</span><span class="p">,</span><span class="w">
                       </span><span class="s2">"<br><strong>Median Household Income: </strong>"</span><span class="p">,</span><span class="w"> 
                       </span><span class="n">gj_shp</span><span class="o">$</span><span class="n">med_house_income14</span><span class="p">,</span><span class="w"> 
                       </span><span class="s2">"<br><strong>Average Female Income: </strong>"</span><span class="p">,</span><span class="w">
                       </span><span class="n">gj_shp</span><span class="o">$</span><span class="n">avg_female_income</span><span class="p">,</span><span class="w"> 
                       </span><span class="s2">"<br><strong>Average Male Income: </strong>"</span><span class="p">,</span><span class="w">
                       </span><span class="n">gj_shp</span><span class="o">$</span><span class="n">avg_male_income</span><span class="p">,</span><span class="w"> 
                       </span><span class="s2">"<br><strong>Wage Equality Index: </strong>"</span><span class="p">,</span><span class="w"> 
                       </span><span class="n">gj_shp</span><span class="o">$</span><span class="n">wage_gini</span><span class="p">,</span><span class="w"> 
                       </span><span class="s2">"<br><strong>Largest Demographic in Poverty: </strong>"</span><span class="p">,</span><span class="w"> 
                       </span><span class="n">gj_shp</span><span class="o">$</span><span class="n">largest_demo_poverty</span><span class="p">)</span><span class="w">


</span><span class="n">state_popup4</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">paste0</span><span class="p">(</span><span class="s2">"<strong>County: </strong>"</span><span class="p">,</span><span class="w"> 
                       </span><span class="n">jt_shp</span><span class="o">$</span><span class="n">county</span><span class="p">,</span><span class="w"> 
                       </span><span class="s2">"<br><strong>Total Amount of 2016 Voters: </strong>"</span><span class="p">,</span><span class="w"> 
                       </span><span class="n">jt_shp</span><span class="o">$</span><span class="n">total</span><span class="p">,</span><span class="w">
                       </span><span class="s2">"<br><strong>Percentage of Earned Votes: </strong>"</span><span class="p">,</span><span class="w"> 
                       </span><span class="nf">round</span><span class="p">(</span><span class="n">jt_shp</span><span class="o">$</span><span class="n">pct</span><span class="p">,</span><span class="w"> </span><span class="m">3</span><span class="p">)</span><span class="o">*</span><span class="w"> </span><span class="m">100</span><span class="p">,</span><span class="w"> </span><span class="s2">"%"</span><span class="p">,</span><span class="w">
                       </span><span class="s2">"<br><strong>Median Household Income: </strong>"</span><span class="p">,</span><span class="w"> 
                       </span><span class="n">jt_shp</span><span class="o">$</span><span class="n">med_house_income14</span><span class="p">,</span><span class="w"> 
                       </span><span class="s2">"<br><strong>Average Female Income: </strong>"</span><span class="p">,</span><span class="w">
                       </span><span class="n">jt_shp</span><span class="o">$</span><span class="n">avg_female_income</span><span class="p">,</span><span class="w"> 
                       </span><span class="s2">"<br><strong>Average Male Income: </strong>"</span><span class="p">,</span><span class="w">
                       </span><span class="n">jt_shp</span><span class="o">$</span><span class="n">avg_male_income</span><span class="p">,</span><span class="w"> 
                       </span><span class="s2">"<br><strong>Wage Equality Index: </strong>"</span><span class="p">,</span><span class="w"> 
                       </span><span class="n">jt_shp</span><span class="o">$</span><span class="n">wage_gini</span><span class="p">,</span><span class="w"> 
                       </span><span class="s2">"<br><strong>Largest Demographic in Poverty: </strong>"</span><span class="p">,</span><span class="w"> 
                       </span><span class="n">jt_shp</span><span class="o">$</span><span class="n">largest_demo_poverty</span><span class="p">)</span><span class="w">
</span>

Here is our map widgets! Feel free to explore and adapt this code for your analysis of a given state!

<span class="c1"># plot the map(s)
</span><span class="n">hrc_map</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">leaflet</span><span class="p">(</span><span class="n">data</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">hrc_shp</span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class="w">
  </span><span class="n">addProviderTiles</span><span class="p">(</span><span class="s2">"CartoDB.Positron"</span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class="w">
  </span><span class="n">addPolygons</span><span class="p">(</span><span class="n">fillColor</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">~</span><span class="n">pal1</span><span class="p">(</span><span class="n">votes</span><span class="p">),</span><span class="w"> 
              </span><span class="n">fillOpacity</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">1</span><span class="p">,</span><span class="w"> 
              </span><span class="n">color</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"#BDBDC3"</span><span class="p">,</span><span class="w"> 
              </span><span class="n">weight</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">1</span><span class="p">,</span><span class="w"> 
              </span><span class="n">popup</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">state_popup1</span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class="w">
  </span><span class="n">addLegend</span><span class="p">(</span><span class="s2">"bottomright"</span><span class="p">,</span><span class="w"> 
            </span><span class="n">pal</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">pal1</span><span class="p">,</span><span class="w"> 
            </span><span class="n">values</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">~</span><span class="n">votes</span><span class="p">,</span><span class="w">
            </span><span class="n">title</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"Total Votes for Hillary Clinton: "</span><span class="p">,</span><span class="w">
            </span><span class="n">opacity</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">1</span><span class="p">)</span><span class="w">
</span><span class="n">print</span><span class="p">(</span><span class="n">hrc_map</span><span class="p">)</span><span class="w">

</span><span class="n">dt_map</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">leaflet</span><span class="p">(</span><span class="n">data</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">dt_shp</span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class="w">
  </span><span class="n">addProviderTiles</span><span class="p">(</span><span class="s2">"CartoDB.Positron"</span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class="w">
  </span><span class="n">addPolygons</span><span class="p">(</span><span class="n">fillColor</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">~</span><span class="n">pal2</span><span class="p">(</span><span class="n">votes</span><span class="p">),</span><span class="w"> 
              </span><span class="n">fillOpacity</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">1</span><span class="p">,</span><span class="w"> 
              </span><span class="n">color</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"#BDBDC3"</span><span class="p">,</span><span class="w"> 
              </span><span class="n">weight</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">1</span><span class="p">,</span><span class="w"> 
              </span><span class="n">popup</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">state_popup2</span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class="w">
  </span><span class="n">addLegend</span><span class="p">(</span><span class="s2">"bottomright"</span><span class="p">,</span><span class="w"> 
            </span><span class="n">pal</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">pal2</span><span class="p">,</span><span class="w"> 
            </span><span class="n">values</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">~</span><span class="n">votes</span><span class="p">,</span><span class="w">
            </span><span class="n">title</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"Total Votes for Donald Drumpf: "</span><span class="p">,</span><span class="w">
            </span><span class="n">opacity</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">1</span><span class="p">)</span><span class="w"> 
</span><span class="n">print</span><span class="p">(</span><span class="n">dt_map</span><span class="p">)</span><span class="w">


</span><span class="n">gj_map</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">leaflet</span><span class="p">(</span><span class="n">data</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">gj_shp</span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class="w">
  </span><span class="n">addProviderTiles</span><span class="p">(</span><span class="s2">"CartoDB.Positron"</span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class="w">
  </span><span class="n">addPolygons</span><span class="p">(</span><span class="n">fillColor</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">~</span><span class="n">pal3</span><span class="p">(</span><span class="n">votes</span><span class="p">),</span><span class="w"> 
              </span><span class="n">fillOpacity</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">1</span><span class="p">,</span><span class="w"> 
              </span><span class="n">color</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"#BDBDC3"</span><span class="p">,</span><span class="w"> 
              </span><span class="n">weight</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">1</span><span class="p">,</span><span class="w"> 
              </span><span class="n">popup</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">state_popup3</span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class="w">
  </span><span class="n">addLegend</span><span class="p">(</span><span class="s2">"bottomleft"</span><span class="p">,</span><span class="w"> 
            </span><span class="n">pal</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">pal3</span><span class="p">,</span><span class="w"> 
            </span><span class="n">values</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">~</span><span class="n">votes</span><span class="p">,</span><span class="w">
            </span><span class="n">title</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"Total Votes for Gary Johnson: "</span><span class="p">,</span><span class="w">
            </span><span class="n">opacity</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">1</span><span class="p">)</span><span class="w"> 
</span><span class="n">print</span><span class="p">(</span><span class="n">gj_map</span><span class="p">)</span><span class="w">


</span><span class="n">jt_map</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">leaflet</span><span class="p">(</span><span class="n">data</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">jt_shp</span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class=...

To leave a comment for the author, please follow the link and comment on their blog: Jasmine Dumas' R Blog.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)