如何解决如何在 AddTimeLine 中设置自定义颜色
我正在尝试使用 leaflet
和 leaftime
包创建时间线图。我想在addTimeline
中设置自定义颜色来指定每个点到他的组,如下:
library(leaflet)
library(leaftime)
library(geojsonio)
power_d <- data.frame(
"Latitude" = c(
33.515556,38.060556,47.903056,49.71,49.041667,31.934167,54.140586,48.494444,48.494444
),"Longitude" = c(
129.837222,-77.789444,7.563056,8.415278,9.175,-82.343889,13.664422,17.681944,17.681944
),"start" = seq.Date(as.Date("2015-01-01"),by = "day",length.out = 10),"end" = seq.Date(as.Date("2015-01-01"),length.out = 10) + 1,color_temp=rep(c("red","blue","green"),len=10)
)
power_geo <- geojsonio::geojson_json(power_d,lat="Latitude",lon="Longitude")
leaflet() %>%
addTiles() %>%
setView(44.0665,23.74667,2) %>%
addTimeline(data = power_geo,timelineOpts = timelineOptions(
styleOptions = styleOptions(
radius = 5,color=color_temp,fillColor = color_temp,fillOpacity = 1
)
)
)
不幸的是,我收到以下错误:
lapply(x,f) 中的错误:找不到对象“color_temp”
我也尝试用 color_temp
替换 power_d$color_temp
,代码运行没有错误,但点的颜色没有改变。颜色参数在上面的代码中不起作用,为什么?以及如何修复它?
解决方法
似乎您无法使用标准 styleOptions
传递颜色向量,但是,?addTimeline
帮助页面中的示例显示了如何根据数据添加颜色使用一点 JavaScript(幸好在示例中提供了)。
使用以“# 根据数据为每个点设置不同样式”开头的示例,您需要稍微更改它以指向您的颜色向量,例如将 data.properties.color
更改为 data.properties.color_temp
。运行下面的代码会导致
# code
leaflet(power_geo) %>%
addTiles() %>%
setView(44.0665,23.74667,2) %>%
addTimeline(
timelineOpts = timelineOptions(
styleOptions = NULL,pointToLayer = htmlwidgets::JS(
"
function(data,latlng) {
return L.circleMarker(
latlng,{
radius: 25,color: data.properties.color_temp,fillColor: data.properties.color_temp,fillOpacity: 1
}
);
}
"
)
)
)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。