[英]Plot MULTILINESTRING using leaflet
I'm fairly new to leaflet and mapping in R.我对 leaflet 和 R 中的映射相当陌生。 I'm trying to visualize the traffic volume for certain multilinestring in R.
我正在尝试可视化 R 中某些多线串的流量。 The closest answer I found was:
我找到的最接近的答案是:
How to plot MULTILINESTRING in leaflet addPolylines? 如何在 leaflet 中的 plot MULTILINESTRING 添加多段线?
But I want to map those multilinestring based on their volumes or counts.但我想根据它们的数量或数量对这些多线串进行 map。 My multilinestring data were originally in characters, so I used 'sf' library to convert them to sfc_multilinestring.
我的 multilinestring 数据最初是字符,所以我使用 'sf' 库将它们转换为 sfc_multilinestring。 You can find the original dataset here
你可以在这里找到原始数据集
My sample data:我的样本数据:
data <- data.frame(
multilinestring = c("MULTILINESTRING ((-114.06036700906716 51.04831941917631, -114.05790835100508 51.04824965329041))", "MULTILINESTRING ((-114.06876825342002 50.96863425573366, -114.0714654457777 50.96864796962547))", "MULTILINESTRING ((-114.03372206187294 51.053232488239935, -114.03370889695204 51.05088210489753))"),
VOLUME = c(22000,5000,5000))
After converting to sfc_MULTILINESTRING转换为 sfc_MULTILINESTRING 后
data$geom <- st_as_sfc(data$multilinestring)
I have tried the same code in the link (I shared above) but I can't figure out the color intensity.我在链接中尝试了相同的代码(我在上面分享),但我无法弄清楚颜色强度。
leaflet((data$geom) %>%
addTiles() %>%
addPolygons()
The code above plots the multilinestring but I don't know how to change the color intensity of the multilinestrng based on their VOLUME.上面的代码绘制了多线字符串,但我不知道如何根据它们的 VOLUME 更改多线字符串的颜色强度。
This is what I want to get eventually (with my full dataset):这就是我最终想要得到的(使用我的完整数据集):
http://www.gisresources.com/free-us-traffic-count-data-use-maptitude-2018-mapping-software/ http://www.gisresources.com/free-us-traffic-count-data-use-maptitude-2018-mapping-software/
Here's my system and R version:这是我的系统和 R 版本:
platform x86_64-w64-mingw32平台 x86_64-w64-mingw32
arch x86_64拱 x86_64
os mingw32操作系统 mingw32
system x86_64, mingw32系统 x86_64,mingw32
status地位
major 4专业 4
minor 0.2未成年人 0.2
svn rev 78730 svn rev 78730
language R语言 R
version.string R version 4.0.2 (2020-06-22) version.string R 版本 4.0.2 (2020-06-22)
Here's how you can do this.这是您如何做到这一点的方法。 A few things to point out:
需要指出的几点:
st_as_sf()
, which converts the data frame to an sf object.st_as_sf()
,它将数据帧转换为 sf object。colorNumeric()
)colorNumeric()
)leaflet()
leaflet()
的调用中data <- data.frame(
multilinestring = c("MULTILINESTRING ((-114.06036700906716 51.04831941917631, -114.05790835100508 51.04824965329041))", "MULTILINESTRING ((-114.06876825342002 50.96863425573366, -114.0714654457777 50.96864796962547))", "MULTILINESTRING ((-114.03372206187294 51.053232488239935, -114.03370889695204 51.05088210489753))"),
VOLUME = c(22000,5000,5000)
)
data$geom <- st_as_sfc(data$multilinestring)
data <- st_as_sf(data)
pal <- colorNumeric(
palette = "Reds",
domain = data$VOLUME
)
leaflet(data) %>%
addTiles() %>%
addPolylines(color = ~pal(VOLUME))
Lastly, you'll want to customize your color palette, as the low-volume links show up in a pale color which is hard to see.最后,您需要自定义您的调色板,因为低容量链接以难以看到的浅色显示。 See here for more guidance: https://rstudio.github.io/leaflet/colors.html
有关更多指导,请参见此处: https://rstudio.github.io/leaflet/colors.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.