如何在 AddTimeLine 中设置自定义颜色
How to set custom color in 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, 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
)
)
)
不幸的是,我收到以下错误:
Error in lapply(x, f) : object 'color_temp' not found
我也试过把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
}
);
}
"
)
)
)
我正在尝试使用 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
)
)
)
不幸的是,我收到以下错误:
Error in lapply(x, f) : object 'color_temp' not found
我也试过把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
}
);
}
"
)
)
)