-1

I'd like to map 2 different routes on 1 map. This is the code I made as an example:

### Plot 2 routes on 1 map

# libraries

library(ggmap)
library(ggplot2)

# plot map

basicmap <- get_map(location = c(lon = 3, lat = 50),
                    zoom = 12,
                    maptype = "roadmap",
                    source = "google",
                    color = "color")  #completely random location
basicmap <- ggmap(basicmap)

# determine routes

routes <- data.frame(from = c("Rocquigny", "Nurlu"),
                     to = c("Heudicourt","Longavesnes"),
                     stringsAsFactors = FALSE)

# calculate routes

calculationroute <- function(startingpoint, stoppoint) {
  route(from = startingpoint,
        to = stoppoint,
        mode = "bicycling",
        structure = "route")
}
  #this function calculates the route

calculatedroutes <- mapply(calculationroute,
                           startingpoint = routes$from,
                           stoppoint = routes$to,
                           SIMPLIFY = FALSE)
  #calculate the 2 routes

# draw routes

drawroute <- function(route) {
  geom_path(aes(data = route,
                x = lon,
                y = lat))
}
  #this functions draws the route

extendedmap <- basicmap + lapply(X = calculatedroutes,
                                 FUN = drawroute)
plot(extendedmap)
  #draw the routes

So theses are the steps I take:

  1. Background map is created
  2. I make a dataframe which contains the starting points and endpoints for 2 routes
  3. Using ggmap I calculate the segments of these routes.
  4. I try to draw the routes on top of the background map

Sadly the last step fails and this error occurs:

Don't know how to automatically pick scale for object of type function. Defaulting to continuous
Error in data.frame(x = c(2.89030838012694, 3.11003494262694, 2.89030838012694,  : 
  arguments imply differing number of rows: 4, 0

I did a search and this previous question wanted to do the same thing. The solution he finally chose uses a for loop and I'd rather keep my code consistent by using lapply instead.

Community
  • 1
  • 1
1053Inator
  • 302
  • 1
  • 15
  • the `for` loop is a fine solution for this since it's adding geoms in a very standard way for plotting in ggplot. there's nothing wrong with using `for` loops in R and/or mixing them with use of the `*apply` idiom. – hrbrmstr Aug 09 '15 at 17:50

1 Answers1

1

As I said in the comment, the for loop works, but you can do this in an even more ggplot-ish way. First, we need to change your ggmap call since the extents aren't large enough for one of your routes:

basicmap <- get_map(location = c(lon = 3, lat = 50),
                    zoom = 8,
                    maptype = "roadmap",
                    source = "google",
                    color = "color")  #completely random location
basicmap <- ggmap(basicmap)

Once you calculate the routes, we can make a long data frame of them:

do.call(rbind.data.frame, lapply(names(calculatedroutes), function(x) {
  cbind.data.frame(route=x, calculatedroutes[[x]], stringsAsFactors=FALSE)
})) -> long_routes

and then use a group aesthetic for the geom_path:

basicmap + geom_path(data=long_routes, 
                     aes(x=lon, y=lat, group=route, color=route),
                     size=1)

enter image description here

hrbrmstr
  • 77,368
  • 11
  • 139
  • 205
  • Thanks for the answer, this works. And I'm sorry about the map not being large enough, I adapted this example from my original code and missed this lapsus. – 1053Inator Aug 10 '15 at 07:59