Spatial Visualization Using R Part - 1

Contact at TJT@TechnicalJockey.com , if you are looking for an Instructor Based Online Training or Corporate Training in R !

Install "ggmap" package by using following code:

install.packages("ggmap")

Load "ggplot2" and "ggmap" packages to use.

library(ggplot2)

library(ggmap)

We can find geo-location code to see place in google map. We want to find geocode of white house. It shows longitude and latitude of location.

geocode("the white house")

We want to see google map of "white house" . We are using qmap() function to plot google map .

baylor <- "the white house"

qmap(baylor, zoom = 14)

 

Contact at TJT@TechnicalJockey.com , if you are looking for an Instructor Based Online Training or Corporate Training in R !

 

There are different types of map sources :

  • Google Maps

  • Open Street Maps

  • Stamen Maps

  • Cloudmade Maps

We can use source argument to draw different types of maps. We want to plot Stamen Maps by using source = "stamen". Stamen Maps has three available tile sets - terrain , watercolor and toner.

We are showing watercolor type Stamen map.

qmap(baylor, zoom = 14, source = "stamen", maptype = "watercolor")

We can show toner type map as:

qmap(baylor, zoom = 14, source = "stamen", maptype = "toner")

 

We can find distance between two locations by using mapdist() function .

from <- "houston"

to <- "waco, texas"

mapdist(from, to)

We can map our route from one location to other location .

route_df <- route(from, to, structure = 'route', mode = 'driving')

To plot google map , we specify location as "Houston" . We use geom_path() function to specify x and y variables to show in map . We specify colour as "grey" to show route of grey colour between "Houston" and "Waco ,Texas" . We use size of line draw in for route path . We specify data to use route_df data to show in map . We specify lineend argument as "round" to end path with round shape corners.

qmap('Houston', zoom = 7) +

 geom_path(

   aes(x = lon, y = lat),  colour = 'grey', size = 1.5,

   data = route_df, lineend = 'round')

 

We are working with crime dataset .We load crime dataset as :

data(crime)

We check the structure of crime dataset by using following code :

str(crime)

 

 

We check the names of all columns of crime dataset .

names(crime)

We check some observations of crime dataset as :

head(crime)

 

In order to add points to this plot , we need to store this image in an object .You first plot the map and then you can add layers of other geospatial objects and/or text. We do this with the code below:

HoustonMap<-qmap("houston", zoom = 14)

We check the data type of HoustonMap . It is a ggplot data type .

 

We assign the column name lon to the x variable and lat as y variable. We used data = crime to get these field from crime data . We used geom_point() function to show points in the map.    

HoustonMap +

geom_point(aes(x = lon, y = lat), data = crime)    

We use colour argument to show different types of offense in the map. The offense is the name of the column that contains the categorical variable that we want to use to distinguish between the color of our points.

HoustonMap+

 geom_point(aes(x = lon, y = lat, colour = offense),data = crime)

We add title to the map by using ggtitle() function .

HoustonMap+

 geom_point(aes(x = lon, y = lat, colour = offense),data = crime)+

 ggtitle("Map of Crime in Houston")

 

We can save this image as a file by wrapping our script with two commands .

png("myHoustonMap.png")  ##This is the name of the image file (in this case a .png file)

HoustonMap+

 geom_point(aes(x = lon, y = lat, colour = offense),data = crime)+

 ggtitle("Map of Crime in Houston")

We can close .png file by using following code :

dev.off()  

We can see we store the .png file as "myHoustonMap.png" .

Contact at TJT@TechnicalJockey.com , if you are looking for an Instructor Based Online Training or Corporate Training in R !

 

We can provide a way for you to geocode these locations . The geocode function works to searching location and returning lat as latitude and lon as longitude for a given location .

This returns the latitude and longitude for West Point , NY .

geocode("West Point, NY")

You can also enter full address to find latitude and longitude .

geocode('1600 Pennsylvania Avenue Northwest, Washington, DC 20500')

We want to create a map of squad . We used stringsAsFactors to not change character data type to factor .  We create a squad data frame that looks like this :

mySquad<-data.frame(

 Names=c("Bill","Dave","Liz","Cody","Austin","Amber","Jimmy","Mike","Amanda"),

 Location=c("Boston, MA","Chicago, IL","Atlanta, GA","Portland, OR","Albuquerque, NM",

            "Dallas, TX","St Louis, MO","Miami, FL","San Francisco, CA"),stringsAsFactors=FALSE)

We create a variable temp to store geocode of all locations in mysquad data frame.

We apply for loop to find geocode for all values of Location . We create two variables lon and lat to store latitude and longitude of all locations respectively.

for(i in 1:9){

 temp<-geocode(mySquad$Location[i]) #Geocode each location and store it in temp

 mySquad$lon[i]<-temp$lon           #Assign the lon

 mySquad$lat[i]<-temp$lat           #Assign the lat

}

We can see mysquad as :

We use color option as "bw" as black and white .We can plot United States map as :

US<-qmap("United States",zoom=4,color="bw")

We add points related to lat and lon values to the map . We used red color to show the points in the map . We used size parameter , which shows the size of point in the map.   We add names value in the map as label associated with points. We add title as "My Squad" .

US+

 geom_point(aes(x = lon, y = lat),color="red",data = mySquad)+

 annotate('text', x=mySquad$lon, y=mySquad$lat+1, label = mySquad$Names, colour = I('red'), size =4)+

 ggtitle("My Squad")

Contact at TJT@TechnicalJockey.com , if you are looking for an Instructor Based Online Training or Corporate Training in R !

 


Contact at TJT@TechnicalJockey.com , if you are looking for an Instructor Based Online Training or Corporate Training in R !

Comments

There are no entries yet.
Please enter the code
* Required fields

Subscribe to our mailing list

* indicates required

Looking for Corporate Training ? Reach out to us at Akriti.Lal@instrovate.com

Reach out to us if you are looking for Corporate Training to Build The Next Generation Analytical Workforce with an in-depth understanding of  Exploratory Data Analysis , Data Visualisation, Data Analytics , AI First , Machine Learning & Deep Learning Training & Consulting helping them to take Data Informed Decision at each stage of the business. 

We understand that At the present times , the Entire Industry is in a Tranformation stage with the Softwares  being rebuilt with Artificial Intelligence Capabilities .

We need SMART WORKFORCE for the SMART SOFTWARES to reap the maximum return . 

 

Whatsapp at +91-9953805788 or email at - akriti.lal@instrovate.com if you would like to know more . 

 

  • Corporate Tableau Training in Gurgaon
  • Corporate Data Analytics Training in Gurgaon
  • Corporate Microsoft Power BI Training in Gurgaon
  • Corporate Microstrategy Training in Gurgaon
  • Corporate Google Data Studio Training in Gurgaon
  • Corporate Python Training in Gurgaon
  • Corporate Advance Analytics in R Programming Training in Gurgaon
  • Corporate Machine Learning Training in Gurgaon
  • Corporate Deep Learning Training in Gurgaon
  • Corporate Data Visualization Training in Gurgaon

 

Address

Instrovate Technologies

Noida, Gurgaon

 

akriti.lal@instrovate.com 

Hit Your Refresh Button To Rise Higher
Print Print | Sitemap
All rights reserved @ Instrovate Technologies
GSTIN : 09AAECI6862K1Z1

Terms Of Services    Privacy Policy    Disclaimer     Refund Policy    


Call

E-mail