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

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 -refMin-hyung Kim
 
Assignment3 solution 3rd_edition
Assignment3 solution 3rd_editionAssignment3 solution 3rd_edition
Assignment3 solution 3rd_editionMysore
 
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 ApplicationSwati Jadhav
 
Javascript Array map method
Javascript Array map methodJavascript Array map method
Javascript Array map methodtanerochris
 
Visualising Big Data
Visualising Big DataVisualising Big Data
Visualising Big DataAmit Kapoor
 
The Web map stack on Django
The Web map stack on DjangoThe Web map stack on Django
The Web map stack on DjangoPaul Smith
 
ggplot2 extensions-ggtree.
ggplot2 extensions-ggtree.ggplot2 extensions-ggtree.
ggplot2 extensions-ggtree.Dr. Volkan OBAN
 
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.Dr. Volkan OBAN
 
Geolocation on Rails
Geolocation on RailsGeolocation on Rails
Geolocation on Railsnebirhos
 
R scatter plots
R scatter plotsR scatter plots
R scatter plotsAbhik Seal
 
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...Dr. Volkan OBAN
 
Surface3d in R and rgl package.
Surface3d in R and rgl package.Surface3d in R and rgl package.
Surface3d in R and rgl package.Dr. Volkan OBAN
 
Mi primer map reduce
Mi primer map reduceMi primer map reduce
Mi primer map reduceRuben Orta
 
Mi primer map reduce
Mi primer map reduceMi primer map reduce
Mi primer map reducebetabeers
 

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

Data visualization-2.1
Data visualization-2.1Data visualization-2.1
Data visualization-2.1RenukaRajmohan
 
Opensource gis development - part 4
Opensource gis development - part 4Opensource gis development - part 4
Opensource gis development - part 4Andrea Antonello
 
Data Visualization with ggplot2.pdf
Data Visualization with ggplot2.pdfData Visualization with ggplot2.pdf
Data Visualization with ggplot2.pdfCarlosTrujillo199971
 
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-talkshonjo2
 
Tech talk ggplot2
Tech talk   ggplot2Tech talk   ggplot2
Tech talk ggplot2jalle6
 
2Bytesprog2 course_2014_c9_graph
2Bytesprog2 course_2014_c9_graph2Bytesprog2 course_2014_c9_graph
2Bytesprog2 course_2014_c9_graphkinan keshkeh
 
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.Dr. Volkan OBAN
 
Company_X_Data_Analyst_Challenge
Company_X_Data_Analyst_ChallengeCompany_X_Data_Analyst_Challenge
Company_X_Data_Analyst_ChallengeMark Yashar
 
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 GraphicsClaus Wilke
 
Spatial visualization with ggplot2
Spatial visualization with ggplot2Spatial visualization with ggplot2
Spatial visualization with ggplot2Joaquim Silva
 
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 #rstatsAjay Ohri
 
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.pptxssuser255bf1
 

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

Hierarchical Clustering - Text Mining/NLP
Hierarchical Clustering - Text Mining/NLPHierarchical Clustering - Text Mining/NLP
Hierarchical Clustering - Text Mining/NLPRupak Roy
 
Clustering K means and Hierarchical - NLP
Clustering K means and Hierarchical - NLPClustering K means and Hierarchical - NLP
Clustering K means and Hierarchical - NLPRupak Roy
 
Network Analysis - NLP
Network Analysis  - NLPNetwork Analysis  - NLP
Network Analysis - NLPRupak Roy
 
Topic Modeling - NLP
Topic Modeling - NLPTopic Modeling - NLP
Topic Modeling - NLPRupak Roy
 
Sentiment Analysis Practical Steps
Sentiment Analysis Practical StepsSentiment Analysis Practical Steps
Sentiment Analysis Practical StepsRupak Roy
 
NLP - Sentiment Analysis
NLP - Sentiment AnalysisNLP - Sentiment Analysis
NLP - Sentiment AnalysisRupak Roy
 
Text Mining using Regular Expressions
Text Mining using Regular ExpressionsText Mining using Regular Expressions
Text Mining using Regular ExpressionsRupak Roy
 
Introduction to Text Mining
Introduction to Text Mining Introduction to Text Mining
Introduction to Text Mining Rupak Roy
 
Apache Hbase Architecture
Apache Hbase ArchitectureApache Hbase Architecture
Apache Hbase ArchitectureRupak Roy
 
Introduction to Hbase
Introduction to Hbase Introduction to Hbase
Introduction to Hbase Rupak Roy
 
Apache Hive Table Partition and HQL
Apache Hive Table Partition and HQLApache Hive Table Partition and HQL
Apache Hive Table Partition and HQLRupak Roy
 
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 Rupak Roy
 
Introductive to Hive
Introductive to Hive Introductive to Hive
Introductive to Hive Rupak Roy
 
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 RDBMSRupak Roy
 
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 Rupak Roy
 
Introduction to scoop and its functions
Introduction to scoop and its functionsIntroduction to scoop and its functions
Introduction to scoop and its functionsRupak Roy
 
Introduction to Flume
Introduction to FlumeIntroduction to Flume
Introduction to FlumeRupak Roy
 
Apache Pig Relational Operators - II
Apache Pig Relational Operators - II Apache Pig Relational Operators - II
Apache Pig Relational Operators - II Rupak Roy
 
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 LineRupak Roy
 
Apache PIG Relational Operations
Apache PIG Relational Operations Apache PIG Relational Operations
Apache PIG Relational Operations 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

Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Neo4j
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptxLBM Solutions
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraDeakin University
 
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxMaking_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxnull - The Open Security Community
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 

Recently uploaded (20)

Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptx
 
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptxVulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning era
 
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxMaking_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 

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