<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/plot-the-vote-making-u-s-senate-house-cartograms-in-r/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://rud.is/b/2017/05/07/plot-the-vote-making-u-s-senate-house-cartograms-in-r/"> R – rud.is</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>Political machinations are a tad insane in the U.S. these days &amp; I regularly hit up <a target="_blank" rel="nofollow" href="http://twitter.com/ProPublica">@ProPublica</a> &amp; <a target="_blank" rel="nofollow" href="http://twitter.com/GovTrack">@GovTrack</a> sites (&amp; sub to the GovTrack e-mail updates) as I try to be an informed citizen, especially since I’ve got a Senator and Representative who seem to be in the sway of <img alt="🍊" class="wp-smiley" src="https://s.w.org/images/core/emoji/2/72x72/1f34a.png">. </p>
<p>I’ve always appreciated the ProPublica and GovTrack cartograms as they present a great deal of information in a compact space (especially the House versions). Something nudged me into starting an R package to let folks create them in R (mainly with <code>ggplot2</code> but an <code>htmlwidget</code> version is planned), which I’ve dubbed <a target="_blank" rel="nofollow" href="https://github.com/hrbrmstr/voteogram"><code>voteogram</code></a>.</p>
<p>With the <code>voteogram</code> package, you can:</p>
<ul>
<li>pull ProPublica roll call vote data for the 101st Congress up through today (via <code>roll_call()</code>)</li>
<li>plot ProPublica-esque Senate roll call vote cartograms</li>
<li>plot ProPublica-esque House roll call vote cartograms</li>
<li>plot GovTrack-esque House roll call vote cartograms</li>
</ul>
<p>GovTrack uses — what I’ve seen <a target="_blank" rel="nofollow" href="http://twitter.com/thosjleeper">@thosjleeper</a> refer to as — a “parliamentary plot” for their version of the Senate roll call cartogram and sir Leeper already has that type of plot covered in <a target="_blank" rel="nofollow" href="https://github.com/leeper/ggparliament"><code>ggparliament</code></a>, so I’ve just focused on the other ones here.</p>
<h3>Roll Call</h3>
<p>You need data for these cartogram generation functions and you can specify your own populated data frame (the needed columns are in the manual pages for the cartogram plotters). However, you’ll likely want to plot existing data that others have tallied and ProPublica makes that super simple since each vote is in a standalone JSON file. All you have to do is specify whether you want the roll call vote for the <code>house</code> or <code>senate</code>, the Congress number (current one is 115), the session number (current one is 1) and the roll call vote number. </p>
<p>For example, we can see all the <strike>idiots</strike> Representatives who voted, recently, to <strike>kill people</strike> repeal the ACA with the following function call:</p>
<pre><code>(h256 &lt;- roll_call(&quot;house&quot;, 115, 1, 256))
## 115th Congress / Session: 1 / House Roll Call: 256 / May  4, 2017
## 
## American Health Care Act
## 
## Result: Passed

