O'Reilly logo

RStudio for R Statistical Computing Cookbook by Andrea Cirillo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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)){ ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required