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.
As you can imagine, we first need to install and load the ggmap
package:
install.packages("ggmap") library(ggmap)
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)){ if(i == 1){segment[i] <- 1}else{ if( i %% 2 != 0 ){ segment [i] <- i-segment[i-1]}else{ segment [i] <- i/2 } } } segment_couple <- c(0,segment[-length(segment)]) trip$segment <- segment trip...