<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/highlighting-with-ggplot2-the-old-school-and-new-school-way/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://www.littlemissdata.com/blog/highlight"> Little Miss Data</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>A while back I had <a rel="nofollow" href="https://twitter.com/LittleMissData/status/973195481483436032" target="_blank">tweeted</a> about a really cool technique that can be used with ggplot2 in R to highlight a subset of your data, while keeping in perspective the trend of the full data set.  I found out about this trick through a bit of a tangled web.  Please stay with me while I lay it out for you.   It all started with a tweet that <a rel="nofollow" href="https://twitter.com/dataandme" target="_blank">Mara Averick</a> shared from a <a rel="nofollow" href="https://drsimonj.svbtle.com/plotting-background-data-for-groups-with-ggplot2" target="_blank">blog</a> that<a rel="nofollow" href="https://twitter.com/drsimonj" target="_blank"> Simon Jackson</a> wrote about a technique that <a rel="nofollow" href="https://twitter.com/hadleywickham" target="_blank">Hadley </a><a rel="nofollow" href="https://twitter.com/hadleywickham" target="_blank">Wickham</a> discussed in his <a rel="nofollow" href="https://github.com/hadley/ggplot2-book" target="_blank">ggplot2 book.</a>   Confused yet?  Well, the good news is that actually implementing the technique is a lot easier than following the discovery path!</p>
<p> </p>
<p> <img data-image-dimensions="818x882" data-image-focal-point="0.5,0.5" src="https://i1.wp.com/static1.squarespace.com/static/58eef8846a4963e429687a4d/t/5b60b6deaa4a997c9e4c3a74/1533064996453/tweet.png?w=456&ssl=1" alt="tweet.png" data-load="false" data-type="image" data-image-id="5b60b6deaa4a997c9e4c3a74" data-recalc-dims="1" data-image="https://static1.squarespace.com/static/58eef8846a4963e429687a4d/t/5b60b6deaa4a997c9e4c3a74/1533064996453/tweet.png"></p>
<p> </p>
<h2>Highlighting The Old School Way</h2>
<p>To implement this idea, we don’t need any fancy packages other than ggplot2.  The steps are simple:</p>
<ol>
<li>Using ggplot2, create a plot with your full data set in grey.</li>
<li>Create a new data frame that has been subset to only include the data which you would like to highlight.  </li>
<li>Add the highlighted data on to your plot created in step 1.  Set the color to something other than grey.  </li>
<li>Celebrate!</li>
</ol>
<p><strong>Example</strong></p>
<p>For our example, we are going to examine the crime incident dataset from <a rel="nofollow" href="https://catalog.data.gov/dataset/seattle-police-department-911-incident-response-52779" target="_blank">Seattle 911 Calls on data.gov</a>.  Note that I have covered this data set through multiple blog posts already such as <a rel="nofollow" href="https://www.littlemissdata.com/blog/maps" target="_blank">map plots in R</a> and <a rel="nofollow" href="https://www.littlemissdata.com/blog/heatmaps" target="_blank">time based heat maps</a>.  </p>
<p><strong>Install and Load Libraries</strong></p>
<pre>install.packages(&quot;lubridate&quot;)
install.packages(&quot;ggplot2&quot;)
install.packages(&quot;ggmap&quot;)
install.packages(&quot;data.table&quot;)
install.packages(&quot;ggrepel&quot;)
install.packages(&quot;dplyr&quot;)
install.packages(&quot;magrittr&quot;)

library(lubridate)
library(ggplot2)
library(ggmap)
library(dplyr)
library(data.table)
library(ggrepel)
library(magrittr)</pre>
<p><strong>Download the Data</strong></p>
<pre>incidents= fread('https://raw.githubusercontent.com/lgellis/MiscTutorial/master/ggmap/i2Sample.csv', stringsAsFactors = FALSE)
str(incidents) attach(incidents)

# Create some color variables for graphing later
custGrey = &quot;#A9A9A9&quot;

#add year to the incidents data frame
incidents$ymd &lt;-mdy_hms(Event.Clearance.Date)
incidents$month &lt;- lubridate::month(incidents$ymd)
incidents$year &lt;- year(incidents$ymd)
incidents$wday &lt;- lubridate::wday(incidents$ymd, label = TRUE)
incidents$hour &lt;- hour(incidents$ymd)

#Create a more manageable data frame with only 2017 data
i2 &lt;- incidents[year>=2017, ]

#Only include complete cases
i2[complete.cases(i2), ]

attach(i2)
head(i2)
</pre>
<p><strong>Create a basic time series plot showing the count of 911 event types by month.  </strong></p>
<pre>#Group the data into a new data frame which has the count of events per month by subgroup

groupSummaries &lt;- i2 %>%
  group_by(month, Event.Clearance.SubGroup) %>%
  summarize(N = length(Event.Clearance.SubGroup))

#View the new data set

head(groupSummaries, n=100)
attach(groupSummaries)

#Graph the data set through ggplot 2

