Drawing a route on a map with ggmap

Working with geospatial data is a simple task with R. Thankfully, the ggmap package provides a good number of facilities for this task.

In particular, this recipe gives you the ability to draw on a map a custom defined route from one point to another.

Getting ready

As you can imagine, we first need to install and load the ggmap package:

install.packages("ggmap")
library(ggmap)

How to do it...

  1. Define the route points using the route() function:
    trip      <- (route(from = "rome", to = "milan",structure = "route", output = "simple"))
  2. Create the map where you want to draw the route:
    route_map <- get_map("italy",zoom = 6) 
  3. Define the segment and segment_couple variables to link trip points:
    segment <- c() for(i in 1:nrow(trip)){ ...

Get RStudio for R Statistical Computing Cookbook now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.