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...
- Define the route points using the
route()
function:trip <- (route(from = "rome", to = "milan",structure = "route", output = "simple"))
- Create the map where you want to draw the route:
route_map <- get_map("italy",zoom = 6)
- Define the
segment
andsegment_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 books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.