str(h256, max.level = 1)
## List of 29
##  $ vote_id              : chr &quot;H_115_1_256&quot;
##  $ chamber              : chr &quot;House&quot;
##  $ year                 : int 2017
##  $ congress             : chr &quot;115&quot;
##  $ session              : chr &quot;1&quot;
##  $ roll_call            : int 256
##  $ needed_to_pass       : int 216
##  $ date_of_vote         : chr &quot;May  4, 2017&quot;
##  $ time_of_vote         : chr &quot;02:18 PM&quot;
##  $ result               : chr &quot;Passed&quot;
##  $ vote_type            : chr &quot;RECORDED VOTE&quot;
##  $ question             : chr &quot;On Passage&quot;
##  $ description          : chr &quot;American Health Care Act&quot;
##  $ nyt_title            : chr &quot;On Passage&quot;
##  $ 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 &quot;No&quot;
##  $ gop_majority_position: chr &quot;Yes&quot;
##  $ votes                :Classes ‘tbl_df’, ‘tbl’ and 'data.frame':  435 obs. of  11 variables:
##  - attr(*, &quot;class&quot;)= chr [1:2] &quot;pprc&quot; &quot;list&quot;</code></pre>
<p>As you can see, it has a custom <code>print</code> function and the usable data (for cartographic needs) is in <code>$votes</code>. You can go to town with just that information, making bar charts or tracking individual Congress-critter votes.</p>
<p>Do your best to cache this data as you retrieve it. ProPublica is a non-profit and the JSON files are on AWS. While there’s a certain number of free bits of bandwidth-per-month allotted buy Amazon’s S3 service, best to make sure you’re not tipping them over on any given month. Plus, the vote data doesn’t change once it’s recorded. Consider donating to them if you decided to always grab fresh copies.</p>
<p>There’s a <code>fortify</code> function for this object (it’s classed <code>pprc</code>) so you can pass it right into <code>ggplot()</code> for use or pipe it into a <code>dplyr</code> chain for aggregation &amp; filtering.</p>
<h3>House Rules</h3>
<p>With the data in hand, we can make some cartograms (the real purpose of the package). I riffed off the ProPublica colors (and haven’t fully finished copying them yet as I need to search for 2 more categories of Independent voting colors) but you can replace them with anything you want. Just reset the scale and use the names in the exposed color value vectors. </p>
<p>There’s also a <code>theme_voteogram()</code> which is designed to augment any base theme (like <code>hrbrthemes::theme_ipsum_rc()</code>) (it’s much like <code>ggthemes::theme_map()</code>).</p>
<p>Here’s the ProPublica view for that particular vote:</p>
<pre><code>house_carto(rep) +
  labs(x=NULL, y=NULL, 
       title=&quot;House Vote 256 - Passes American Health Care Act,\nRepealing Obamacare&quot;) +
  theme_ipsum_rc(plot_title_size = 24) +
  theme_voteogram()</code></pre>
<p><a target="_blank" rel="nofollow" href="https://i1.wp.com/rud.is/b/wp-content/uploads/2017/05/rep_pp-1.png?ssl=1"><img alt="" class="wp-image-5917" src="https://i1.wp.com/rud.is/b/wp-content/uploads/2017/05/rep_pp-1.png?resize=780%2C546&ssl=1" data-recalc-dims="1"></a></p>
<p>The <code>house_carto()</code> function defaults to the ProPublica cartogram, but you can easily change that:</p>
<pre><code>house_carto(rep, &quot;gt&quot;) +
  labs(x=NULL, y=NULL, 
       title=&quot;House Vote 256 - Passes American Health Care Act,\nRepealing Obamacare&quot;) +
  theme_ipsum_rc(plot_title_size = 24) +
  theme_voteogram()</code></pre>
<p><a target="_blank" rel="nofollow" href="https://i1.wp.com/rud.is/b/wp-content/uploads/2017/05/rep_gt-1.png?ssl=1"><img alt="" class="wp-image-5918" src="https://i1.wp.com/rud.is/b/wp-content/uploads/2017/05/rep_gt-1.png?resize=780%2C546&ssl=1" data-recalc-dims="1"></a></p>
<h3>Senate Drools</h3>
<p>Again, the <code>senate_carto()</code> function only has the ProPublica-esque cartogram available and works pretty much the same way after getting the Senate vote data:</p>
<pre><code>sen &lt;- roll_call(&quot;senate&quot;, 115, 1, 110)

senate_carto(sen) +
  labs(title=&quot;Senate Vote 110 - Invokes Cloture on Neil Gorsuch Nomination&quot;) +
  theme_ipsum_rc(plot_title_size = 24) +
  theme_voteogram()</code></pre>
<p><a target="_blank" rel="nofollow" href="https://i0.wp.com/rud.is/b/wp-content/uploads/2017/05/sen-1.png?ssl=1"><img alt="" class="wp-image-5920" src="https://i0.wp.com/rud.is/b/wp-content/uploads/2017/05/sen-1.png?resize=780%2C546&ssl=1" data-recalc-dims="1"></a></p>
<h3>FIN</h3>
<p>There’s a bit of work left to do in the package (including an <code>htmlwidget</code> version). You’re invited to file <a target="_blank" rel="nofollow" href="https://github.com/hrbrmstr/voteogram/pulls">PRs</a> or <a target="_blank" rel="nofollow" href="https://github.com/hrbrmstr/voteogram/issues">Issues</a> as you are so moved.</p>
<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://rud.is/b/2017/05/07/plot-the-vote-making-u-s-senate-house-cartograms-in-r/"> R – rud.is</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/search/data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/search/map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/search/animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/search/sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/search/SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/search/git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/search/hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/search/PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/search/time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/search/trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/PYKeZk-ygPE"></a></div>