Introduction

The objective is to explore ‘ggmap’ package in R and use this package to plot points on the map. For this post, I’ll be using the map of India. Initially, I’ll try to explain some of the basic functions in ggmap and then I’ll explain by plotting different airports in India. Let’s start..

Load the libraries

library(ggmap)
## Warning: package 'ggmap' was built under R version 3.3.3
## Loading required package: ggplot2
## I am also loading ggplot2 with ggmap as ggmap() returns a ggplot object and we can use this object to apply all of the ggplot functions
library(ggplot2)

Get map of India and plot it

get_map is a smart wrapper that queries the Google Maps, OpenStreetMap, Stamen Maps or Naver Map servers for a map. In this post, I’ve used google as the default source.

map <- get_map(location = 'India', zoom = 4)
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=India&zoom=4&size=640x640&scale=2&maptype=terrain&language=en-EN&sensor=false
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=India&sensor=false

Understanding Basic Map Types available in get_map map_type option

## Basic Map type
ggmap(map)

## I've used zoom value 10 for subsequent charts to make it easier to understand the difference in the output
## Roadmap 
roadmap <- get_map("India", maptype='roadmap', zoom = 10)
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=India&zoom=10&size=640x640&scale=2&maptype=roadmap&language=en-EN&sensor=false
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=India&sensor=false
ggmap(roadmap)

## Hybrid
hybird <- get_map("India", maptype='hybrid', zoom = 10)
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=India&zoom=10&size=640x640&scale=2&maptype=hybrid&language=en-EN&sensor=false
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=India&sensor=false
ggmap(hybird)

## Satellite
satellite <- get_map("India", maptype='satellite', zoom = 10)
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=India&zoom=10&size=640x640&scale=2&maptype=satellite&language=en-EN&sensor=false
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=India&sensor=false
ggmap(satellite)

## Terrain
terrain <- get_map("India", maptype='terrain', zoom = 10)
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=India&zoom=10&size=640x640&scale=2&maptype=terrain&language=en-EN&sensor=false
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=India&sensor=false
ggmap(terrain)

Using latitude and Longitude data to plot points on the map

Load the data file. The dataset is available here: https://data.humdata.org/dataset/ourairports-ind

airports <- read.csv("C://Users//Abhay//Desktop//in-airports.csv", header=T)

## Listing out first few rows
head(airports)
##      id ident          type                                      name
## 1 26555  VIDP large_airport       Indira Gandhi International Airport
## 2 26434  VABB large_airport Chhatrapati Shivaji International Airport
## 3 35145  VOBL large_airport          Kempegowda International Airport
## 4 26618  VOMM large_airport             Chennai International Airport
## 5 26444  VAGO large_airport                           Dabolim Airport
## 6 26609  VOCI large_airport              Cochin International Airport
##   latitude_deg longitude_deg elevation_ft continent iso_country iso_region
## 1     28.56650       77.1031          777        AS          IN      IN-DL
## 2     19.08870       72.8679           39        AS          IN      IN-MM
## 3     13.19790       77.7063         3000        AS          IN      IN-KA
## 4     12.99001       80.1693           52        AS          IN      IN-TN
## 5     15.38080       73.8314          150        AS          IN      IN-GA
## 6     10.15200       76.4019           30        AS          IN      IN-KL
##    municipality scheduled_service gps_code iata_code local_code
## 1     New Delhi                 1     VIDP       DEL           
## 2        Mumbai                 1     VABB       BOM           
## 3     Bangalore                 1     VOBL       BLR           
## 4       Chennai                 1     VOMM       MAA           
## 5 Vasco da Gama                 1     VOGO       GOI           
## 6        Cochin                 1     VOCI       COK           
##                                        home_link
## 1                 http://www.newdelhiairport.in/
## 2                            http://www.csia.in/
## 3 http://www.bengaluruairport.com/home/home.jspx
## 4                                               
## 5                                               
## 6                 http://www.cochin-airport.com/
##                                                           wikipedia_link
## 1       http://en.wikipedia.org/wiki/Indira_Gandhi_International_Airport
## 2 http://en.wikipedia.org/wiki/Chhatrapati_Shivaji_International_Airport
## 3         https://en.wikipedia.org/wiki/Kempegowda_International_Airport
## 4             http://en.wikipedia.org/wiki/Chennai_International_Airport
## 5                           http://en.wikipedia.org/wiki/Dabolim_Airport
## 6              http://en.wikipedia.org/wiki/Cochin_International_Airport
##                                                                      keywords
## 1                                                     Palam Air Force Station
## 2                                         Bombay, Sahar International Airport
## 3                                                                            
## 4                                                                            
## 5 Goa Airport, Dabolim Navy Airbase, दाबà¥<U+008B>ळà¥<U+0080> विमानतळ
## 6                                                                      Cochin
##     score              last_updated
## 1   51475 2017-06-06T21:37:26+00:00
## 2 1014475 2013-04-12T01:27:48+00:00
## 3   51200 2016-02-01T17:54:36+00:00
## 4   51150 2008-12-07T02:26:07+00:00
## 5     875 2013-04-27T02:00:28+00:00
## 6    1000 2011-09-02T04:51:36+00:00
## I'll be using latitude_deg and longitude_deg columns to plot the points on the map
## geom_point() is a ggplot2 function and it plots the points on the top of the map
points <- ggmap(map) + geom_point(aes(x = longitude_deg, y = latitude_deg), data = airports, alpha = .5)

points

Varying the size of the points depending upon the elevation of the airport

variablePoints <- ggmap(map) + geom_point(aes(x = longitude_deg, y = latitude_deg, size = elevation_ft), data = airports, alpha = .5)

## Modify the legend name
updatedMap <- variablePoints + scale_size_area(name = "Elevation Level (in feet)")

updatedMap

qmplot()

Another way to quickly plot a map is by using qmplot(). It automatically downloads the map based on values passed and plot the points on the map.

qmplot(longitude_deg, latitude_deg, data=airports)
## Using zoom = 5...
## Map from URL : http://tile.stamen.com/toner-lite/5/21/12.png
## Map from URL : http://tile.stamen.com/toner-lite/5/22/12.png
## Map from URL : http://tile.stamen.com/toner-lite/5/23/12.png
## Map from URL : http://tile.stamen.com/toner-lite/5/24/12.png
## Map from URL : http://tile.stamen.com/toner-lite/5/21/13.png
## Map from URL : http://tile.stamen.com/toner-lite/5/22/13.png
## Map from URL : http://tile.stamen.com/toner-lite/5/23/13.png
## Map from URL : http://tile.stamen.com/toner-lite/5/24/13.png
## Map from URL : http://tile.stamen.com/toner-lite/5/21/14.png
## Map from URL : http://tile.stamen.com/toner-lite/5/22/14.png
## Map from URL : http://tile.stamen.com/toner-lite/5/23/14.png
## Map from URL : http://tile.stamen.com/toner-lite/5/24/14.png
## Map from URL : http://tile.stamen.com/toner-lite/5/21/15.png
## Map from URL : http://tile.stamen.com/toner-lite/5/22/15.png
## Map from URL : http://tile.stamen.com/toner-lite/5/23/15.png
## Map from URL : http://tile.stamen.com/toner-lite/5/24/15.png
## Warning: `panel.margin` is deprecated. Please use `panel.spacing` property
## instead

We can further use gplot2 functions on the object returned by ggmap() to modify the points to suit our requirements.