<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="http://www.r-bloggers.com/making-time-rivers-in-r/"></div></div><div><a data-url="http://www.r-bloggers.com/making-time-rivers-in-r/" data-counturl="http://www.r-bloggers.com/making-time-rivers-in-r/" data-count="horizontal" data-text="Making “Time Rivers” in R" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://rud.is/b/2016/06/28/making-time-rivers-in-r/"> R – rud.is</a></strong>, and kindly contributed to <a rel="nofollow" href="http://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>Once again, <a target="_blank" href="http://twitter.com/albertocairo">@albertocairo</a> notices an interesting chart and spurs pondering in the visualization community with <a target="_blank" href="http://www.thefunctionalart.com/2016/06/defying-conventions-in-visualization.html">his post</a> covering an unusual “vertical time series” chart produced for the print version of the NYTimes:</p>
<p><a target="_blank" href="http://i1.wp.com/rud.is/b/wp-content/uploads/2016/06/IMG_0509-1.jpg?ssl=1"><img alt="IMG_0509-1" class="wp-image-4475" src="http://i1.wp.com/rud.is/b/wp-content/uploads/2016/06/IMG_0509-1.jpg?w=70%25&ssl=1" data-recalc-dims="1"></a></p>
<p>I’m actually less concerned about the vertical time series chart component here since I agree with TAVE<sup>*</sup> Cairo that folks are smart enough to grok it and that it will be a standard convention soon enough given the prevalence of our collective tiny, glowing rectangles. The Times folks plotted Martin-Quinn (M-Q) scores for the U.S. Supreme Court justices which are <i>estimates</i> of how liberal or conservative a justice was in a particular term. Since they are <i>estimates</i> they aren’t exact and while it’s fine to plot the mean value (as suggested by the M-Q folks), if we’re going to accept the intelligence of the reader to figure out the nouveau time series layout, perhaps we can also show them some of the <i>uncertainty</i> behind these estimates.</p>
<p>What I’ve done below is take the data provided by the M-Q folks and make what I’ll call a vertical time series river plot using the mean, median and one standard deviation. This shows the possible range of real values the estimates can take and provides a less-precise but more forthright view of the values (in my opinion). You can see right away that they estimates are not so precise, but there is still an overall trend for the justices to become more liberal in modern times.</p>
<p><a target="_blank" href="http://i2.wp.com/rud.is/b/wp-content/uploads/2016/06/Cursor_and_RStudio-1.png?ssl=1"><img alt="Cursor_and_RStudio" class="wp-image-4476" src="http://i2.wp.com/rud.is/b/wp-content/uploads/2016/06/Cursor_and_RStudio-1.png?w=90%25&ssl=1" data-recalc-dims="1"></a></p>
<p>The ggplot2 code is a bit intricate, which is one reason I’m posting it. You need to reorient your labeling mind due to the need to use <code>coord_flip()</code>. I also added an arrow on the Y-axis to show how time flows. I think the vis community will need to help standardize on some good practices for how to deal with these vertical time series charts to help orient readers more quickly. In a more dynamic visualization, either using something like D3 or even just stop-motion animation, the flow could actually draw in the direction time flows, which would definitely make it easier immediately orient the reader.</p>
<p>However, the main point here is to not be afraid to show uncertainty. In fact, the more we all work at it, the better we’ll all be able to come up with effective ways to show it.</p>
<p><sup>*</sup> == “The Awesome Visualization Expert” since he winced at my use of “Dr. Cairo” <img alt="🙂" class="wp-smiley" src="https://s.w.org/images/core/emoji/72x72/1f642.png"></p>
<pre><code>library(dplyr)
library(readr)
library(ggplot2)  # devtools::install_github(&quot;hadley/ggplot2&quot;)
library(hrbrmisc) # devtools::install_github(&quot;hrbrmstr/hrbrmisc&quot;)
library(grid)
library(scales)

URL &lt;- &quot;http://mqscores.berkeley.edu/media/2014/justices.csv&quot;
fil &lt;- basename(URL)
if (!file.exists(fil)) download.file(URL, fil)

justices &lt;- read_csv(fil)

justices %>%
  filter(term>=1980,
         justiceName %in% c(&quot;Thomas&quot;, &quot;Scalia&quot;, &quot;Alito&quot;, &quot;Roberts&quot;, &quot;Kennedy&quot;,
                            &quot;Breyer&quot;, &quot;Kagan&quot;, &quot;Ginsburg&quot;, &quot;Sotomayor&quot;)) %>%
  mutate(col=ifelse(justiceName %in% c(&quot;Breyer&quot;, &quot;Kagan&quot;, &quot;Ginsburg&quot;, &quot;Sotomayor&quot;),
                    &quot;Democrat&quot;, &quot;Republican&quot;)) -> recent

