[英]How to draw a line with color based on a numeric variable using Leaflet in R
這是怎么回事!
我有一個數據框,其中每一行都有 object 的即時速度和坐標(緯度和經度)信息。
我正在嘗試畫一條連接所有這些經緯度的線,這條線的顏色就是速度。 任何人都可以幫助我嗎?
這是一些代碼:
require(leaflet)
df <- data.frame(Latitude = seq(14, 15, length.out = 100),
Longitude = seq(45, 46, length.out = 100),
Speed = runif(100, 50, 70))
pal <- colorNumeric(palette = "Greens", domain = df$Speed)
leaflet()%>%
addTiles() %>%
addPolylines(data = df,
lng = ~Longitude,
lat = ~Latitude,
color = ~pal(Speed))
library(leaflet)
library(tidyverse)
df <- data.frame(lat = seq(14, 15, length.out = 100),
lng = seq(45, 46, length.out = 100),
Speed = runif(100, 50, 70))
pal <- colorNumeric(palette = "Greens", domain = df$Speed)
colorGradient <- df$Speed %>%
pal
df1 <- df %>%
mutate(nextLat = lead(lat),
nextLng = lead(lng),
color = colorGradient
)
gradient_map <- leaflet() %>%
addTiles()
for (i in 1:nrow(df1)) {
gradient_map <- addPolylines(map = gradient_map,
data = df1,
lng = as.numeric(df1[i, c('lng', 'nextLng')]),
lat = as.numeric(df1[i, c('lat', 'nextLat')]),
color = as.character(df1[i, c('color')])
)
}
gradient_map
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.