我正在尝试使用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, 54.140586, 48.494444, 48.494444
),
"Longitude" = c(
129.837222, -77.789444, 7.563056, 8.415278, 9.175, -82.343889,
13.664422, 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"), by = "day", 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
}
);
}
"
)
)
)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句