ggplot(groupSummaries, aes(x=month, y=N, color=Event.Clearance.SubGroup) )+ 
  geom_line() +
  theme(legend.position=&quot;bottom&quot;,legend.text=element_text(size=7),
        legend.title = element_blank()) +
  scale_x_discrete(name =&quot;Month&quot;, 
                 limits=c(3,6,9,12))

</pre>
<p> </p>
<p> <img data-image-dimensions="2150x1344" data-image-focal-point="0.5,0.5" src="https://static1.squarespace.com/static/58eef8846a4963e429687a4d/t/5b60ca6bf950b7b346af1068/1533069940424/?format=1000w" data-load="false" data-type="image" data-image-id="5b60ca6bf950b7b346af1068" data-image="https://static1.squarespace.com/static/58eef8846a4963e429687a4d/t/5b60ca6bf950b7b346af1068/1533069940424/"></p>
<p> </p>
<p> </p>
<p><strong>Create a Graph Highlighting Data with a Max Month Count of 95 or Greater</strong></p>
<pre># Create a data frame with only events types that have had a peak of 95 calls in a month or more

groupSummariesF &lt;- groupSummaries %>%
  group_by(Event.Clearance.SubGroup) %>% 
  filter(max(N) > 95) %>%
  ungroup()

head(groupSummariesF)

# Create a layered plot with one layer of grey data for the full data set and one layer of color data for the subset data set 

ggplot() +
  geom_line(aes(month, N, group = Event.Clearance.SubGroup), 
            data = groupSummaries, colour = alpha(&quot;grey&quot;, 0.7)) +
  geom_line(aes(month, N, group = Event.Clearance.SubGroup, colour = Event.Clearance.SubGroup), 
            data = groupSummariesF) +  
  scale_x_discrete(name =&quot;Month&quot;, 
                   limits=c(3,6,9,12)) +
  theme(legend.position=&quot;bottom&quot;,legend.text=element_text(size=7),
        legend.title = element_blank())

</pre>
<p> </p>
<p> <img data-image-dimensions="2134x1340" data-image-focal-point="0.5,0.5" src="https://i2.wp.com/static1.squarespace.com/static/58eef8846a4963e429687a4d/t/5b60ca95aa4a997c9e512cb6/1533069983459/timeseries_highlight.png?w=456&ssl=1" alt="timeseries_highlight.png" data-load="false" data-type="image" data-image-id="5b60ca95aa4a997c9e512cb6" data-recalc-dims="1" data-image="https://static1.squarespace.com/static/58eef8846a4963e429687a4d/t/5b60ca95aa4a997c9e512cb6/1533069983459/timeseries_highlight.png"></p>
<p> </p>
<p> </p>
<p>One of the great things about the “old school way” of doing this type of highlighting is that it can be done with presumably every extension to the ggplot2 package.  For example, you can use this same technique to highlight with the <a rel="nofollow" href="https://cran.r-project.org/web/packages/ggmap/ggmap.pdf" target="_blank">ggmap</a> package.   The code for these graphs is incredibly simple and has been included in my <a rel="nofollow" href="https://github.com/lgellis/MiscTutorial/tree/master/gghighlight" target="_blank">github repo</a>.  </p>
<p> <img data-image-dimensions="1936x792" data-image-focal-point="0.5,0.5" src="https://i2.wp.com/static1.squarespace.com/static/58eef8846a4963e429687a4d/t/5b60ccc71ae6cf38b8b400fa/1533070547746/Screen+Shot+2018-07-31+at+3.52.25+PM.png?w=456&ssl=1" alt="Screen Shot 2018-07-31 at 3.52.25 PM.png" data-load="false" data-type="image" data-image-id="5b60ccc71ae6cf38b8b400fa" data-recalc-dims="1" data-image="https://static1.squarespace.com/static/58eef8846a4963e429687a4d/t/5b60ccc71ae6cf38b8b400fa/1533070547746/Screen+Shot+2018-07-31+at+3.52.25+PM.png"></p>
<p> </p>
<h2>Highlighting The New School Way</h2>
<p>While the above methodology is quite easy, it can be a bit of a pain at times to create and add the new data frame.  Further, you have to tinker more with the labelling to really call out the highlighted data points.  </p>
<p>Thanks to <a rel="nofollow" href="https://twitter.com/yutannihilation" target="_blank">Hiroaki Yutani</a>, we now have the <a rel="nofollow" href="https://github.com/yutannihilation" target="_blank">gghighlight</a><a rel="nofollow" href="https://github.com/yutannihilation" target="_blank"> package</a> which does most of the work for us with a small function call!!   Please note that a lot of this code was created by looking at examples on her <a rel="nofollow" href="https://yutannihilation.github.io/gghighlight/articles/gghighlight.html" target="_blank">introduction document</a>. </p>
<p>The new school way is even more simple:</p>
<ol>
<li>Using ggplot2, create a plot with your full data set.</li>
<li>Add the gghighlight() function to your plot with the conditions set to identify your subset.</li>
<li>Celebrate.</li>
</ol>
<p><strong>Example</strong></p>
<p>For our first example, we are going to create the same time series graph from above.  However, we are going to perform the highlighting with gghighlight vs manual layering.  </p>
<pre># Install the gghighlight package

