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)
route()
function:trip <- (route(from = "rome", to = "milan",structure = "route", output = "simple"))
route_map <- get_map("italy",zoom = 6)
segment
and segment_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$segment_couple <- segment_coupleDraw the final map route_map + geom_point(aes(x = lon, y = lat, size = 5, colour = hours),data = trip)+ geom_line(data = trip,aes(group = segment)) + geom_line(data = trip, aes(group = segment_couple))
This code will result in the following map being plotted within the viewer pane (Reference: Map data 2015 GeoBasis-De/BKG):
In step 1, we define route points using the route()
function. The route()
function queries the Google Maps server to retrieve a set of points describing the requested route. Since we specified output = "simple"
, the output will result in data.frame
with the following attributes:
$ m : num 15 49 66 124 157 173 546 139 393 243 ... $ km : num 0.015 0.049 0.066 0.124 0.157 0.173 0.546 0.139 0.393 0.243 ... $ miles : num 0.00932 0.03045 0.04101 0.07705 0.09756 ... $ seconds : num 3 25 26 20 34 44 134 37 75 45 ... $ minutes : num 0.05 0.417 0.433 0.333 0.567 ... $ hours : num 0.000833 0.006944 0.007222 0.005556 0.009444 ... $ leg : int 1 2 3 4 5 6 7 8 9 10 ... $ lon : num 12.5 12.5 12.5 12.5 12.5 ... $ lat : num 41.9 41.9 41.9 41.9 41.9 ...
Here are all possible arguments to be specified when calling the function:
In step 2, we define the segment
and segment_couple
variables to link trip points. Custom route visualization will be obtained by leveraging the geom_line()
function and passing to it a group
argument in order to link adjacent points in the route (refer to the details of step 3 for further info rmation).
This can be done only after having defined the segment
and segment_couple
variables, which are basically a way to link two points stored in two subsequent points, giving them a common value for the segment
and segment_couple
variables.
Here is how it looks (only last columns are shown here):
> head(trip[,8:11]) lon lat segment segment_couple 1 12.49640 41.90290 1 0 2 12.49623 41.90293 1 1 3 12.49573 41.90318 2 1 4 12.49600 41.90359 2 2 5 12.49710 41.90434 3 2 6 12.49587 41.90541 3 3
As you can easily see, every point is linked to the precedent by the segment variable and is linked to the subsequent by the segment_couple
variable.
We draw the final map in step 3. This step involves the following:
route_map
object through the get_map
function, which produces another query to the Google Maps serverPoints are obtained using the geom_point()
function and passing to it lon
and lat
as x
and y
, while lines are obtained with two calls to the geom_line()
function, the first having the segment variable as the grouping factor (that is, the rule you use to draw lines on your plot) and the second having the segment_couple
variable as the grouping factor. Through this double call, we can be sure that every point is connected to each other.
ggmap
in the introductory paper ggmap: Spatial Visualization with ggplot2 (http://vita.had.co.nz/papers/ggmap.html).