<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/r%e2%81%b6-general-attys-distributions/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://rud.is/b/2017/07/25/r%E2%81%B6-general-attys-distributions/"> R – rud.is</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>Matt <a rel="nofollow" href="http://twitter.com/stiles" target="_blank">@stiles</a> is a spiffy data journalist at the <a rel="nofollow" href="http://twitter.com/latimes" target="_blank">@latimes</a> and he posted an interesting chart on U.S. Attorneys General longevity (given that the current US AG is on thin ice):</p>
<blockquote class="twitter-tweet" data-lang="en"><p lang="en" dir="ltr">Only Watergate and the Civil War have prompted shorter tenures as AG (if Sessions were to leave now). A daily viz: <a rel="nofollow" href="https://t.co/aJ4KDsC5kC" target="_blank">https://t.co/aJ4KDsC5kC</a> <a rel="nofollow" href="https://t.co/ZoiEV3MhGp" target="_blank">pic.twitter.com/ZoiEV3MhGp</a></p>
<p>— Matt Stiles (<a rel="nofollow" href="http://twitter.com/stiles" target="_blank">@stiles</a>) <a rel="nofollow" href="https://twitter.com/stiles/status/889867141104754688" target="_blank">July 25, 2017</a></p></blockquote>
<p></p>
<p>I thought it would be neat (since Matt did the data scraping part already) to look at AG tenure distribution by party, while also pointing out where Sessions falls.</p>
<p>Now, while Matt did scrape the data, it’s tucked away into a javascript variable in an <code>iframe</code> on the page that contains his vis.</p>
<p><a rel="nofollow" href="https://i0.wp.com/rud.is/b/wp-content/uploads/2017/07/Developer_Tools_-_http___thedailyviz_com_2017_07_25_chart-if-ousted-jeff-sessions-would-have-a-historically-short-tenure_.png?ssl=1" target="_blank"><img alt="" src="https://i0.wp.com/rud.is/b/wp-content/uploads/2017/07/Developer_Tools_-_http___thedailyviz_com_2017_07_25_chart-if-ousted-jeff-sessions-would-have-a-historically-short-tenure_.png?resize=780%2C709&ssl=1" class="wp-image-6116" data-recalc-dims="1"></a></p>
<p>It’s still easier to get it from there vs re-scrape Wikipedia (like Matt did) thanks to the <code>V8</code> package by <a rel="nofollow" href="http://twitter.com/opencpu" target="_blank">@opencpu</a>.</p>
<p>The following code:</p>
<ul>
<li>grabs the vis iframe</li>
<li>extracts and evaluates the target javascript to get a nice data frame</li>
<li>performs some factor re-coding (for better grouping and to make it easier to identify Sessions)</li>
<li>plots the distributions using the beeswarm quasirandom alogrithm</li>
</ul>
<pre><code>library(V8)
library(rvest)
library(ggbeeswarm)
library(hrbrthemes)
library(tidyverse)

pg &lt;- read_html(&quot;http://mattstiles.org/dailygraphics/graphics/attorney-general-tenure-20172517/child.html?initialWidth=840&amp;childId=pym_0&amp;parentTitle=Chart%3A%20If%20Ousted%2C%20Jeff%20Sessions%20Would%20Have%20a%20Historically%20Short%20Tenure%20%7C%20The%20Daily%20Viz&amp;parentUrl=http%3A%2F%2Fthedailyviz.com%2F2017%2F07%2F25%2Fchart-if-ousted-jeff-sessions-would-have-a-historically-short-tenure%2F&quot;)

ctx &lt;- v8()
ctx$eval(html_nodes(pg, xpath=&quot;.//script[contains(., 'DATA')]&quot;) %>% html_text())

ctx$get(&quot;DATA&quot;) %>% 
  as_tibble() %>% 
  readr::type_convert() %>% 
  mutate(party = ifelse(is.na(party), &quot;Other&quot;, party)) %>% 
  mutate(party = fct_lump(party)) %>% 
  mutate(color1 = case_when(
    party == &quot;Democratic&quot; ~ &quot;#313695&quot;,
    party == &quot;Republican&quot; ~ &quot;#a50026&quot;,
    party == &quot;Other&quot; ~ &quot;#4d4d4d&quot;)
  ) %>% 
  mutate(color2 = ifelse(grepl(&quot;Sessions&quot;, label), &quot;#2b2b2b&quot;, &quot;#00000000&quot;)) -> ags

ggplot() + 
  geom_quasirandom(data = ags, aes(party, amt, color = color1)) +
  geom_quasirandom(data = ags, aes(party, amt, color = color2), 
                   fill = &quot;#ffffff00&quot;, size = 4, stroke = 0.25, shape = 21) +
  geom_text(data = data_frame(), aes(x = &quot;Republican&quot;, y = 100, label = &quot;Jeff Sessions&quot;), 
            nudge_x = -0.15, family = font_rc, size = 3, hjust = 1) +
  scale_color_identity() +
  scale_y_comma(limits = c(0, 4200)) +
  labs(x = &quot;Party&quot;, y = &quot;Tenure (days)&quot;, 
       title = &quot;U.S. Attorneys General&quot;,
       subtitle = &quot;Distribution of tenure in office, by days &amp; party: 1789-2017&quot;,
       caption = &quot;Source data/idea: Matt Stiles &lt;bit.ly/2vXAHTM>&quot;) +
  theme_ipsum_rc(grid = &quot;XY&quot;)</code></pre>
<p><a rel="nofollow" href="https://i0.wp.com/rud.is/b/wp-content/uploads/2017/07/plot_zoom_png-2.png?ssl=1" target="_blank"><img alt="" src="https://i0.wp.com/rud.is/b/wp-content/uploads/2017/07/plot_zoom_png-2.png?resize=780%2C699&ssl=1" class="wp-image-6117" data-recalc-dims="1"></a></p>
<p>I turned the data into a CSV and stuck it in <a rel="nofollow" href="https://gist.github.com/hrbrmstr/c9eddb30e6fb5038085eb30b7d94f556" target="_blank">this gist</a> if folks want to play w/o doing the js scraping.</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/07/25/r%E2%81%B6-general-attys-distributions/"> 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 rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/oH7QLtuBXhE" height="1"></a></div>