<div>Here’s my belated Statistics Sunday post, using a cool technique I just learned about: <a href="https://www.littlemissdata.com/blog/highlight">gghighlight</a>. This R package works with ggplot2 to highlight a subset of data. To demonstrate, I’ll use a dataset I analyzed for a previous post about my <a href="http://www.deeplytrivial.com/2018/01/data-analysis-of-my-2017-reading.html">2017 reading habits</a>. [Side note: My <a href="https://www.goodreads.com/challenges/7501-2018-reading-challenge">reading goal for this year</a> is 60 books, and I’m already at 43! I may have to increase my goal at some point.]
<div>
<div>
<div>
<pre><span>setwd</span><span>(</span><span>&quot;~/R&quot;</span><span>)</span><br><span>library</span><span>(tidyverse)</span><br></pre>
</div>
<div>
<pre><span>books</span><span>&lt;-</span><span>read_csv</span><span>(</span><span>&quot;2017_books.csv&quot;</span><span>,</span> <span>col_names</span> <span>=</span> <span>TRUE</span><span>)</span><br></pre>
</div>
<div>
<pre>## Warning: Duplicated column names deduplicated: 'Author' => 'Author_1' [13]<br></pre>
</div>
<div>
<pre>## Parsed with column specification:<br>## cols(<br>##   .default = col_integer(),<br>##   Title = col_character(),<br>##   Author = col_character(),<br>##   G_Rating = col_double(),<br>##   Started = col_character(),<br>##   Finished = col_character()<br>## )<br></pre>
</div>
<div>
<pre>## See spec(...) for full column specifications.<br></pre>
</div>
</div>
</div>
<p>One analysis I conducted with this dataset was to look at the correlation between book length (number of pages) and read time (number of days it took to read the book). We can also generate a scatterplot to visualize this relationship.</p>
<div>
<div>
<div>
<pre><span>cor.test</span><span>(books</span><span>$</span><span>Pages, books</span><span>$</span><span>Read_Time)</span><br></pre>
</div>
<div>
<pre>## <br>## 	Pearson's product-moment correlation<br>## <br>## data:  books$Pages and books$Read_Time<br>## t = 3.1396, df = 51, p-value = 0.002812<br>## alternative hypothesis: true correlation is not equal to 0<br>## 95 percent confidence interval:<br>##  0.1482981 0.6067498<br>## sample estimates:<br>##       cor <br>## 0.4024597<br></pre>
</div>
<div>
<pre><span>scatter</span> <span>&lt;-</span> <span>ggplot</span><span>(books,</span> <span>aes</span><span>(Pages, Read_Time))</span> <span>+</span><br>  <span>geom_point</span><span>(</span><span>size</span> <span>=</span> <span>3</span><span>)</span> <span>+</span><br>  <span>theme_classic</span><span>()</span> <span>+</span><br>  <span>labs</span><span>(</span><span>title</span> <span>=</span> <span>&quot;Relationship Between Reading Time and Page Length&quot;</span><span>)</span> <span>+</span><br>  <span>ylab</span><span>(</span><span>&quot;Read Time (in days)&quot;</span><span>)</span> <span>+</span><br>  <span>xlab</span><span>(</span><span>&quot;Number of Pages&quot;</span><span>)</span> <span>+</span><br>  <span>theme</span><span>(</span><span>legend.position</span><span>=</span><span>&quot;none&quot;</span><span>,</span><span>plot.title</span><span>=</span><span>element_text</span><span>(</span><span>hjust</span><span>=</span><span>0.5</span><span>))</span><br></pre>
</div>
</div>
</div>
<p></p>
<div><a rel="nofollow" href="https://i2.wp.com/4.bp.blogspot.com/--8rUkl6PxWE/W2ndA5Y0qZI/AAAAAAAANY0/zp8nzbQ4gW4iNlVQqT44fwhV4DaYFI35gCLcBGAs/s1600/basic_scatter.png?ssl=1" target="_blank"><img border="0" src="https://i1.wp.com/4.bp.blogspot.com/--8rUkl6PxWE/W2ndA5Y0qZI/AAAAAAAANY0/zp8nzbQ4gW4iNlVQqT44fwhV4DaYFI35gCLcBGAs/s320/basic_scatter.png?resize=320%2C229&ssl=1" data-original-width="450" width="320" data-original-height="486" data-recalc-dims="1" height="229"></a></div>
<p>There’s a significant <a href="http://www.deeplytrivial.com/2018/01/statistical-sins-types-of-statistical.html">positive</a> correlation here, meaning the longer books take more days to read. It’s a moderate correlation, and there are certainly other variables that may explain why a book took longer to read. For instance, nonfiction books may take longer. Books read in October or November (while I was gearing up for and participating in NaNoWriMo, respectively) may also take longer, since I had less spare time to read. I can conduct regressions and other analyses to examine which variables impact read time, but one of the most important parts of sharing results is creating good data visualizations. How can I show the impact these other variables have on read time in an understandable and visually appealing way?</p>
<p>gghighlight will let me draw attention to different parts of the plot. For example, I can ask gghighlight to draw attention to books that took longer than a certain amount of time to read, and I can even ask it to label those books.</p>
<div>
<div>
<div>
<pre><span>library</span><span>(gghighlight)</span><br></pre>
</div>
<div>
<pre><span>scatter</span> <span>+</span> <span>gghighlight</span><span>(Read_Time</span> <span>></span> <span>14</span><span>)</span> <span>+</span><br>  <span>geom_label</span><span>(</span><span>aes</span><span>(</span><span>label</span> <span>= Title),</span><br>             <span>hjust</span> <span>=</span> <span>1</span><span>,</span><br>             <span>vjust</span> <span>=</span> <span>1</span><span>,</span><br>             <span>fill</span> <span>=</span> <span>&quot;blue&quot;</span><span>,</span><br>             <span>color</span> <span>=</span> <span>&quot;white&quot;</span><span>,</span><br>             <span>alpha</span> <span>=</span> <span>0.5</span><span>)</span><br></pre>
</div>
</div>
<p></p>
<div><a rel="nofollow" href="https://i2.wp.com/4.bp.blogspot.com/-zyZzyICNCaY/W2nceP63EbI/AAAAAAAANYk/F7kiR5tEjYsv0AFtFqU1mPZdtBIsLeLfgCLcBGAs/s1600/bookstitles.png?ssl=1" target="_blank"><img border="0" src="https://i2.wp.com/4.bp.blogspot.com/-zyZzyICNCaY/W2nceP63EbI/AAAAAAAANYk/F7kiR5tEjYsv0AFtFqU1mPZdtBIsLeLfgCLcBGAs/s320/bookstitles.png?resize=320%2C228&ssl=1" data-original-width="450" width="320" data-original-height="891" data-recalc-dims="1" height="228"></a></div>
</div>