install.packages(&quot;gghighlight&quot;)
library(gghighlight)

# Create the highlighted graph

ggplot(groupSummaries, aes(month, N, colour = Event.Clearance.SubGroup)) +
  geom_line() + 
  gghighlight(max(N) > 95,  label_key = Event.Clearance.SubGroup) +  
  scale_x_discrete(name =&quot;Month&quot;, 
                   limits=c(3,6,9,12))</pre>
<p> </p>
<p> <img data-image-dimensions="2124x1364" data-image-focal-point="0.5,0.5" src="https://i1.wp.com/static1.squarespace.com/static/58eef8846a4963e429687a4d/t/5b60d0ea575d1f505a73d12c/1533071605568/gghighlight1.png?w=456&ssl=1" alt="gghighlight1.png" data-load="false" data-type="image" data-image-id="5b60d0ea575d1f505a73d12c" data-recalc-dims="1" data-image="https://static1.squarespace.com/static/58eef8846a4963e429687a4d/t/5b60d0ea575d1f505a73d12c/1533071605568/gghighlight1.png"></p>
<p> </p>
<p><strong>More Examples</strong></p>
<p>Well that was so easy, we are going to try a few more ggmap plot types to see how we fare.  Below show both a scatterplot and histogram chart.  </p>
<pre># Try a scatterplot chart

ggplot(groupSummaries, aes(month, N, colour = Event.Clearance.SubGroup, use_group_by=FALSE)) +
  geom_point() +
  gghighlight(N > 200,  label_key = Event.Clearance.SubGroup) + 
  scale_x_discrete(name =&quot;Month&quot;,
                   limits=c(3,6,9,12))

# Try a histogram chart

ggplot(groupSummaries, aes(N, fill = Event.Clearance.SubGroup)) +
  geom_histogram() +
  theme(legend.position=&quot;bottom&quot;,legend.text=element_text(size=7),
        legend.title = element_blank()) +
  gghighlight(N > 100,  label_key = Event.Clearance.SubGroup, use_group_by = FALSE) +
  facet_wrap(~ Event.Clearance.SubGroup)</pre>
<p> <img data-image-dimensions="1558x1358" data-image-focal-point="0.5,0.5" src="https://i0.wp.com/static1.squarespace.com/static/58eef8846a4963e429687a4d/t/5b60d1c18a922d43bf07b429/1533071825274/Screen+Shot+2018-07-31+at+4.15.53+PM.png?w=456&ssl=1" alt="Screen Shot 2018-07-31 at 4.15.53 PM.png" data-load="false" data-type="image" data-image-id="5b60d1c18a922d43bf07b429" data-recalc-dims="1" data-image="https://static1.squarespace.com/static/58eef8846a4963e429687a4d/t/5b60d1c18a922d43bf07b429/1533071825274/Screen+Shot+2018-07-31+at+4.15.53+PM.png"></p>
<p> <img data-image-dimensions="1544x1346" data-image-focal-point="0.5,0.5" src="https://i0.wp.com/static1.squarespace.com/static/58eef8846a4963e429687a4d/t/5b60d1db70a6ad79d56dd620/1533071850216/Screen+Shot+2018-07-31+at+4.16.10+PM.png?w=456&ssl=1" alt="Screen Shot 2018-07-31 at 4.16.10 PM.png" data-load="false" data-type="image" data-image-id="5b60d1db70a6ad79d56dd620" data-recalc-dims="1" data-image="https://static1.squarespace.com/static/58eef8846a4963e429687a4d/t/5b60d1db70a6ad79d56dd620/1533071850216/Screen+Shot+2018-07-31+at+4.16.10+PM.png"></p>
<h2>THANK YOU</h2>
<p>Thanks for reading along while we explored data highlighting through layers and gghighlight.  Please share your thoughts and creations with me on <a rel="nofollow" href="https://twitter.com/LittleMissData" target="_blank">twitter</a>. </p>
<p>Note that the full code is available on my <a rel="nofollow" href="https://github.com/lgellis/MiscTutorial/tree/master/twitter_wordcloud" target="_blank"> </a><a rel="nofollow" href="https://github.com/lgellis/MiscTutorial/tree/master/gghighlight" target="_blank">github</a><a rel="nofollow" href="https://github.com/lgellis/MiscTutorial/tree/master/gghighlight" target="_blank"> repo</a><a rel="nofollow" href="https://github.com/lgellis/MiscTutorial/tree/master/Heatmap" target="_blank">.</a>  If you have trouble downloading the file from github, go to the <a rel="nofollow" href="https://github.com/lgellis/MiscTutorial" target="_blank">main page of the repo</a> and select “Clone or Download” and then “Download Zip”.</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://www.littlemissdata.com/blog/highlight"> Little Miss Data</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/06vHDLD5Zj8" height="1"></a></div>