SlideShare a Scribd company logo
1 of 16
Download to read offline
Geospatial plots
Using get_maps(), ggmap(), ggplot2()
ggmap() makes it easy to retrieve raster map tiles from popular online
mapping services like Google Maps, Stamen Maps, Open Street Map and plot
the dataset on maps using the ggplot2 framework
Includes 3 easy Steps for geospatial plots:
 First get the map using get_map(“location/coordinates”,maptype=“ ”)->p
 Second, plot the map using ggmap(p)
 Finally use ggplot2() objects like P+ geom_point(), geom_density2d() to
plot the underlying dataset.
Let’s understand this with the help of an example:
Geospatial plots: ggmap()
#install and load the relevant packages
>library(lubridate) #to manipulate time in the dataset
>library(ggplot2) #to plot the underlying data
>library(ggmap) #to get and plot the map
>library(dplyr) #to filter the dataset
>library(ggrepel) #alternative to geom_text to label the points
#load the dataset
>crimes<-read.csv(“crimes.csv”,header=T,stringAsFactors=FALSE)
>dn<-read.csv(“dangerousNeighborhood.csv”,header=T,stringAsFactors=FALSE)
>View(crimes)
>attach(crimes)#so that we don’t have to use the reference like crime$col_name
>View(dn) >attach(dn)
Geospatial plots: ggmap()
View(crimes)
Geospatial plots: ggmap()
View(dn)
Geospatial plots: ggmap()
#we will extract the data of the year 2017 to 18 to analyze a manageable time
frame
#first format the column in date format using lubridate
>crimes$ymd <-mdy_hms(Event.Clearance.Date)
>crimes$year <- year(crimes$ymd)
#extract the years to filter 2017-18 data using dplyr()
>c2<-filter(crimes,year==2017 | year==2018)
dn$label <-paste(Rank, Location, sep="-")
Geospatial plots:ggmap()
 STEP 1:
