This document was last updated at 2017-05-09 02:46:55.

A blogger by the name of hrbrmstr posted a nice article about his voteogram package, and that article was posted on R Bloggers (https://www.r-bloggers.com/plot-the-vote-making-u-s-senate-house-cartograms-in-r/). Here I hope to explore some of the package’s features and abilites.

Gathering the Data

# INSTALLATION (if needed)
#install.packages("devtools")
library("devtools")
#devtools::install_github("hrbrmstr/voteogram")

# recommended libraries
library("voteogram")
library("jsonlite")
library("hrbrthemes")
library("tidyverse")

The author of the voteogram package provided the following line2 of code to get users started

sen <- roll_call("senate", 115, 1, 110)
rep <- roll_call("house", 115, 1, 256)

which retrieves data from the United States Senate or House of Representatives representively, followed by the group number (here, the 115th Congress), session number, and roll call number. This data comes from websites such as https://www.propublica.org/ and https://www.govtrack.us/. Calling the list gives a summary of the results

rep
## 115th Congress / Session: 1 / House Roll Call: 256 / May  4, 2017
## 
## American Health Care Act
## 
## Result: Passed

while we will probably later focus on the voting records extracted by rep$votes

rep$votes
## # A tibble: 435 × 11
##    bioguide_id role_id        member_name sort_name party state_abbrev
## *        <chr>   <int>              <chr>     <chr> <chr>        <chr>
## 1      A000374     274      Ralph Abraham   Abraham     R           LA
## 2      A000370     294        Alma  Adams     Adams     D           NC
## 3      A000055     224 Robert B. Aderholt  Aderholt     R           AL
## 4      A000371     427       Pete Aguilar   Aguilar     D           CA
## 5      A000372     268         Rick Allen     Allen     R           GA
## 6      A000367     131       Justin Amash     Amash     R           MI
## 7      A000369     388        Mark Amodei    Amodei     R           NV
## 8      A000375     320    Jodey Arrington Arrington     R           TX
## 9      B001291     590        Brian Babin     Babin     R           TX
## 10     B001298     206          Don Bacon     Bacon     R           NE
## # ... with 425 more rows, and 5 more variables:
## #   display_state_abbrev <chr>, district <int>, position <chr>,
## #   dw_nominate <lgl>, pp_id <chr>

For thorough exploration, we can view the structure of the list.

str(rep)
## List of 29
##  $ vote_id              : chr "H_115_1_256"
##  $ chamber              : chr "House"
##  $ year                 : int 2017
##  $ congress             : chr "115"
##  $ session              : chr "1"
##  $ roll_call            : int 256
##  $ needed_to_pass       : int 216
##  $ date_of_vote         : chr "May  4, 2017"
##  $ time_of_vote         : chr "02:18 PM"
##  $ result               : chr "Passed"
##  $ vote_type            : chr "RECORDED VOTE"
##  $ question             : chr "On Passage"
##  $ description          : chr "American Health Care Act"
##  $ nyt_title            : chr "On Passage"
##  $ total_yes            : int 217
##  $ total_no             : int 213
##  $ total_not_voting     : int 1
##  $ gop_yes              : int 217
##  $ gop_no               : int 20
##  $ gop_not_voting       : int 1
##  $ dem_yes              : int 0
##  $ dem_no               : int 193
##  $ dem_not_voting       : int 0
##  $ ind_yes              : int 0
##  $ ind_no               : int 0
##  $ ind_not_voting       : int 0
##  $ dem_majority_position: chr "No"
##  $ gop_majority_position: chr "Yes"
##  $ votes                :Classes 'tbl_df', 'tbl' and 'data.frame':   435 obs. of  11 variables:
##   ..$ bioguide_id         : chr [1:435] "A000374" "A000370" "A000055" "A000371" ...
##   ..$ role_id             : int [1:435] 274 294 224 427 268 131 388 320 590 206 ...
##   ..$ member_name         : chr [1:435] "Ralph Abraham" "Alma  Adams" "Robert B. Aderholt" "Pete Aguilar" ...
##   ..$ sort_name           : chr [1:435] "Abraham" "Adams" "Aderholt" "Aguilar" ...
##   ..$ party               : chr [1:435] "R" "D" "R" "D" ...
##   ..$ state_abbrev        : chr [1:435] "LA" "NC" "AL" "CA" ...
##   ..$ display_state_abbrev: chr [1:435] "La." "N.C." "Ala." "Calif." ...
##   ..$ district            : int [1:435] 5 12 4 31 12 3 2 19 36 2 ...
##   ..$ position            : chr [1:435] "Yes" "No" "Yes" "No" ...
##   ..$ dw_nominate         : logi [1:435] NA NA NA NA NA NA ...
##   ..$ pp_id               : chr [1:435] "LA_5" "NC_12" "AL_4" "CA_31" ...
##  - attr(*, "class")= chr [1:2] "pprc" "list"

For now, I am assuming that there have been at least \(N = 256\) roll call votes for this current House of Representatives, so I hope to gather that much into one structure. I will first retrieve the results of the first roll call of the 115th House of Representatives 1st session.

HOR115 <- tbl_df(data.frame(roll_id = 1, roll_call("house", 115, 1, 1)$votes))

Now I hope that a simple for loop will be able to retrieve the subsequent roll calls’ votes. Note that every roll call describes the set number of 435 representatives.

N <- 256

for(roll_call_counter in 200:N){
  thisRollCall_description <- roll_call("house", 115, 1, roll_call_counter)$description
  thisRollCall_votes <- roll_call("house", 115, 1, roll_call_counter)$votes
  thisRollCall_id <- rep(roll_call_counter, 435)
  HOR115 <- bind_rows(HOR115,
                      tbl_df(data.frame(roll_id = thisRollCall_id, 
                                        thisRollCall_votes, 
                                        description = thisRollCall_description)))
}
## Warning in bind_rows_(x, .id): Unequal factor levels: coercing to character

At this time, my computer can load about one roll call per second, but the data retrieval seems to time out if I try to grab every roll call. Thank goodness for code chunk caches.


Focusing on a Representative

I live in Merced, which is a relatively small city in the middle of California. Our representative is Jim Costa. Fortunately, with the dplyr tools, it is easy to filter his votes. For starters, here is Jim Costa’s voting distribution for this session.

JimCostaRecord <- HOR115 %>%
  filter(member_name == "Jim Costa")
table(JimCostaRecord$position)
## 
##      No Present     Yes 
##      27       1      30

From there, we can filter which roll calls were approved by Representative Costa.

JimCostaRecord %>% filter(position == "Yes") %>%
  select(roll_id, member_name, party, state_abbrev, district, position, description) %>% 
  print(n = nrow(.)) #print all rows for dramatic effect
## # A tibble: 30 × 7
##    roll_id member_name party state_abbrev district position
##      <dbl>       <chr> <chr>        <chr>    <int>    <chr>
## 1      205   Jim Costa     D           CA       16      Yes
## 2      206   Jim Costa     D           CA       16      Yes
## 3      207   Jim Costa     D           CA       16      Yes
## 4      209   Jim Costa     D           CA       16      Yes
## 5      210   Jim Costa     D           CA       16      Yes
## 6      214   Jim Costa     D           CA       16      Yes
## 7      215   Jim Costa     D           CA       16      Yes
## 8      216   Jim Costa     D           CA       16      Yes
## 9      218   Jim Costa     D           CA       16      Yes
## 10     220   Jim Costa     D           CA       16      Yes
## 11     221   Jim Costa     D           CA       16      Yes
## 12     222   Jim Costa     D           CA       16      Yes
## 13     223   Jim Costa     D           CA       16      Yes
## 14     226   Jim Costa     D           CA       16      Yes
## 15     227   Jim Costa     D           CA       16      Yes
## 16     231   Jim Costa     D           CA       16      Yes
## 17     232   Jim Costa     D           CA       16      Yes
## 18     233   Jim Costa     D           CA       16      Yes
## 19     235   Jim Costa     D           CA       16      Yes
## 20     236   Jim Costa     D           CA       16      Yes
## 21     237   Jim Costa     D           CA       16      Yes
## 22     238   Jim Costa     D           CA       16      Yes
## 23     239   Jim Costa     D           CA       16      Yes
## 24     242   Jim Costa     D           CA       16      Yes
## 25     243   Jim Costa     D           CA       16      Yes
## 26     247   Jim Costa     D           CA       16      Yes
## 27     248   Jim Costa     D           CA       16      Yes
## 28     249   Jim Costa     D           CA       16      Yes
## 29     250   Jim Costa     D           CA       16      Yes
## 30     255   Jim Costa     D           CA       16      Yes
## # ... with 1 more variables: description <chr>

Plotting our Representatives’ Votes

Above being able to extract Congress’ voting records with the roll_call command, the voteogram package allows us to easily graph the results of a single roll call as a United States map visualization.

Here I will focus on roll call 255 of the 115th House of Representatives: the most recent bill that my representative Jim Costa approved. First, the package’s author recommended a ProPublica view.

rep255 <- roll_call("house", 115, 1, 255)

house_carto(rep255) +
  labs(x = NULL, y = NULL, 
       title = rep255$description) +
  theme_ipsum_rc(plot_title_size = 24) +
  theme_voteogram()

print(rep255$description)
## [1] "To amend the Public Health Service Act to eliminate the non-application of certain State waiver provisions to Members of Congress and congressional staff"

Next, you can also use the hexagonal layout with this voteogram package as well (note the "gt")

house_carto(rep255, "gt") +
  labs(x = NULL, y = NULL, 
       title = rep255$description) +
  theme_ipsum_rc(plot_title_size = 24) +
  theme_voteogram()


That’s it for now. In the future we will need to see how to access the Amazon AWS database for longer periods of time (to gather all of the votes).