SlideShare a Scribd company logo
2
Most read
3
Most read
8
Most read
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

PPTX
CRIM-INVEST-PROTOCOLS.pptx
PDF
Planimetria de campo y de gabinete en caso de deslizamiento de terreno
PPTX
PDF
Comparative police systems_preview
PPTX
Remote sensing: Accuracy Assesment
PPTX
Application of Photography to Law Enforcement
PDF
Spatial Data Science with R
PPTX
FORENSIC 2 (Fingerprint).pptx
CRIM-INVEST-PROTOCOLS.pptx
Planimetria de campo y de gabinete en caso de deslizamiento de terreno
Comparative police systems_preview
Remote sensing: Accuracy Assesment
Application of Photography to Law Enforcement
Spatial Data Science with R
FORENSIC 2 (Fingerprint).pptx

What's hot (8)

PDF
Geographic query and analysis
PDF
Generating the Trends and Forecast of Crime Rates in Ozamiz City, Philippines
PPT
PPTX
Metodología en criminalistica
PPTX
CDI-2-ppt (1).pptx
PPT
Application of GIS in Criminology and Defence Intelligence
PPTX
Blood spatter interpretation-FORENSIC SEROLOGY
PDF
УРОК 3. ЖУРНАЛІСТСЬКІ РОЗСЛІДУВАННЯ: ОСНОВИ
Geographic query and analysis
Generating the Trends and Forecast of Crime Rates in Ozamiz City, Philippines
Metodología en criminalistica
CDI-2-ppt (1).pptx
Application of GIS in Criminology and Defence Intelligence
Blood spatter interpretation-FORENSIC SEROLOGY
УРОК 3. ЖУРНАЛІСТСЬКІ РОЗСЛІДУВАННЯ: ОСНОВИ
Ad

Similar to Geo Spatial Plot using R (20)

PDF
Data visualization using the grammar of graphics
DOCX
Data visualization with R and ggplot2.docx
ODP
Создание картограмм на принципах грамматики графики. С помощью R-расширения g...
PDF
data-visualization.pdf
PDF
Data visualization-2.1
PDF
VISIALIZACION DE DATA.pdf
PDF
Opensource gis development - part 4
PDF
Python grass
PDF
Data Visualization with ggplot2.pdf
DOCX
R-ggplot2 package Examples
DOCX
PDF
Edit/correct India Map In Cdat Documentation - With Edited World Map Data
PDF
Mashup caravan android-talks
PDF
r for data science 2. grammar of graphics (ggplot2) clean -ref
PPTX
Tech talk ggplot2
DOC
Computer graphics
DOC
Computer graphics
PDF
2Bytesprog2 course_2014_c9_graph
DOCX
Data Visualization with R.ggplot2 and its extensions examples.
DOCX
Company_X_Data_Analyst_Challenge
Data visualization using the grammar of graphics
Data visualization with R and ggplot2.docx
Создание картограмм на принципах грамматики графики. С помощью R-расширения g...
data-visualization.pdf
Data visualization-2.1
VISIALIZACION DE DATA.pdf
Opensource gis development - part 4
Python grass
Data Visualization with ggplot2.pdf
R-ggplot2 package Examples
Edit/correct India Map In Cdat Documentation - With Edited World Map Data
Mashup caravan android-talks
r for data science 2. grammar of graphics (ggplot2) clean -ref
Tech talk ggplot2
Computer graphics
Computer graphics
2Bytesprog2 course_2014_c9_graph
Data Visualization with R.ggplot2 and its extensions examples.
Company_X_Data_Analyst_Challenge
Ad

More from Rupak Roy (20)

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

Recently uploaded (20)

PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PPTX
Cloud computing and distributed systems.
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Sensors and Actuators in IoT Systems using pdf
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Advanced Soft Computing BINUS July 2025.pdf
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PPTX
breach-and-attack-simulation-cybersecurity-india-chennai-defenderrabbit-2025....
PPTX
Big Data Technologies - Introduction.pptx
PDF
GDG Cloud Iasi [PUBLIC] Florian Blaga - Unveiling the Evolution of Cybersecur...
PDF
KodekX | Application Modernization Development
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
Reach Out and Touch Someone: Haptics and Empathic Computing
Cloud computing and distributed systems.
Review of recent advances in non-invasive hemoglobin estimation
Sensors and Actuators in IoT Systems using pdf
Diabetes mellitus diagnosis method based random forest with bat algorithm
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Advanced Soft Computing BINUS July 2025.pdf
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
The Rise and Fall of 3GPP – Time for a Sabbatical?
breach-and-attack-simulation-cybersecurity-india-chennai-defenderrabbit-2025....
Big Data Technologies - Introduction.pptx
GDG Cloud Iasi [PUBLIC] Florian Blaga - Unveiling the Evolution of Cybersecur...
KodekX | Application Modernization Development
Dropbox Q2 2025 Financial Results & Investor Presentation
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Chapter 3 Spatial Domain Image Processing.pdf
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf

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