Mapping techniques with R

There are several ways to plot maps with R. Some of the techniques I have tried are covered here, either gathered through diferent sources or written on my own.

library(ggmap)
## Loading required package: ggplot2
library(geosphere)
## Loading required package: sp
library(maps)
library(ggplot2)

# this example shows obtaining coordinates (latitude, longitude) using
# map_world function, and drawing them using geom_polygon
world = map_data("world")
basemap = ggplot(legend = FALSE) + geom_polygon(data = world, aes(x = long, 
    y = lat, group = group, fill = group)) + theme(legend.position = "none")  # geom_point could also have been used instead of geom_polygon, depending on required aesthetics. See effect on map by not specifying fill option, or move fill to outside aes() to choose a fixed fill vs a group fill.
basemap

plot of chunk unnamed-chunk-1

basemap = basemap + theme(panel.background = element_rect(fill = "#000000"), 
    panel.grid.major = element_blank(), panel.grid.minor = element_blank())
basemap

plot of chunk unnamed-chunk-1


# now that we have basemap, let's try to plot a eucliean line betewen
# Bangalore, India nd New York, USA
ban = geocode("Bangalore, India")
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Bangalore,+India&sensor=false
## Google Maps API Terms of Service : http://developers.google.com/maps/terms
ny = geocode("New York, USA")
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=New+York,+USA&sensor=false
## Google Maps API Terms of Service : http://developers.google.com/maps/terms
inter = as.data.frame(gcIntermediate(c(ban$lon, ban$lat), c(ny$lon, ny$lat), 
    n = 50, addStartEnd = TRUE))
# names(inter) = c('lon', 'lat') # if required
basemap + geom_line(data = inter, aes(x = lon, y = lat), color = "blue")

plot of chunk unnamed-chunk-1

# if euclidean line is not required, for a straight line, use geom_segment
basemap + geom_segment(aes(x = ban$lon, y = ban$lat, xend = ny$lon, yend = ny$lat), 
    color = "blue", size = 0.3)

plot of chunk unnamed-chunk-1


# this example illustrates plotting driving map between two locations. It
# uses route function that queries Google Maps API.
rt1 = route(from = "Bangalore", to = "Chennai", mode = "driving")
## Information from URL : http://maps.googleapis.com/maps/api/directions/json?origin=Bangalore&destination=Chennai&mode=driving&units=metric&alternatives=false&sensor=false
## Google Maps API Terms of Service : http://developers.google.com/maps/terms
BangaloreMap = qmap("Bangalore", zoom = 7, color = "bw")
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=Bangalore&zoom=7&size=%20640x640&scale=%202&maptype=terrain&sensor=false
## Google Maps API Terms of Service : http://developers.google.com/maps/terms
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Bangalore&sensor=false
## Google Maps API Terms of Service : http://developers.google.com/maps/terms
BangaloreMap + geom_leg(aes(x = startLon, y = startLat, xend = endLon, yend = endLat), 
    color = "green", data = rt1)

plot of chunk unnamed-chunk-1


# this example shows getting a map using qmap function
HoustonMap = qmap("Houston", zoom = 14, color = "bw", legend = "topleft")
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=Houston&zoom=14&size=%20640x640&scale=%202&maptype=terrain&sensor=false
## Google Maps API Terms of Service : http://developers.google.com/maps/terms
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Houston&sensor=false
## Google Maps API Terms of Service : http://developers.google.com/maps/terms
HoustonMap

plot of chunk unnamed-chunk-1

# now plot crime data on Houston map using geom_point on top of the map
HoustonMap + geom_point(aes(x = lon, y = lat, colour = offense, size = offense), 
    data = crime)  # plots points on lat long
## Warning: Removed 80568 rows containing missing values (geom_point).

plot of chunk unnamed-chunk-1