如何在R中以多种颜色绘制多段线? [英] How to plot polylines in multiple colors in R?

查看:304
本文介绍了如何在R中以多种颜色绘制多段线?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我正在研究R中的一个自定义路由规划器。我正在使用Google Maps Directions API的输出。我想在两个地方之间的地图上显示路线。到目前为止,一切都很顺利。唯一的问题是,我现在不知道如何根据速度给出多种颜色的路线。我在网上搜索了几天,找不到符合我目标的东西。这就是为什么我发了这篇文章。





然后我用Le在下面的代码中使用te来显示它:

<$ p $ #install.packages(leaflet)
库(小册子)


pal < - colorNumeric(
palette = unique (折线$ Col),
domain =折线$ Speed,
na.color =#FFFFFF

rm(地图)
地图< - leaflet (a)长度(独特(多段线$ Step_ID))){

$(b)地图< - addTiles(地图)
a < - 1
b $ b 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

到目前为止,我认为你必须c reate多个PolyLines(纠正我,如果我错了)绘制路线中的多种颜色。这就是为什么我做了for循环,以便将PolyLine添加到地图中。





Everthing就是这么想的。唯一的问题是线的着色。我希望像Google那样对流量着色。



有人可以帮我解决这个问题吗?

解决方案

为了完全复制您的问题,您需要向我们提供 polyline 的实际数据(即不是屏幕截图)。在此之前,我将创建自己的数据集并向您展示如何创建彩色线条。



而且,当您使用Google的API获取方向,我假设你将有一个API密钥,所以我要告诉你如何使用我的googleway软件包来做到这一点

  library(googleway)

api_key< - your_api_key

directions< - google_directions(origin =St Kilda,Melbourne,Australia,
destination =澳大利亚维多利亚州季隆市,
key = api_key)

## API的结果给你以米为单位的距离,以秒为单位的时间
# #(所以我们需要计算速度
spd< - (方向$ routes $ legs [[1]] $ steps [[1]] $ distance $ value / 1000)/(directions $ routes $ legs [[ 1]] $ steps [[1]] $ duration $ value / 60/60)

##然后我们可以开始构建要在plot
##中使用的对象,并且as我们留在Google的API中,我们可以e编码的多段线绘制路线
##而不是提取坐标
df< - data.frame(speed = spd,
polyline = directions $ routes $ legs [[1] ] $ steps [[1]] $ polyline)



df $ floorSpeed < - floor(df $ speed)
colors < - seq(1 ,floor(max(df $ speed)))
colors < - colorRampPalette(c(red,yellow,green))(length(colors))

df< - merge(df,
data.frame(speed = 1:length(colors),
color = colors),
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 =color,
stroke_weight = 5,stroke_opacity = 0.9)



请参阅此答案 a>来制作Shiny中的路线规划器。

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.

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.

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?

解决方案

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)

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

这篇关于如何在R中以多种颜色绘制多段线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