Get the map using get_map() or get_googlemap()
>Seattle<-get_googlemap(center = c(lon = -122.335167, lat = 47.608013),
zoom = 11, scale = 2, maptype ='terrain')
> Seattle<-get_map(location = c(lon = -122.335167, lat = 47.608013),
zoom = 11, maptype ='terrain', source = "google" )
Where,
zoom= map zoom, an integer from 10(continent) to 21(building), default is 10
scale= number of pixels returned possible values are 1,24(e.g. sizec(640X640),
scale=2 returns an image with (1280x1280) pixels
source= Google Maps ("google"), OpenStreetMap ("osm"), Stamen Maps
("stamen"), or CloudMade maps ("cloudmade")
maytype= “terrain", "terrain-background", "satellite", "roadmap", and "hybrid"
(google maps), "terrain", "watercolor", and "toner" (stamen maps)
Geospatial plots: ggmap()
STEP 2:
Plot the map using ggmap()
>ggmap(Seattle)
>p<- ggmap(Seattle)
Step 3:
Using ggplot2() to plot the dataset
>p + geom_point(data=c2,aes(x= Longitude,
y=Latitude, colour = Initial.Type.Group),size = 3)
+theme(legend.position="bottom") #Where size= 3 are the size of data points
Geospatial plots:ggmap()
#In the last map, it looks a bit dense and dirty because all the data points of the incidents were
sitting on top of each other. Now what we will do we will filter out the top most dangerous crimes
else the important one according to the needs.
> c2important<-filter(c2, Event.Clearance.Group %in% c('TRESPASS', 'ASSAULTS', 'SUSPICIOUS
CIRCUMSTANCES', 'BURGLARY', 'PROWLER', 'ASSAULTS', 'PROPERTY DAMAGE', 'ARREST',
'NARCOTICS COMPLAINTS','THREATS', 'HARASSMENT', 'WEAPONS CALLS','PROSTITUTION' ,
'ROBBERY', 'FAILURE TO REGISTER (SEX OFFENDER)', 'LEWD CONDUCT', 'HOMICIDE'))
#we will redo the plot for only important crimes
with ‘alpha=0.4’ to make the points transparent
>p + geom_point(data=c2important,aes(x= Longitude,
y=Latitude, colour = Initial.Type.Group),alpha=0.4,
size = 3) +theme(legend.position="bottom")
Now we will add the 2nd dataset that have the list of
most dangerous neighborhood which in turn will help us
to understand the types of crimes for each neighborhood.
ggplot2::geom_point()
#we can do this by adding an another geom_point layer on the top existing plot to
plot the 2nd dataset values and to differentiate from the existing plot we will use
shapes (objects) for plotting the each value of the 2nd dataset. Hence we will use
the scale_shape_manual() function to plot more than the default 6 shapes
>dn$Location<-as.factor(dn$Location)
>p +geom_point(data=c2important,
aes(x= Longitude, y=Latitude,
colour = Initial.Type.Group),alpha=0.4,
size=3) +theme(legend.position="right")
+geom_point(data=dn,aes(x=long, y=lat,
shape=Location, stroke = 2),
colour= "black", size =3)
+scale_shape_manual(values=1:nlevels(dn$Location))
ggplot2:: scale_shape_manual()
Now in previous plot we can observe that there is hardly any space left for ‘Legends’.
So to free some space for our future ‘legends’ we will simply change the shape
based neighborhood identifiers to labels. Labeling is a bit difficulty when it comes in
using two different datasets within the same plot and we might face labels
overlapping or seating on top of each other. This means we have to use some other
function than geom_text. For this example we will use geom_label_repel()
>dn$label<-paste(Rank,Location,sep="-") #creating ranked labels in the dn datasets.
#converting the shape based neighborhood identifiers to labels
>p+geom_point(data=c2important,
aes(x= Longitude, y=Latitude,
colour = Initial.Type.Group),
alpha=0.4,size= 3)
+theme(legend.position="right")
+geom_point(data=dn,
aes(x =long, y =lat, stroke = 2),
colour= "black", size =3)
+geom_label_repel(aes(long,lat,
label = label), data=dn, size = 4,
box.padding = 0.2, point.padding = 0.3)
ggrepel::geom_label_repel()
#Alternatively we can also plot the density of the data for each events by using
stat_density2d() function and get the same results like geom_point() function.
>p +stat_density2d(data=c2important,aes(x=Longitude,y=Latitude,
fill= ..level..),alpha=0.4,size = 0.01,
bins = 30,geom = "polygon")
+geom_point(data=dn,aes(x=long,y =lat,
stroke = 2),colour= "red", size =3)
+geom_label_repel(aes(long, lat,
label = label),data=dn,size = 4,
box.padding = 0.2, point.padding = 0.3)
#now we will add a density line to highlight
the density estimates again by
using geom_density2d() function.
ggplot2::stat_density2d()
#adding density lines
>p +stat_density2d(data=c2important,aes(x=Longitude,y=Latitude,
fill= ..level..),alpha=0.4, size = 0.01, bins = 30, geom="polygon")
+geom_density2d(data = c2,aes(x = Longitude, y = Latitude), size = 0.3)
+geom_point(data=dn,
aes(x =long, y =lat, stroke = 2),
colour= "red", size =3)
+geom_label_repel(aes(long, lat,
label = label), data=dn,size = 4,
box.padding = 0.2, point.padding = 0.3)
ggplot2:: geom_point()
#another way to highlight the most occurred crime types is by using facet_wrap() function
#first filter the data with the most occurred crime types
>table(crimes$Event.Clearance.Group)
>c2sub <-filter(c2, Event.Clearance.Group %in% c('TRAFFIC RELATED CALLS',
'DISTURBANCES', 'SUSPICIOUS CIRCUMSTANCES', 'MOTOR VEHICLE COLLISION
INVESTIGATION'))
#applying facet_wrap()
>p +stat_density2d(data=c2sub,
aes(x= Longitude, y=Latitude, fill= ..level..),
alpha=0.4, size = 0.2, bins = 30, geom = "polygon")
+geom_density2d(data = c2sub,
aes(x = Longitude, y = Latitude), size = 0.3)
+facet_wrap(~ Event.Clearance.Group)
ggplot2:: facet_wrap()
#Finally polishing the plot by adding the small details.
>p +stat_density2d(data=c2sub,aes(x= Longitude,y=Latitude,fill= ..level..),
alpha=0.4, size = 0.2, bins = 30, geom= "polygon")+geom_density2d(data=
c2sub,aes(x = Longitude, y = Latitude),
size = 0.3) +geom_point(data=dn,
aes(x =long, y =lat, shape=Location,
stroke = 2),colour= “red", size =2,
alpha=0.5)
+scale_shape_manual(values=1:nlevels(
dn$Location))
+facet_wrap(~ Event.Clearance.Group)
ggplot2:: facet_wrap()
Next: Predict the unlimited benefit using machine
learning.
Thank you

More Related Content

What's hot

Assignment3 solution 3rd_edition
Assignment3 solution 3rd_editionAssignment3 solution 3rd_edition
Assignment3 solution 3rd_edition
Mysore
 
Geohex at Off4g2009
Geohex at Off4g2009Geohex at Off4g2009
Geohex at Off4g2009
Tadayasu Sasada
 
Geolocation on Rails
Geolocation on RailsGeolocation on Rails
Geolocation on Rails
nebirhos
 
R
RR
R
exsuns
 

What's hot (20)

r for data science 2. grammar of graphics (ggplot2) clean -ref
r for data science 2. grammar of graphics (ggplot2)  clean -refr for data science 2. grammar of graphics (ggplot2)  clean -ref
r for data science 2. grammar of graphics (ggplot2) clean -ref
 
Assignment3 solution 3rd_edition
Assignment3 solution 3rd_editionAssignment3 solution 3rd_edition
Assignment3 solution 3rd_edition
 
Integration of Google-map in Rails Application
Integration of Google-map in Rails ApplicationIntegration of Google-map in Rails Application
Integration of Google-map in Rails Application
 
Javascript Array map method
Javascript Array map methodJavascript Array map method
Javascript Array map method
 
Visualising Big Data
Visualising Big DataVisualising Big Data
Visualising Big Data
 
Geohex at Off4g2009
Geohex at Off4g2009Geohex at Off4g2009
Geohex at Off4g2009
 
CLUSTERGRAM
CLUSTERGRAMCLUSTERGRAM
CLUSTERGRAM
 
The Web map stack on Django
The Web map stack on DjangoThe Web map stack on Django
The Web map stack on Django
 
ggplot2 extensions-ggtree.
ggplot2 extensions-ggtree.ggplot2 extensions-ggtree.
ggplot2 extensions-ggtree.
 
Advanced Data Visualization in R- Somes Examples.
Advanced Data Visualization in R- Somes Examples.Advanced Data Visualization in R- Somes Examples.
Advanced Data Visualization in R- Somes Examples.
 
Basic Calculus in R.
Basic Calculus in R. Basic Calculus in R.
Basic Calculus in R.
 
Geolocation on Rails
Geolocation on RailsGeolocation on Rails
Geolocation on Rails
 
R
RR
R
 
R scatter plots
R scatter plotsR scatter plots
R scatter plots
 
CLIM Undergraduate Workshop: Tutorial on R Software - Huang Huang, Oct 23, 2017
CLIM Undergraduate Workshop: Tutorial on R Software - Huang Huang, Oct 23, 2017CLIM Undergraduate Workshop: Tutorial on R Software - Huang Huang, Oct 23, 2017
CLIM Undergraduate Workshop: Tutorial on R Software - Huang Huang, Oct 23, 2017
 
Some R Examples[R table and Graphics] -Advanced Data Visualization in R (Some...
Some R Examples[R table and Graphics] -Advanced Data Visualization in R (Some...Some R Examples[R table and Graphics] -Advanced Data Visualization in R (Some...
Some R Examples[R table and Graphics] -Advanced Data Visualization in R (Some...
 
Surface3d in R and rgl package.
Surface3d in R and rgl package.Surface3d in R and rgl package.
Surface3d in R and rgl package.
 
Googlevis examples
Googlevis examplesGooglevis examples
Googlevis examples
 
Mi primer map reduce
Mi primer map reduceMi primer map reduce
Mi primer map reduce
 
Mi primer map reduce
Mi primer map reduceMi primer map reduce
Mi primer map reduce
 

Similar to Geo Spatial Plot using R

Edit/correct India Map In Cdat Documentation - With Edited World Map Data
Edit/correct India Map In Cdat  Documentation -  With Edited World Map Data Edit/correct India Map In Cdat  Documentation -  With Edited World Map Data
Edit/correct India Map In Cdat Documentation - With Edited World Map Data
Arulalan T
 
Mashup caravan android-talks
Mashup caravan android-talksMashup caravan android-talks
Mashup caravan android-talks
honjo2
 
Company_X_Data_Analyst_Challenge
Company_X_Data_Analyst_ChallengeCompany_X_Data_Analyst_Challenge
Company_X_Data_Analyst_Challenge
Mark Yashar
 
sexy maps comes to R - ggplot+ google maps= ggmap #rstats
sexy maps comes to R - ggplot+ google maps= ggmap #rstatssexy maps comes to R - ggplot+ google maps= ggmap #rstats
sexy maps comes to R - ggplot+ google maps= ggmap #rstats
Ajay Ohri
 

Similar to Geo Spatial Plot using R (20)

data-visualization.pdf
data-visualization.pdfdata-visualization.pdf
data-visualization.pdf
 
Data visualization-2.1
Data visualization-2.1Data visualization-2.1
Data visualization-2.1
 
VISIALIZACION DE DATA.pdf
VISIALIZACION DE DATA.pdfVISIALIZACION DE DATA.pdf
VISIALIZACION DE DATA.pdf
 
Opensource gis development - part 4
Opensource gis development - part 4Opensource gis development - part 4
Opensource gis development - part 4
 
Python grass
Python grassPython grass
Python grass
 
Data Visualization with ggplot2.pdf
Data Visualization with ggplot2.pdfData Visualization with ggplot2.pdf
Data Visualization with ggplot2.pdf
 
No3
No3No3
No3
 
Edit/correct India Map In Cdat Documentation - With Edited World Map Data
Edit/correct India Map In Cdat  Documentation -  With Edited World Map Data Edit/correct India Map In Cdat  Documentation -  With Edited World Map Data
Edit/correct India Map In Cdat Documentation - With Edited World Map Data
 
Mashup caravan android-talks
Mashup caravan android-talksMashup caravan android-talks
Mashup caravan android-talks
 
Tech talk ggplot2
Tech talk   ggplot2Tech talk   ggplot2
Tech talk ggplot2
 
Computer graphics
Computer graphics   Computer graphics
Computer graphics
 
Computer graphics
Computer graphics   Computer graphics
Computer graphics
 
2Bytesprog2 course_2014_c9_graph
2Bytesprog2 course_2014_c9_graph2Bytesprog2 course_2014_c9_graph
2Bytesprog2 course_2014_c9_graph
 
Data Visualization with R.ggplot2 and its extensions examples.
Data Visualization with R.ggplot2 and its extensions examples.Data Visualization with R.ggplot2 and its extensions examples.
Data Visualization with R.ggplot2 and its extensions examples.
 
Company_X_Data_Analyst_Challenge
Company_X_Data_Analyst_ChallengeCompany_X_Data_Analyst_Challenge
Company_X_Data_Analyst_Challenge
 
ggplot2: An Extensible Platform for Publication-quality Graphics
ggplot2: An Extensible Platform for Publication-quality Graphicsggplot2: An Extensible Platform for Publication-quality Graphics
ggplot2: An Extensible Platform for Publication-quality Graphics
 
Spatial visualization with ggplot2
Spatial visualization with ggplot2Spatial visualization with ggplot2
Spatial visualization with ggplot2
 
sexy maps comes to R - ggplot+ google maps= ggmap #rstats
sexy maps comes to R - ggplot+ google maps= ggmap #rstatssexy maps comes to R - ggplot+ google maps= ggmap #rstats
sexy maps comes to R - ggplot+ google maps= ggmap #rstats
 
Pycon2011
Pycon2011Pycon2011
Pycon2011
 
4 CG_U1_M3_PPT_4 DDA.pptx
4 CG_U1_M3_PPT_4 DDA.pptx4 CG_U1_M3_PPT_4 DDA.pptx
4 CG_U1_M3_PPT_4 DDA.pptx
 

More from Rupak Roy

More from Rupak Roy (20)

Hierarchical Clustering - Text Mining/NLP
Hierarchical Clustering - Text Mining/NLPHierarchical Clustering - Text Mining/NLP
Hierarchical Clustering - Text Mining/NLP
 
Clustering K means and Hierarchical - NLP
Clustering K means and Hierarchical - NLPClustering K means and Hierarchical - NLP
Clustering K means and Hierarchical - NLP
 
Network Analysis - NLP
Network Analysis  - NLPNetwork Analysis  - NLP
Network Analysis - NLP
 
Topic Modeling - NLP
Topic Modeling - NLPTopic Modeling - NLP
Topic Modeling - NLP
 
Sentiment Analysis Practical Steps
Sentiment Analysis Practical StepsSentiment Analysis Practical Steps
Sentiment Analysis Practical Steps
 
NLP - Sentiment Analysis
NLP - Sentiment AnalysisNLP - Sentiment Analysis
NLP - Sentiment Analysis
 
Text Mining using Regular Expressions
Text Mining using Regular ExpressionsText Mining using Regular Expressions
Text Mining using Regular Expressions
 
Introduction to Text Mining
Introduction to Text Mining Introduction to Text Mining
Introduction to Text Mining
 
Apache Hbase Architecture
Apache Hbase ArchitectureApache Hbase Architecture
Apache Hbase Architecture
 
Introduction to Hbase
Introduction to Hbase Introduction to Hbase
Introduction to Hbase
 
Apache Hive Table Partition and HQL
Apache Hive Table Partition and HQLApache Hive Table Partition and HQL
Apache Hive Table Partition and HQL
 
Installing Apache Hive, internal and external table, import-export
Installing Apache Hive, internal and external table, import-export Installing Apache Hive, internal and external table, import-export
Installing Apache Hive, internal and external table, import-export
 
Introductive to Hive
Introductive to Hive Introductive to Hive
Introductive to Hive
 
Scoop Job, import and export to RDBMS
Scoop Job, import and export to RDBMSScoop Job, import and export to RDBMS
Scoop Job, import and export to RDBMS
 
Apache Scoop - Import with Append mode and Last Modified mode
Apache Scoop - Import with Append mode and Last Modified mode Apache Scoop - Import with Append mode and Last Modified mode
Apache Scoop - Import with Append mode and Last Modified mode
 
Introduction to scoop and its functions
Introduction to scoop and its functionsIntroduction to scoop and its functions
Introduction to scoop and its functions
 
Introduction to Flume
Introduction to FlumeIntroduction to Flume
Introduction to Flume
 
Apache Pig Relational Operators - II
Apache Pig Relational Operators - II Apache Pig Relational Operators - II
Apache Pig Relational Operators - II
 
Passing Parameters using File and Command Line
Passing Parameters using File and Command LinePassing Parameters using File and Command Line
Passing Parameters using File and Command Line
 
Apache PIG Relational Operations
Apache PIG Relational Operations Apache PIG Relational Operations
Apache PIG Relational Operations
 

Recently uploaded

Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
 

Recently uploaded (20)

Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of Brazil
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 

Geo Spatial Plot using R

  • 2. ggmap() makes it easy to retrieve raster map tiles from popular online mapping services like Google Maps, Stamen Maps, Open Street Map and plot the dataset on maps using the ggplot2 framework Includes 3 easy Steps for geospatial plots:  First get the map using get_map(“location/coordinates”,maptype=“ ”)->p  Second, plot the map using ggmap(p)  Finally use ggplot2() objects like P+ geom_point(), geom_density2d() to plot the underlying dataset. Let’s understand this with the help of an example: Geospatial plots: ggmap()
  • 3. #install and load the relevant packages >library(lubridate) #to manipulate time in the dataset >library(ggplot2) #to plot the underlying data >library(ggmap) #to get and plot the map >library(dplyr) #to filter the dataset >library(ggrepel) #alternative to geom_text to label the points #load the dataset >crimes<-read.csv(“crimes.csv”,header=T,stringAsFactors=FALSE) >dn<-read.csv(“dangerousNeighborhood.csv”,header=T,stringAsFactors=FALSE) >View(crimes) >attach(crimes)#so that we don’t have to use the reference like crime$col_name >View(dn) >attach(dn) Geospatial plots: ggmap()
  • 6. #we will extract the data of the year 2017 to 18 to analyze a manageable time frame #first format the column in date format using lubridate >crimes$ymd <-mdy_hms(Event.Clearance.Date) >crimes$year <- year(crimes$ymd) #extract the years to filter 2017-18 data using dplyr() >c2<-filter(crimes,year==2017 | year==2018) dn$label <-paste(Rank, Location, sep="-") Geospatial plots:ggmap()
  • 7.  STEP 1: Get the map using get_map() or get_googlemap() >Seattle<-get_googlemap(center = c(lon = -122.335167, lat = 47.608013), zoom = 11, scale = 2, maptype ='terrain') > Seattle<-get_map(location = c(lon = -122.335167, lat = 47.608013), zoom = 11, maptype ='terrain', source = "google" ) Where, zoom= map zoom, an integer from 10(continent) to 21(building), default is 10 scale= number of pixels returned possible values are 1,24(e.g. sizec(640X640), scale=2 returns an image with (1280x1280) pixels source= Google Maps ("google"), OpenStreetMap ("osm"), Stamen Maps ("stamen"), or CloudMade maps ("cloudmade") maytype= “terrain", "terrain-background", "satellite", "roadmap", and "hybrid" (google maps), "terrain", "watercolor", and "toner" (stamen maps) Geospatial plots: ggmap()
  • 8. STEP 2: Plot the map using ggmap() >ggmap(Seattle) >p<- ggmap(Seattle) Step 3: Using ggplot2() to plot the dataset >p + geom_point(data=c2,aes(x= Longitude, y=Latitude, colour = Initial.Type.Group),size = 3) +theme(legend.position="bottom") #Where size= 3 are the size of data points Geospatial plots:ggmap()
  • 9. #In the last map, it looks a bit dense and dirty because all the data points of the incidents were sitting on top of each other. Now what we will do we will filter out the top most dangerous crimes else the important one according to the needs. > c2important<-filter(c2, Event.Clearance.Group %in% c('TRESPASS', 'ASSAULTS', 'SUSPICIOUS CIRCUMSTANCES', 'BURGLARY', 'PROWLER', 'ASSAULTS', 'PROPERTY DAMAGE', 'ARREST', 'NARCOTICS COMPLAINTS','THREATS', 'HARASSMENT', 'WEAPONS CALLS','PROSTITUTION' , 'ROBBERY', 'FAILURE TO REGISTER (SEX OFFENDER)', 'LEWD CONDUCT', 'HOMICIDE')) #we will redo the plot for only important crimes with ‘alpha=0.4’ to make the points transparent >p + geom_point(data=c2important,aes(x= Longitude, y=Latitude, colour = Initial.Type.Group),alpha=0.4, size = 3) +theme(legend.position="bottom") Now we will add the 2nd dataset that have the list of most dangerous neighborhood which in turn will help us to understand the types of crimes for each neighborhood. ggplot2::geom_point()
  • 10. #we can do this by adding an another geom_point layer on the top existing plot to plot the 2nd dataset values and to differentiate from the existing plot we will use shapes (objects) for plotting the each value of the 2nd dataset. Hence we will use the scale_shape_manual() function to plot more than the default 6 shapes >dn$Location<-as.factor(dn$Location) >p +geom_point(data=c2important, aes(x= Longitude, y=Latitude, colour = Initial.Type.Group),alpha=0.4, size=3) +theme(legend.position="right") +geom_point(data=dn,aes(x=long, y=lat, shape=Location, stroke = 2), colour= "black", size =3) +scale_shape_manual(values=1:nlevels(dn$Location)) ggplot2:: scale_shape_manual()
  • 11. Now in previous plot we can observe that there is hardly any space left for ‘Legends’. So to free some space for our future ‘legends’ we will simply change the shape based neighborhood identifiers to labels. Labeling is a bit difficulty when it comes in using two different datasets within the same plot and we might face labels overlapping or seating on top of each other. This means we have to use some other function than geom_text. For this example we will use geom_label_repel() >dn$label<-paste(Rank,Location,sep="-") #creating ranked labels in the dn datasets. #converting the shape based neighborhood identifiers to labels >p+geom_point(data=c2important, aes(x= Longitude, y=Latitude, colour = Initial.Type.Group), alpha=0.4,size= 3) +theme(legend.position="right") +geom_point(data=dn, aes(x =long, y =lat, stroke = 2), colour= "black", size =3) +geom_label_repel(aes(long,lat, label = label), data=dn, size = 4, box.padding = 0.2, point.padding = 0.3) ggrepel::geom_label_repel()
  • 12. #Alternatively we can also plot the density of the data for each events by using stat_density2d() function and get the same results like geom_point() function. >p +stat_density2d(data=c2important,aes(x=Longitude,y=Latitude, fill= ..level..),alpha=0.4,size = 0.01, bins = 30,geom = "polygon") +geom_point(data=dn,aes(x=long,y =lat, stroke = 2),colour= "red", size =3) +geom_label_repel(aes(long, lat, label = label),data=dn,size = 4, box.padding = 0.2, point.padding = 0.3) #now we will add a density line to highlight the density estimates again by using geom_density2d() function. ggplot2::stat_density2d()
  • 13. #adding density lines >p +stat_density2d(data=c2important,aes(x=Longitude,y=Latitude, fill= ..level..),alpha=0.4, size = 0.01, bins = 30, geom="polygon") +geom_density2d(data = c2,aes(x = Longitude, y = Latitude), size = 0.3) +geom_point(data=dn, aes(x =long, y =lat, stroke = 2), colour= "red", size =3) +geom_label_repel(aes(long, lat, label = label), data=dn,size = 4, box.padding = 0.2, point.padding = 0.3) ggplot2:: geom_point()
  • 14. #another way to highlight the most occurred crime types is by using facet_wrap() function #first filter the data with the most occurred crime types >table(crimes$Event.Clearance.Group) >c2sub <-filter(c2, Event.Clearance.Group %in% c('TRAFFIC RELATED CALLS', 'DISTURBANCES', 'SUSPICIOUS CIRCUMSTANCES', 'MOTOR VEHICLE COLLISION INVESTIGATION')) #applying facet_wrap() >p +stat_density2d(data=c2sub, aes(x= Longitude, y=Latitude, fill= ..level..), alpha=0.4, size = 0.2, bins = 30, geom = "polygon") +geom_density2d(data = c2sub, aes(x = Longitude, y = Latitude), size = 0.3) +facet_wrap(~ Event.Clearance.Group) ggplot2:: facet_wrap()
  • 15. #Finally polishing the plot by adding the small details. >p +stat_density2d(data=c2sub,aes(x= Longitude,y=Latitude,fill= ..level..), alpha=0.4, size = 0.2, bins = 30, geom= "polygon")+geom_density2d(data= c2sub,aes(x = Longitude, y = Latitude), size = 0.3) +geom_point(data=dn, aes(x =long, y =lat, shape=Location, stroke = 2),colour= “red", size =2, alpha=0.5) +scale_shape_manual(values=1:nlevels( dn$Location)) +facet_wrap(~ Event.Clearance.Group) ggplot2:: facet_wrap()
  • 16. Next: Predict the unlimited benefit using machine learning. Thank you