R is very well known for its powerful visualization capabilities, and many powerful packages for visualization have been written like lattice and ggplot2(). Wouldn’t it be super cool to explore how we can use the ggplot2() and ggmap() packages to make interesting geospatial visualizations?

So let’s do just that!

In order to make it simpler for all of you, let me divide this post into two parts. In the first part we will explore how to download google maps and overlay lat-long data. In part two, we will look at how to extract data with lat-long information from the shape files.

Let‘s begin…

Downloading google maps and overlaying lat-long data

One can easily download google maps using ggmap() package. As you can see from the code snippet below,  google maps can be simply downloaded using the name of the place. Once, we have a google map, we can easily overlay the data on the map if we have lat-long information in our dataset.

I will make use of data from https://data.cityofnewyork.us/Business/MINY-Vendors/79z8-9mcf. This data is about MINY vendors. I extracted this data as a .csv file named as vendor.csv. Let us load the data into R and lets see how we can plot the information on this file using ggmap()

setwd(“C:\\Users\\Gunnvant\\Downloads\\MINY_Vendors“)
library(dplyr)
library(ggmap)
library(ggplot2)
vendor<-read.csv(“vendor.csv“)
vendor<-tbl_df(vendor)
str(vendor)#Notice coords.x1 and coords.x2 denote longitude and lattitude

## Classes ‘tbl_df’, ‘tbl’ and ‘data.frame’:    897 obs. of  21 variables:
##  $ X         : int  1 2 3 4 5 6 7 8 9 10 …
##  $ ID        : int  421 422 423 424 425 426 427 428 429 430 …
##  $ CATEGORY  : Factor w/ 44 levels “Accounting Services”,..: 21 21 21 21 21 21 21 21 21 21 …
##  $ VENDORNAME: Factor w/ 856 levels “\”Murdered by the Mob\” Interactive Comedy Mystery Dinner”,..: 732 735 738 740 747 751 754 755 756 757 …
##  $ WEBSITE   : Factor w/ 756 levels “http://blakleyhotel.com”,..: 151 334 233 276 322 361 402 682 414 423 …
##  $ ADDRESS   : Factor w/ 835 levels “1-7 East 91st Street”,..: 803 320 570 207 154 614 50 171 81 613 …
##  $ ADDRESS2  : Factor w/ 155 levels “# 103″,”#12”,..: NA NA NA NA NA NA NA NA NA NA …
##  $ CITY      : Factor w/ 67 levels “Arverne”,”Astoria”,..: 46 46 46 46 46 46 46 46 46 46 …
##  $ STATE     : Factor w/ 2 levels “NJ”,”NY”: 2 2 2 2 2 2 2 2 2 2 …
##  $ ZIP       : int  10016 10011 10023 10128 10017 10036 10022 10019 10036 10001 …
##  $ TELEPHONE : Factor w/ 818 levels “(201) 370-3659”,..: 207 47 398 143 389 169 386 114 101 99 …
##  $ CONTACT   : Factor w/ 780 levels “* Reuben”,”* Timothy Kim”,..: 339 64 622 732 441 25 764 41 417 451 …
##  $ CONTACTEMA: Factor w/ 739 levels “???”,”212rita@gmail.com”,..: 360 54 586 689 467 57 721 7 412 416 …
##  $ TYPED     : logi  NA NA NA NA NA NA …
##  $ CAMERAICON: Factor w/ 1 level “yes”: NA 1 1 NA NA NA NA 1 NA NA …
##  $ BOROUGH   : Factor w/ 10 levels “Bronx”,”Brooklyn”,..: 5 5 5 5 5 5 5 5 5 5 …
##  $ BCODE     : int  1 1 1 1 1 1 1 1 1 1 …
##  $ HOUSENUM  : Factor w/ 529 levels “1”,”10″,”100″,..: 507 173 338 112 82 370 24 90 42 370 …
##  $ STREETNAME: Factor w/ 423 levels “1 AVENUE”,”10 AVENUE”,..: 244 370 406 163 143 390 149 400 390 379 …
##  $ coords.x1 : num  -74 -74 -74 -74 -74 …
##  $ coords.x2 : num  40.7 40.7 40.8 40.8 40.8 …

#getting map from google maps
map<-get_map(“Newyork“)
ggmap(map)+geom_point(data=vendor,aes(x=coords.x1,y=coords.x2),colour=“blue”,alpha=0.6) #Notice that we are using ggplot2() to overlay data on the map created using ggmap()

map1gmap2g

 

#Suppose we wanted to show all the stores in Long Island City and Staten Island
vendor%>%filter(CITY==”Long Island City”|CITY==”Staten Island”)->venC#Subsetted data
ggmap(map)+geom_polygon(data=venC,aes(x=coords.x1,y=coords.x2,fill=CITY),alpha=0.6)

Hope you found that interesting and you will go on to try a few more variations. Feel free to share your feedback in the comments box below. And look out for part two of this post where we will look at Extracting data with lat-long information from shape files.

Interested in doing geospatial visualizataions for a career?  Well check out the various Data Analytics courses Jigsaw Academy has to offer. Take that first step…become a Data Analyst.

Related Reads:

How Upshot is Using Interactive Visualizations to Make Online News Fun and Interactive

Weave Magic With Interactive Visualizations: Creating Polychart Using rCharts

SHARE
share

Are you ready to build your own career?