just_labs &lt;- data_frame(
  label=c(&quot;Thomas&quot;, &quot;Scalia&quot;, &quot;Alito&quot;, &quot;Roberts&quot;, &quot;Kennedy&quot;, &quot;Breyer&quot;, &quot;Kagan&quot;, &quot;Ginsburg&quot;, &quot;Sotomayor&quot;),
      x=c(  1990.5,   1985.5,  2004.5,    2004.5,    1986.5,      1994,   2010,     1992.5,      2008.5),
      y=c(     2.9,      1.4,    1.35,       1.7,       1.0,      -0.1,   -0.9,       -0.1,          -2)
)

gg &lt;- ggplot(recent)
gg &lt;- gg + geom_hline(yintercept=0, alpha=0.5)
gg &lt;- gg + geom_label(data=data.frame(x=c(0.1, -0.1),
                                      label=c(&quot;More →\nconservative&quot;, &quot;← More\nliberal&quot;),
                                      hjust=c(0, 1)), aes(y=x, x=1982, hjust=hjust, label=label),
                      family=&quot;Arial Narrow&quot;, fontface=&quot;bold&quot;, size=4, label.size=0, vjust=1)
gg &lt;- gg + geom_ribbon(aes(ymin=post_mn-post_sd, ymax=post_mn+post_sd, x=term,
                             group=justice, fill=col, color=col), size=0.1, alpha=0.3)
gg &lt;- gg + geom_line(aes(x=term, y=post_med, color=col, group=justice), size=0.1)
gg &lt;- gg + geom_text(data=just_labs, aes(x=x, y=y, label=label),
                     family=&quot;Arial Narrow&quot;, size=2.5)
gg &lt;- gg + scale_x_reverse(expand=c(0,0), limits=c(2014, 1982),
                           breaks=c(2014, seq(2010, 1990, -10), 1985, 1982),
                           labels=c(2014, seq(2010, 1990, -10), &quot;1985\nTERM\n↓&quot;, &quot;&quot;))
gg &lt;- gg + scale_y_continuous(expand=c(0,0), labels=c(-2, &quot;0\nM-Q Score&quot;, 2, 4))
gg &lt;- gg + scale_color_manual(name=NULL, values=c(Democrat=&quot;#2166ac&quot;, Republican=&quot;#b2182b&quot;), guide=FALSE)
gg &lt;- gg + scale_fill_manual(name=&quot;Nominated by a&quot;, values=c(Democrat=&quot;#2166ac&quot;, Republican=&quot;#b2182b&quot;))
gg &lt;- gg + coord_flip()
gg &lt;- gg + labs(x=NULL, y=NULL,
                title=&quot;Martin-Quinn scores for selected justices, 1985-2014&quot;,
                subtitle=&quot;Ribbon band derived from mean plus one standard deviation. Inner line is the M-Q median.&quot;,
                caption=&quot;Data source: http://mqscores.berkeley.edu/measures.php&quot;)
gg &lt;- gg + theme_hrbrmstr_an(grid=&quot;XY&quot;)
gg &lt;- gg + theme(plot.subtitle=element_text(margin=margin(b=15)))
gg &lt;- gg + theme(legend.title=element_text(face=&quot;bold&quot;))
gg &lt;- gg + theme(legend.position=c(0.05, 0.6))
gg &lt;- gg + theme(plot.margin=margin(20,20,20,20))
gg</code></pre>
<p>Yes, I manually positioned the names of the justices, hence the weird spacing for those lines. Also, after publishing this post, I tweaked the line-height of the “More Liberal”/”More Conservative” top labels a bit and would definitely suggest doing that to anyone attempting to reproduce this code (the setting I used was <code>0.9</code>).</p>
<p><div><div><div><div data-width="55" data-show-faces="false" data-send="true" data-layout="box_count" data-height="62" data-href="http://www.r-bloggers.com/making-time-rivers-in-r/"></div></div><div><a data-url="http://www.r-bloggers.com/making-time-rivers-in-r/" data-counturl="http://www.r-bloggers.com/making-time-rivers-in-r/" data-count="vertical" data-text="Making “Time Rivers” in R" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://rud.is/b/2016/06/28/making-time-rivers-in-r/"> R – rud.is</a></strong>.</div>
<hr>
<a rel="nofollow" href="http://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="http://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="http://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="http://www.r-bloggers.com/?s=data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="http://www.r-bloggers.com/?s=Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="http://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="http://www.r-bloggers.com/?s=map">maps</a>, <a title="Animation in R" rel="nofollow" href="http://www.r-bloggers.com/?s=animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="http://www.r-bloggers.com/?s=RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="http://www.r-bloggers.com/?s=sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="http://www.r-bloggers.com/?s=LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="http://www.r-bloggers.com/?s=SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="http://www.r-bloggers.com/?s=eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="http://www.r-bloggers.com/?s=git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="http://www.r-bloggers.com/?s=hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="http://www.r-bloggers.com/?s=Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="http://www.r-bloggers.com/?s=PCA">PCA</a>, <a title="Time series" rel="nofollow" href="http://www.r-bloggers.com/?s=time+series">time series</a>, <a title="finance trading" rel="nofollow" href="http://www.r-bloggers.com/?s=trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/z1uwT5bgg5o"></a></div>