An interactive visualization of London’s crime data using Shiny applications and R

David Morison
3 min readApr 19, 2017

After becoming familiar working with the GIS packages in R and plotting the locations of different crimes in London in a previous post, I encountered some limitations such as the amount of data that can be visualized at one time on a map. Instead of making a separate map to plot different crimes for each month I thought it would be a better idea to create an interactive plot using Shiny.

Shiny is a web application framework for R created by RStudio. It allows you to turn your R code into interactive visualizations, which you can then deploy on a server or host it on shinyapps.

With this in mind and my idea I had for extending the maps I created for plotting London's crime I did some Googling to learn the basics of building a Shiny application. The Shiny website has some good tutorials for getting started. Although you don’t need any web development skills I did find that having a background in web development made this process even easier.

Basically a Shiny app requires two parts, a ui to handle the visual interface of the application and a server to interpret the interactions coming from the ui. Once I was familiar with the basic functions needed for each of these it was just a matter of looking at a number of examples, figuring out some of the necessary syntax and applying the R code I had for plotting the maps from my previous project. Below is the code for my application.

The application allows you to toggle between the different types of crime for selected months of the year whose locations are then plotted alongside on the map. As an additional feature to the crime location map I thought it would be interesting to see how much the amount of each crime differs between the months throughout the year. For this I included a simple chart below the map, which plots the trend for the selected crimes. The only caveat is when one of the crime types selected has a significantly higher amount compared to the other crimes selected it is difficult to visualize the trend due to the increased scale. I did enjoy seeing how the trend in bicycle theft followed a clear seasonal pattern where more incidences take place during the summer months when the weather is more appropriate for cycling and more people would be out cycling, then it drops off steeply after October when it becomes darker and colder.

It appears that Medium doesn’t support embedding a Shiny application so I have included an image of what the finished product looks like and then if you want to see it in action simply head over to where I have it embedded on my website at www.bike2bytes.com.

Summary

This post serves as an example of how you can take a visualization from R and make it interactive using Shiny. This might not be necessary for all visualizations but in this case it helped solve a problem I encountered of having a large amount of data to visualize at one time.

I hope this might also help others when trying to learn how to build a Shiny application like all the many examples that did the same for me.

--

--

David Morison

Web developer interested in all things science, data and technology. Masters degree in atmospheric science. Endurance sports fanatic.