<p>Here, the gghighlight function identifies the subset (books that took more than 2 weeks to read) and labels those books with the Title variable. Three of the four books with long read time values are non-fiction, and one was read for a course I took, so reading followed a set schedule. But the fourth is a fiction book, which took over 20 days to read. Let’s see how month impacts reading time, by highlighting books read in November. To do that, I’ll need to alter my dataset somewhat. The dataset contains a starting date and finish date, which were read in as characters. I need to convert those to dates and pull out the month variable to create my indicator.</p>
<div>
<div>
<div>
<pre><span>library</span><span>(lubridate)</span><br></pre>
</div>
<div>
<pre>## <br>## Attaching package: 'lubridate'<br></pre>
</div>
<div>
<pre>## The following object is masked from 'package:base':<br>## <br>##     date<br></pre>
</div>
<div>
<pre><span>books</span><span>$</span><span>Started</span> <span>&lt;-</span> <span>mdy</span><span>(books</span><span>$</span><span>Started)</span><br><span>books</span><span>$</span><span>Start_Month</span> <span>&lt;-</span> <span>month</span><span>(books</span><span>$</span><span>Started)</span><br><span>books</span><span>$</span><span>Month</span> <span>&lt;-</span> <span>ifelse</span><span>(books</span><span>$</span><span>Start_Month</span> <span>></span> <span>10</span> <span>&amp;</span> <span>books</span><span>$</span><span>Start_Month</span> <span></span> <span>12</span><span>, books</span><span>$</span><span>Month</span> <span>&lt;-</span> <span>1</span><span>,</span><br>                      <span>books</span><span>$</span><span>Month</span> <span>&lt;-</span> <span>0</span><span>)</span><br><span>scatter</span> <span>+</span> <span>gghighlight</span><span>(books</span><span>$</span><span>Month</span> <span>==</span> <span>1</span><span>)</span> <span>+</span><br>  <span>geom_label</span><span>(</span><span>aes</span><span>(</span><span>label</span> <span>= Title),</span> <span>hjust</span> <span>=</span> <span>1</span><span>,</span> <span>vjust</span> <span>=</span> <span>1</span><span>,</span> <span>fill</span> <span>=</span> <span>&quot;blue&quot;</span><span>,</span> <span>color</span> <span>=</span> <span>&quot;white&quot;</span><span>,</span> <span>alpha</span> <span>=</span> <span>0.5</span><span>)</span><br></pre>
</div>
</div>
<p></p>
<div><a rel="nofollow" href="https://i0.wp.com/2.bp.blogspot.com/-CHyZ17fDkWo/W2ncu6eYNbI/AAAAAAAANYs/mPH6cmNgLu88tYOTttaK-yGNEl9xxlnOgCLcBGAs/s1600/unnamed-chunk-4-1.png?ssl=1" target="_blank"><img border="0" src="https://i2.wp.com/2.bp.blogspot.com/-CHyZ17fDkWo/W2ncu6eYNbI/AAAAAAAANYs/mPH6cmNgLu88tYOTttaK-yGNEl9xxlnOgCLcBGAs/s320/unnamed-chunk-4-1.png?resize=320%2C320&ssl=1" data-original-width="450" width="320" data-original-height="504" data-recalc-dims="1" height="320"></a></div>
</div>
<p>The book with the longest read time was, in fact, read during November, when I was spending most of my time writing. </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="http://www.deeplytrivial.com/2018/08/statistics-sunday-highlighting-subset.html"> Deeply Trivial</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/ohj5Gc8AIs0" height="1"></a></div></div>