<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/daily-metric-tracking-with-ggplot2/"></div></div><div><a data-url="https://www.r-bloggers.com/daily-metric-tracking-with-ggplot2/" data-counturl="https://www.r-bloggers.com/daily-metric-tracking-with-ggplot2/" data-count="horizontal" data-text="Daily metric tracking with ggplot2" 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://johnmackintosh.com/2016-11-27-calendar-heatmaps/"> HighlandR</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>In my role I have to produce a lot of run charts and control charts for monthly data, but sometimes there are processes behind the run charts that merit looking at the data on a daily basis.</p>
<p>It can become impractical to start producing run charts split by day across all metrics (e.g. one for Sunday, one for Monday etc.) so as a quick way of visualising data and spotting patterns for further investigation I’m starting to quite like heatmaps.</p>
<p>There are a few examples already of calendar style heatmaps in R – specifically Paul Bleicher’s <a target="_blank" rel="nofollow" href="http://blog.revolutionanalytics.com/2009/11/charting-time-series-as-calendar-heat-maps-in-r.html">CalendarHeat</a> function based on lattice (see link for discussion), and the <a target="_blank" rel="nofollow" href="https://github.com/Ather-Energy/ggTimeSeries">ggTimeSeries</a> extension which gives us similar functionality within ggplot2.</p>
<p>Generally, I’ve seen these used for tracking an individual metric over an extended time period (e.g. a year or more). I have never got to grips with the lattice system, so I decided to have a bash at producing some thing loosely based on these but using ggplot2 instead.</p>
<p>I had 2 aims:<br>
– I wanted something that would allow me to visualise data for a larger number of metrics, but over a much smaller time period<br>
– It was absolutely not to be a run chart or some other line plot (in my defence, I’ve produced 1000’s of these already and will be producing many more in the future)</p>
<p>Before we go on please bear in mind this is <em>randomly generated</em> data (<em>from the future!</em>) – so it is very much pot luck as to how effective these look. The aim was to have a mix of high, mid and low frequency variables to mirror typical metrics within most organisations:</p>
<h3>All metrics, month at a glance</h3>
<p><img alt="2016-11-27-Heatmapnolabels.png" src="https://i1.wp.com/johnmackintosh.com/img/2016-11-27-Heatmapnolabels.png?w=456" data-recalc-dims="1"></p>
<p>This gives a nice overview of a dozen metrics, per day, for a given month, in this case, Dec 2016 – which, at the time of writing, hasn’t happened yet (I did say this was made up didn’t I?)</p>
<p>The first question that arises – to label, or not? Generally heatmaps aren’t labelled, and I like the purity of the above plot, but unless it’s made available as an interactive plot , the audience has no way to accurately determine the values.</p>
<p>Speaking of interactive, here is how this would look using plotly within R:</p>
<p><img alt="2016-11-27plotlyheatmap3.png" src="https://i1.wp.com/johnmackintosh.com/img/2016-11-27plotlyheatmap3.png?w=456" data-recalc-dims="1"></p>
<p>Plotly is really nice, but I can see that people may not want to be hovering over each tile to determine the value, especially if its a plot type that is new to them.</p>
<p>So, bearing in mind I generally have to export plots as static images, here is a labelled version:</p>
<p><img alt="2016-11-27-Heatmapwithlabels.png" src="https://i0.wp.com/johnmackintosh.com/img/2016-11-27-Heatmapwithlabels.png?w=456" data-recalc-dims="1"></p>
<p>Here I overlaid labels with <code>geom_text</code> , after creating separate dataframes to control the text colour – depending on the values within the data you may have to play around with this.</p>
<p>One criticism of these is that the 0 values are filled and so it becomes difficult to determine much variance between these and the other values for metrics A-C.</p>
<p>A way round that is to rescale each metric individually to a value between 0 and 1, to standardise the variance in the fill shades across metrics. You then use the rescaled value as the basis for plotting – and overlay with the labels as before:</p>
<p><img alt="2016-11-27RescaledMonthViewHeatmapWithLabels.png" src="https://i1.wp.com/johnmackintosh.com/img/2016-11-27RescaledMonthViewHeatmapWithLabels.png?w=456" data-recalc-dims="1"></p>
<h3>Calendar style view by metric</h3>
<p>If you want to look at patterns by weekday, both individually and across all the metrics, then this layout can work well. First, here’s the labelled, unscaled version:</p>
<p><img alt="2016-MetricCalendarHeatmapWithLabels.png" src="https://i2.wp.com/johnmackintosh.com/img/2016-MetricCalendarHeatmapWithLabels.png?w=456" data-recalc-dims="1"></p>
<p>Here is the rescaled version, for comparison:</p>
<p><img alt="2016-11-27RescaledMetricCalendarHeatmapWithLabels.png" src="https://i1.wp.com/johnmackintosh.com/img/2016-11-27RescaledMetricCalendarHeatmapWithLabels.png?w=456" data-recalc-dims="1"></p>
<p>Bearing in mind this is entirely randomly generated data, you can still see some patterns here for some metrics. If you try something similar with your own data you may find something that was previously “non-obvious” now stands out. Then you can delve deeper if there is specific day or sequence that catches your attention.</p>
<h3>Lose the zeros</h3>
<p>This speaks for itself, here is the first plot again but this time I stripped out the zero values so they did not get filled in with the same palette as the other values.</p>
<p><img alt="2016-11-27-HeatmapWhiteZeroWithLabels.png" src="https://i0.wp.com/johnmackintosh.com/img/2016-11-27-HeatmapWhiteZeroWithLabels.png?w=456" data-recalc-dims="1"></p>
<p>You may have to denote that white = 0, or it may be assumed that these are missing values instead. Again, the best approach depends on the data and your audience.</p>
<h3>Summary</h3>
<p>Heatmaps may not be everyone’s cup of tea. You could use more robust methods to determine whether 2 or more metrics increase together or not. There are also better ways of visualising these relationships, like the awesome looking <a target="_blank" rel="nofollow" href="https://drsimonj.svbtle.com/explore-correlations-in-r-with-corrr">corrr</a> package.</p>
<p>That said, I still like heatmaps as a quick way to visualise high volume/high frequency data underlying some of the high level metrics that I routinely measure. You don’t know what previously unseen patterns may emerge, so there is nothing to lose by plotting them. I also find that people who may not be huge fans of charts (there are some strange folk who prefer tables), actually <em>do</em> like heatmaps. Again, once people get engaged then you can move on to other visualisations in due course.</p>
<p>I’m going to finish off with a plot I produced earlier in the year, for a talk I did on ggplot2. This shows data by hour, day, month and year. Its based on a very old dataset from the <code>Interpol.T</code> library – I wanted something that would allow me to plot hourly data by day and I have used similar plots with real-life data as well:</p>
<p><img alt="2016-11-27temps3.png" src="https://i0.wp.com/johnmackintosh.com/img/2016-11-27temps3.png?w=456" data-recalc-dims="1"></p>
<p>It looks better on screen, it has to be said – it got a bit squished during the export process – but you get the idea. I’ve been trying to recreate something like this with QlikView and that is still very much a work in progress, whereas ggplot2 and lubridate make it easier to take an idea and see it through to reality.</p>
<p>That’s all for this post, hope you liked this and hope it has sparked some ideas for taking an alternative look at your data.</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="https://www.r-bloggers.com/daily-metric-tracking-with-ggplot2/"></div></div><div><a data-url="https://www.r-bloggers.com/daily-metric-tracking-with-ggplot2/" data-counturl="https://www.r-bloggers.com/daily-metric-tracking-with-ggplot2/" data-count="vertical" data-text="Daily metric tracking with ggplot2" 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://johnmackintosh.com/2016-11-27-calendar-heatmaps/"> HighlandR</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/?s=tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/?s=data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/?s=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/?s=ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/?s=map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/?s=animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/?s=RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/?s=sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/?s=LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/?s=SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/?s=eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/?s=git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/?s=hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/?s=Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/?s=PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/?s=time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/?s=trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/h8IkuacEDN4"></a></div>