Thanks to David Kahle and Hadley Wickham who ramped up that example and subsequently developed the {ggmap} package, making maps in R can be really intuitive and fun!
I wrote a wrapper function that takes a location within England and Wales, downloads crime data around that location over a certain period of time and creates crime weather plots. This blog post discusses the data used, methodology and the wrapper function with some worked examples. The codes are available here.
(Nov-2013 Update: I have updated the codes and created a web app using Shiny and ShinyApps. For more information, please read this new blog post.)
Data
The street-level crime data is one of the 9,000 datasets available from data.gov.uk. The data can be downloaded systematically via the Police API. The latest version of the API no longer requires authentication.
The following URL can be used to obtain crime records at street-level within a one-mile radius of a single point. The parameters required are latitude, longitude and month. The downloaded data is in JSON format which can be converted into R's data format using the {RJSONIO} package.
Example URL: http://data.police.uk/api/crimes-street/all-crime?lat=52.629729&lng=-1.131592&date=2012-04
The following URL can be used to obtain crime records at street-level within a one-mile radius of a single point. The parameters required are latitude, longitude and month. The downloaded data is in JSON format which can be converted into R's data format using the {RJSONIO} package.
Example URL: http://data.police.uk/api/crimes-street/all-crime?lat=52.629729&lng=-1.131592&date=2012-04
Methodology
The methodology can be summarised in the following six steps:
1. Obtain latitude and longitude of a user-defined location using ggmap::geocode.
2. Download crime data via the Police API as discussed above.
3. Convert JSON into a list and then a data frame.
4. Download a base map from Google using ggmap::get_googlemap.
5. Covert the base map into a ggplot object using ggmap::ggmap.
6. Add multiple layers on top of the base map using the data frame like a normal ggplot.
For more details, check out the functions in the codes:
1. Obtain latitude and longitude of a user-defined location using ggmap::geocode.
2. Download crime data via the Police API as discussed above.
3. Convert JSON into a list and then a data frame.
4. Download a base map from Google using ggmap::get_googlemap.
5. Covert the base map into a ggplot object using ggmap::ggmap.
6. Add multiple layers on top of the base map using the data frame like a normal ggplot.
For more details, check out the functions in the codes:
- "get.data" and "list2df" for steps 1, 2 and 3
- "visualise.data" for steps 4, 5 and 6
Wrapper and Worked Examples
The wrapper function looks like this ...
... given the location, time period and a few more graphical settings, the wrapper can produce a crime weather map. The following worked examples illustrate the usage.
crimeplot.wrapper <- function( point.of.interest = "London Eye", ## user-defined location period = c("2013-01","2013-02"), ## period of time in YYYY-MM type.map = "roadmap", ## roadmap, terrain, satellite or hybrid type.facet = NA, ## options: NA, month, category or type type.print = NA, ## options: NA, panel or window output.plot = TRUE, ## print it to a png file? output.filename = "temp.png", ## provide a filename output.size = c(700,700)) ## width and height setting
Example 1 - All crimes around London Eye from Jan-2013 to Apr-2013
Comments:
Here we can see a huge crime hotspt in the Soho district of London - an area full of bars, restaurants, theatres and nightclubs (did I mention Chinatown?)
Codes:
## Define the period
ex1.period <- format(seq(as.Date("2013-01-01"),length=4,by="months"),"%Y-%m")
## Use the wrapper ex1.plot <- crimeplot.wrapper(point.of.interest = "London Eye", period = ex1.period, type.map = "roadmap", output.filename = "ex1.png", output.size = c(700,700))
Example 2 - Typical crimes and traffic incidents around London Eye from Jan-2013 to Apr-2013
(Note: click on the image to see original image in higher resolution)
Comments:
Now we seperate the data from British Transport Police (BTP) and all other forces (Force) using the facet function in {ggplot}. We can see a traffic black spot on the other side of River Thames.
Codes:
## Define the period ex2.period <- format(seq(as.Date("2013-01-01"),length=4,by="months"),"%Y-%m") ## Use the wrapper ex2.plot <- crimeplot.wrapper(point.of.interest = "London Eye", period = ex2.period, type.map = "roadmap", type.facet = "type", output.filename = "ex2.png", output.size = c(1400,700))
Example 3 - Monthly crimes in Manchester for the year 2012 on a satellite map
(Note: click on the image to see original image in higher resolution)
Comments:
Using the facet function on "month", we can look at the changes in patterns over time. Looks like there is not much seasonality in Manchester as the crime hotspots remain hot over the year.
Codes:
## Define the period ex3.period <- format(seq(as.Date("2012-01-01"),length=12,by="months"),"%Y-%m") ## Use the wrapper ex3.plot <- crimeplot.wrapper(point.of.interest = "Manchester", period = ex3.period, type.map = "satellite", type.facet = "month", output.filename = "ex3.png", output.size = c(1400,1400))
Example 4 - Crimes by categories in Liverpool from Jan-2013 to Apr-2013 on a hybrid map
(Note: click on the image to see original image in higher resolution)
Comments:
Now we separate different categories of crimes. It is interesting to see that only a small part of the city is affected by shoplifting and other theft while burglary, arson and vehicle crimes are very common problems in Liverpool.
Codes:
## Define the period ex4.period <- format(seq(as.Date("2013-01-01"),length=4,by="months"),"%Y-%m") ## Use the wrapper ex4.plot <- crimeplot.wrapper(point.of.interest = "Liverpool", period = ex4.period, type.map = "hybrid", type.facet = "category", output.filename = "ex4.png", output.size = c(1400,1400))
Further Work
Further work is needed to ...1. optimise the codes for "list2df" transformation (At the moment it is quite slow. I tried lapply but it didn't give me back the desired data frame format but I know there must be a solution.)
2. better automate the graphical settings for output resolution, font size etc.
3. make it interactive using {Shiny}
(Nov-2013 Update: I have improved (1) using plyr::ldply and done (2 & 3). See this.)
Acknowledgement
I would like to thank Yanchang Zhao for his excellent book titled "R and Data Mining: Examples and Case Studies" which encouraged me to shift from MATLAB to R. All embedded codes were Created by Pretty R at inside-R.org.
Thanks for sharing this article...
ReplyDeleteLeanpitch provides online training in Advanced Scrum Master during this lockdown period everyone can use it wisely.
Advanced Scrum Master Training Online
Thanks for sharing this informative content , Great work
ReplyDeleteTo crack scrum master interview : Scrum Master Interview Questions
Thanks for sharing this informative content , Great work
ReplyDeleteLeanpitch provides online training in Scrum Master during this lockdown period everyone can use it wisely.
CSM online
Thanks for sharing this informative content , Great work
ReplyDeleteLeanpitch provides online training in Advanced Scrum Master during this lockdown period everyone can use it wisely.
Advanced Scrum Master Training
Thanks for sharing this informative content , Great work
ReplyDeleteLeanpitch provides online training in Scrum Master during this lockdown period everyone can use it wisely.
CSM training online
Thanks for sharing this informative content , Great work
ReplyDeleteTo crack scrum master interview : Scrum Master Interview Questions
Thanks for sharing this informative content , Great work
ReplyDeleteLeanpitch provides online training in Scrum Master during this lockdown period everyone can use it wisely.
CSM online certification
Thanks for sharing this informative content , Great work
ReplyDeleteLeanpitch provides online training in Scrum Master during this lockdown period everyone can use it wisely.
CSM training online
Thanks for sharing this informative content , Great work
ReplyDeleteLeanpitch provides online training in Advanced Scrum Master during this lockdown period everyone can use it wisely.
Advanced CSM training online
Thanks for sharing this informative content , Great work
ReplyDeleteLeanpitch provides online training in Coaching agile transitions during this lockdown period everyone can use it wisely.
Coaching agile transitions
Thanks for sharing this informative content , Great work
ReplyDeleteLeanpitch provides online training in Devops during this lockdown period everyone can use it wisely.
Devops Online Training
Thanks for sharing this informative content , Great work
ReplyDeleteLeanpitch provides online training in Product prototyping during this lockdown period everyone can use it wisely.
icp-cat training
Thanks for sharing this informative content , Great work
ReplyDeleteRead this PSM vs CSM blog from Leanpitch to get a better conclusion : PSM vs CSM
Thanks for sharing this informative content , Great work
ReplyDeleteLeanpitch provides crash course in Conducting User Interview during this lockdown period everyone can use it wisely
Conducting user interview
Thanks for sharing this informative content , Great work
ReplyDeleteLeanpitch provides online training in Devops during this lockdown period everyone can use it wisely.
Devops Online Training
SEO is the best process for ranking a webpage on Search Engine. We are the best SEO Service company in Dhaka, Bangladesh. Are you leading a small business and looking for SEO Services?
ReplyDeleteWe are the SEO Expert. We use natural SEO Strategy to rank your website on the first page and the first position of every Search Engine. Doing Local SEO, Website Analysis and Audit, On Page SEO,
Off Page SEO, Competitor Analysis, PBN Backlink, Guest Posting, Wiki Backlink, Content Marketing, Create a backlink with long content and Website Speed increasing like as your best SEO consultant Call us Now whatsapp: +(88) 01537587949
SEO Service
Free bangla sex video:careful
good post outsourcing institute in bangladesh
Thanks for sharing this informative content , Great work
ReplyDeleteRead this Blog to become a perfect Product Manager : Best Books for Product managers
Thanks for sharing this informative content , Great work
ReplyDeleteRead this Blog to know about Scrum artifacts : Scrum artifacts
Thanks for sharing this informative content , Great work
ReplyDeleteDevops Online Training
Leanpitch provides online training in Devops during this lockdown period everyone can use it wisely.
Thanks for sharing this informative content , Great work
ReplyDeleteTo crack Agile coach interview : Agile coach interview questions
Thanks for sharing this informative content , Great work
ReplyDeleteLeanpitch provides online training in Enterprise Agile coaching during this lockdown period everyone can use it wisely.
Enterprise Agile Coaching
Thanks for sharing this informative content , Great work
ReplyDeleteLeanpitch provides online certification in scrum master during this lockdown period everyone can use it wisely.
Scrum master certification
Thanks for sharing this informative content , Great work
ReplyDeleteLeanpitch provides online certification in Enterprise agile coaching during this lockdown period everyone can use it wisely.
Enterprise Agile Coaching
Thanks for sharing this informative content , Great work
ReplyDeleteLeanpitch provides online training in Product management during this lockdown period everyone can use it wisely.
Product management Certification
Thanks for sharing this.,
ReplyDeleteLeanpitch provides online training in Scrum Master during this lockdown period everyone can use it wisely.
Join Leanpitch 2 Days CSM Certification Workshop in different cities.
certified scrum master certification cost
Good. I am really impressed with your writing talents and also with the layout on your weblog. Appreciate, Is this a paid subject matter or did you customize it yourself? Either way keep up the nice quality writing, it is rare to peer a nice weblog like this one nowadays. Thank you, check also virtual edge and speaker thank you notes
ReplyDelete
ReplyDeleteThanks for sharing this.,
Leanpitch provides crash course in Brain hacks using NLP everyone can use it wisely.
Brain hacks with NLP
NLP training
Thanks for sharing this.,
ReplyDeleteLeanpitch provides crash course in Brain hacks using NLP everyone can use it wisely.
NLP training
NLP crash course
ReplyDeleteThanks for sharing this.,
Leanpitch provides crash course in Facilitating change everyone can use it wisely.
Reducing barrier for change
Facilitating change
Thanks for sharing this.,
ReplyDeleteLeanpitch provides crash course in Facilitating change everyone can use it wisely.
Facilitating change
Facilitating change in the workplace
Your site is truly cool and this is an extraordinary moving article and If it's not too much trouble share more like that. Thank You..
ReplyDeleteDigital Marketing Course in Hyderabad
ReplyDeleteThanks for sharing this informative content.,
Turient is an All-in-one platform for all our teaching needs. If Teaching is your passion ,enabling is ours
Read the Informative blog - 11 Free Teaching Tools for Online Teachers
11 Free Teaching Tools for Online Teachers
Free Teaching Tools
Thanks for sharing this informative content.,
ReplyDeleteLeanpitch provides online training in Agile team facilitation during this lockdown period everyone can use it wisely.
Agile team facilitation
ICP ATF
Thanks for sharing this informative content.,
ReplyDeleteLeanpitch provides online training in Agile team facilitation during this lockdown period everyone can use it wisely.
Agile team facilitation
ICAGILE ATF
ReplyDeleteThanks for sharing this informative content.,
Leanpitch provides online training in Agile team facilitation during this lockdown period everyone can use it wisely.
ICP ATF
Agile facilitator
Thanks for sharing this informative content.,
ReplyDeleteLeanpitch provides online training in Agile team facilitation during this lockdown period everyone can use it wisely.
Agile facilitator
Team facilitator in Agile
ReplyDeleteThanks for sharing this.,
Leanpitch provides online training in Build mvp without code ,everyone can use it wisely.
Build mvp without code
no code mvp
Thank a lot. You have done excellent job. I enjoyed your blog . Nice efforts
ReplyDeleteData Science Certification in Hyderabad
Wow, happy to see this awesome post. I hope this think help any newbie for their awesome work and by the way thanks for share this awesomeness, i thought this was a pretty interesting read when it comes to this topic. Thank you..
ReplyDeleteArtificial Intelligence Course
I need to thank you for this very good read and i have bookmarked to check out new things from your post. Thank you very much for sharing such a useful article and will definitely saved and revisit your site.
ReplyDeleteData Science Course
I am sure it will help many people. Keep up the good work. It's very compelling and I enjoyed browsing the entire blog.
ReplyDeleteBusiness Analytics Course in Bangalore
Very good message. I stumbled across your blog and wanted to say that I really enjoyed reading your articles.
ReplyDeleteAI Courses in Bangalore
I am really enjoying reading your well written articles. I am looking forward to reading new articles. Keep up the good work.
ReplyDeleteData Science Courses in Bangalore
I feel very grateful that I read this. It is very helpful and very informative and I really learned a lot from it.
ReplyDeleteData Analytics Course
Outsourcing Training institute In Dhaka Said,
ReplyDeleteThanks for sharing this informative content , Great work.
Thanks for sharing this.,
ReplyDeleteLeanpitch provides online training in Scrum Master, everyone can use it wisely.
Join Leanpitch 2 Days CSM Certification Workshop in different cities.
Scrum master certification
csm certification
Thanks Your post is so cool and this is an extraordinary moving article and If it's not too much trouble share more like that.
ReplyDeleteDigital Marketing Course in Hyderabad
You have done excellent job Thanks a lot and I enjoyed your blog. Great Post.
ReplyDeleteData Science Certification in Hyderabad
Great post happy to see this. I thought this was a pretty interesting read when it comes to this topic Information. Thanks..
ReplyDeleteArtificial Intelligence Course
Nice Post thank you very much for sharing such a useful information and will definitely saved and revisit your site and i have bookmarked to check out new things frm your post.
ReplyDeleteData Science Course
Excellent effort to make this blog more wonderful and attractive. Information shared was useful thankyou.
ReplyDeleteData Science Institutes in Bangalore
Excellent effort to make this blog more wonderful and attractive.
ReplyDeleteBusiness Analytics Course in Bangalore
Excellent content ,Thanks for sharing this .,
ReplyDeleteHow to start a podcast
Virtual classroom setup
Excellent content ,Thanks for sharing this .,
ReplyDeleteMust have Skills for online teachers
Free teaching tools
Excellent content ,Thanks for sharing this .,
ReplyDeleteOnline teaching platform
Online teaching tool
Really nice and amazing post. I was looking for this kind of information, Keep posting. Thanks for sharing.
ReplyDeleteData Science Courses in Bangalore
Very informative blog! There is so much information here that can help thank you for sharing.
ReplyDeleteData Science Syllabus
Very useful article to read and Information was helpful.I would like to thank you for the efforts you had made for writing this awesome article.
ReplyDeleteData Analytics Course
This is an informative and knowledgeable article. therefore, I would like to thank you for your effort in writing this article.
ReplyDeleteData Science Course in Bangalore
ReplyDeleteI read your excellent blog post. It's a great job. I enjoyed reading your post for the first time, thank you.
Data Science Institutes in Bangalore