Search code examples
rgoogle-mapsleafletgoogle-directory-api

How to plot polylines in multiple colors in R?


I'm working on a custom route planner in R at the moment. I'm using the output of the Google Maps Directions API. I want to show the route on a map between two places. Everything is going great so far. The only problem is that I don't know how to give the route multiple colors based on Speed at the moment. I searched the internet for a few days and I couldn't find something that fits my goal. That's why I made this post.

This is how my polyline data frame looks like:

Then I visualized it in Leafet with te following code:

#install.packages("leaflet")
library(leaflet)


pal <- colorNumeric(
palette = unique(polyline$Col),
domain = polyline$Speed,
na.color = "#FFFFFF"
)
rm(map)
map <- leaflet()
map <- addTiles(map)
a <- 1
for(a in length(unique(polyline$Step_ID))){


map <- addPolylines(map,lng = polyline$Lon, 
           lat = polyline$Lat,
           data = polyline[polyline$Step_ID==a,],
           color = polyline$col)
a <- a + 1
}
map <- addLegend(map,"bottomright", pal = pal, values = polyline$Speed,
        title = "Speed",
        opacity = 1)
map

So far I think you have to create multiple PolyLines(correct me if I'm wrong) to plot multiple colors in the route. That's why I made a for loop, to add ever PolyLine into the map.

This is how my visualization looks like

Everthing is just how want it. The only problem is the coloring of the line. I want the coloring of the lines just like Google does with traffic.

Can someone help me out with this please?


Solution

  • To fully replicate your question you need to provide us with the actual data for polyline (i.e, not a screenshot). Until then, I'm going to create my own data set and show you how to create the coloured lines.

    And, as you're using Google's API to get the directions, I'm assuming you'll have an API key, so I'm going to show you how to do it using my googleway package

    library(googleway)
    
    api_key <- "your_api_key"
    
    directions <- google_directions(origin = "St Kilda, Melbourne, Australia",
                                    destination = "Geelong, Victoria, Australia",
                                    key = api_key)
    
    ## the results of the API give you distance in metres, and time in seconds
    ## so we need to calculate teh speed
    spd <- (directions$routes$legs[[1]]$steps[[1]]$distance$value / 1000) / (directions$routes$legs[[1]]$steps[[1]]$duration$value/ 60 / 60)
    
    ## then we can start to build the object to use in the plot
    ## and as we are staying within Google's API, we can use the encoded polyline to plot the routes
    ## rather than extracting the coordinates
    df <- data.frame(speed = spd,
                     polyline = directions$routes$legs[[1]]$steps[[1]]$polyline)
    
    
    
    df$floorSpeed <- floor(df$speed)
    colours <- seq(1, floor(max(df$speed)))
    colours <- colorRampPalette(c("red", "yellow","green"))(length(colours))
    
    df <- merge(df, 
                data.frame(speed = 1:length(colours), 
                           colour = colours), 
                by.x = "floorSpeed", 
                by.y = "speed")
    
    map_key <- "your_map_api_key"
    
    google_map(key = map_key) %>%
        add_polylines(data = df, polyline = "points", stroke_colour = "colour",
                       stroke_weight = 5, stroke_opacity = 0.9)
    

    enter image description here

    See this answer for a way of making the route planner in Shiny.