LOADING

Type to search

Spatial Visualization Using R Part – 1

To Know more about the Different Corporate Training & Consulting Visit our website www.Instrovate.com Or Email : info@instrovate.com or WhatsApp / Call at +91 74289 52788

Data Analytics R Programming

Spatial Visualization Using R Part – 1

Share

Install “ggmap” package by using following code:

install.packages(“ggmap”)

Load “ggplot2” and “ggmap” packages to use.

library(ggplot2)

library(ggmap)

Spatial Visualization Using R Part - 1 29

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”)

Spatial Visualization Using R Part - 1 30

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)

Spatial Visualization Using R Part - 1 31
Spatial Visualization Using R Part - 1 32

Contact at info@instrovate.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”)

Spatial Visualization Using R Part - 1 33

We can show toner type map as:

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

Spatial Visualization Using R Part - 1 34

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

from <- “houston”

to <- “waco, texas”

mapdist(from, to)

Spatial Visualization Using R Part - 1 35

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’)

Spatial Visualization Using R Part - 1 36

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)

Spatial Visualization Using R Part - 1 37

We check the names of all columns of crime dataset .

names(crime)

Spatial Visualization Using R Part - 1 38

We check some observations of crime dataset as :

head(crime)

Spatial Visualization Using R Part - 1 39

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)

Spatial Visualization Using R Part - 1 40

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

Spatial Visualization Using R Part - 1 41

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)    

Spatial Visualization Using R Part - 1 42

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)

Spatial Visualization Using R Part - 1 43

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”)

Spatial Visualization Using R Part - 1 44

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 info@instrovate.com , if you are looking for an Instructor Based Online Training or Corporate Training in R !

Spatial Visualization Using R Part - 1 45

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”)

Spatial Visualization Using R Part - 1 46

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

geocode(‘1600 Pennsylvania Avenue Northwest, Washington, DC 20500’)

Spatial Visualization Using R Part - 1 47

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)

Spatial Visualization Using R Part - 1 48

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

}

Spatial Visualization Using R Part - 1 49

We can see mysquad as :

Spatial Visualization Using R Part - 1 50

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 info@instrovate.com , if you are looking for an Instructor Based Online Training or Corporate Training in R !

Spatial Visualization Using R Part - 1 51