The hourly heatmap with ggplot2

[This article was first published on HighlandR, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

a quick follow up to my last post –

I’ve had a few folk get in touch following my last post, all commenting on the last plot :

2016-11-30DailyTemps.png

Most of these enquiries went along the following lines:

“I hadn’t thought of doing that. It looks really easy. But how do you do it?”

Unfortunately I was away the last couple of days without access to the code, and I didn’t want to pass on the wrong info. Back home now though, and the code is now uploaded as a gist to GitHub :

HeatmapHrbyDay.R

What is the use case? Any organisation/operation that is 24/7 will have some sort of data they can monitor via such a plot. Server loads / web traffic etc could also be analysed this way.

Some comments :

  • The Interpol dataset is pretty large, so we filter out for the first weather station, which conveniently had 2 full years data. We remove all the data we don’t need as we go.

  • I use the ggExtra package for easy removal of gridlines (and depending on the plot, easy rotation of axis labels). I know these are not the main functions or reason for the ggExtra package existing, but these commands are easy to remember. Big thanks to Dean Attali for all his work on this and other cool R packages. You don’t have to use this package to remove the gridlines, but I will leave you to find the necessary syntax (hint : check my first blog post).

  • The beauty and flexibility of ggplot2 is demonstrated once again. The bulk of the plot is produced with these few lines:

    p <-ggplot(df,aes(day,hour,fill=temp))+  
    geom_tile(color= "white",size=0.1) +  
    scale_fill_viridis(name=" Hrly Temps C",option ="C")  
    p <-p + facet_grid(year~month)  

If you type ‘p’ in the console at this point you will see the plot. The rest of the code is formatting tweaks. For more heatmap awesomeness, check out Bob Rudis’s blog:
Bob Rudis faceted heatmaps

I think I’m done with heatmaps for the time being - hope this helped you visualise your data, would love to hear from you if it did.

To leave a comment for the author, please follow the link and comment on their blog: HighlandR.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)