In our previous post The Wonderful World of Geospatial Visualizations Explained in 2 Minutes, we learnt how to download google maps and overlay lat-long data. In this post, we will look at how to extract data with lat-long information from the shape files.

Very often while working on geospatial visualizations, we don’t have the lattitude longitude information. Most of the geospatial data (data with lat-long information) is in shape files and extracting the data from a shape file can be a little tricky. Let me now illustrate how we can do just this, i.e extract data out of shape files which are of the class “SpatialPointsDataFrame”.Some shape files are read as a class “SpatialPolygonDataFrame”, in order to extract data out of such files one can refer to http://cran.r-project.org/doc/contrib/intro-spatial-rl.pdf

Below are the steps to extract data out of a file of class “SpatialPointsDataFrame”

(1) Read the shape file using rgdal() pacakage

(2) Check if the location data is in lat long format

(i) If not, convert into lat long format

(3) Combine the data and lat long information into one dataframe, and extract a .csv file

Below is the code for extracting data from a shape file.

 

library(rgdal)

library(ggplot2)
library(ggmap)
library(rgeos)

setwd(“C:\\Users\\Gunnvant\\Downloads\\MINY_Vendors”)
sh<-readOGR(dsn=”.”,”DOITT_MINY_VENDOR_01_13SEPT2010″)#Reading shape file using rgdal() library’s readOGR command

## OGR data source with driver: ESRI Shapefile
## Source: “.”, layer: “DOITT_MINY_VENDOR_01_13SEPT2010”
## with 897 features
## It has 18 fields

head(sh@coords)#Notice that the coordinates don’t seem to be in lat-long format

##      coords.x1 coords.x2
## [1,]    988225    210485
## [2,]    985113    210450
## [3,]    989037    220311
## [4,]    996773    223379
## [5,]    991844    214237
## [6,]    989257    214660

class(sh)

## [1] “SpatialPointsDataFrame”
## attr(,”package”)
## [1] “sp”

sh<-spTransform(sh,CRS(“+init=epsg:4326″))#Using spTrandform function to convert the location data to lat-long format, +init=epsg:4326 is the coordinate refrence code for lat-long data
vendor<-data.frame(sh@data,sh@coords)#Combining data with location information
head(tbl_df(vendor))

## Source: local data frame [6 x 20]
##
##    ID CATEGORY                     VENDORNAME
## 1 421   Hotels     The Carlton Madison Avenue
## 2 422   Hotels              The Chelsea Hotel
## 3 423   Hotels               The Empire Hotel
## 4 424   Hotels             The Franklin Hotel
## 5 425   Hotels The Helmsley Middletowne Hotel
## 6 426   Hotels          The Iroquois New York
## Variables not shown: WEBSITE (fctr), ADDRESS (fctr), ADDRESS2 (fctr), CITY
##   (fctr), STATE (fctr), ZIP (dbl), TELEPHONE (fctr), CONTACT (fctr),
##   CONTACTEMA (fctr), TYPED (fctr), CAMERAICON (fctr), BOROUGH (fctr),
##   BCODE (dbl), HOUSENUM (fctr), STREETNAME (fctr), coords.x1 (dbl),
##   coords.x2 (dbl)

write.csv(vendor,”vendor.csv”)”)#Extracting the dataset with lat-long data as a .csv file.

 

For creating interesting geospatial visualizations in R, I think the following workflow is most suitable:

  1. If one already has a dataset with lat-long information, then simply overlay that data on a google map using ggmap() and ggplot2()

2.If someone wants to get data with lat-long information, then the best way is to read shapefiles into R, combine the coordinates information with the data into a dataframe and then use ggmap() and ggplot2() to make visualizations.

Why don’t you try this out? And then write in and tell me all about it. Such tasks will help build your analytics skill set and confidence. At interviews you will be able to talk about it and woo your recruiters. Go on then get started!

To find out more about careers in analytics, take a look at the various analytics courses Jigsaw has to offer.

Image courtesy freedigitalphotos.net. By vectorolie

Related Reads:

The Wonderful World of Geospatial Visualizations Explained in 2 Minutes

Weave Magic With Interactive Visualizations: Creating Polychart Using rCharts

SHARE
share

Are you ready to build your own career?