【问题标题】:How to plot polylines in multiple colors in R?如何在R中绘制多种颜色的折线?
【发布时间】:2017-02-22 11:50:14
【问题描述】:

我目前正在 R 中开发自定义路线规划器。我正在使用 Google Maps Directions API 的输出。我想在两个地方之间的地图上显示路线。到目前为止一切都很好。唯一的问题是我目前不知道如何根据 Speed 为路线提供多种颜色。我在互联网上搜索了几天,但找不到符合我目标的东西。这就是我发这篇文章的原因。

然后我在 Leafet 中使用以下代码将其可视化:

#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

到目前为止,我认为您必须创建多个折线(如果我错了,请纠正我)以在路线中绘制多种颜色。这就是为什么我做了一个 for 循环,将 PolyLine 添加到地图中。

一切都是想要的。唯一的问题是线条的着色。我想要线条的颜色,就像 Google 对流量所做的那样。

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

【问题讨论】:

  • 你能dput你的数据集吗
  • .Label = c("red", "orange", "green"), class= "factor")), .Names = c("Step_ID", "Dist_Value", "Dist_Text" , "Dur_Text", "Dur_Value", "Lat", "Lon", "Speed", "Col"), row.names = c(NA, 1448L), class= "data.frame")

标签: r google-maps leaflet google-directory-api


【解决方案1】:

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

而且,当您使用 Google 的 API 来获取路线时,我假设您将拥有一个 API 密钥,所以我将向您展示如何使用我的 googleway 包进行操作

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)

请参阅this answer,了解在 Shiny 中制作路线规划器的方法。

【讨论】:

  • 哎呀对不起!我是新来的网站。谢谢,这对我有用!它仅适用于我的个人 Mac,不适用于我实习期间的 Windows 计算机。有什么奇怪的。但我会弄清楚问题出在哪里。但是感谢您的帮助和您的包裹!
  • @DaveMeijdam - 在 Windows 上安装软件包或查看地图是否存在问题?
  • 当我运行devtools::install_github("SymbolixAU/googleway") 时它不会给出任何错误。但是当我运行绘制地图的代码时,它什么也没显示。查看器保持空白。
  • @DaveMeijdam - 是的,我知道这个问题;我认为它在 RStudio 本身内,但我无法找到它。如果您选择“在浏览器中打开”,它将在浏览器窗口中显示。
  • 啊,我明白了,谢谢!你认为它会在闪亮时工作吗?
猜你喜欢
  • 2016-06-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-15
  • 1970-01-01
  • 2015-03-18
  • 2021-09-28
  • 1970-01-01
相关资源
最近更新 更多