% hc_add_series_map(map = region_map-6ren">
gpt4 book ai didi

javascript - Highmap R(或)javascript - 添加自定义图例

转载 作者:数据小太阳 更新时间:2023-10-29 04:12:18 25 4
gpt4 key购买 nike

这是我的代码片段,

output$map <- renderHighchart({
region_map = hcmap("countries/nz/nz-all")
highchart(type = "map") %>%
hc_title(text = "Average") %>%
hc_add_series_map(map = region_map, df = data1, joinBy = "name", value = "LTA", borderColor = "#141B4D",
color = "color",
showInLegend = TRUE,
borderWidth = 1)
) %>%
hc_tooltip(useHTML = TRUE, headerFormat ="", pointFormat = "{point.name} <br> LTA: {point.value}") %>%
})

enter image description here

还有我的数据,

enter image description here

structure(list(name = c("Auckland", "Bay of Plenty", "Canterbury", 
"Central North Island", "Central Otago / Lakes District", "Coromandel"
), LTA = c(23, 42, 25, 69, 71, 145), Changelabel = c("<20% Decrease",
">20% Decrease", "<20% Decrease", ">20% Decrease", ">20% Decrease",
">20% Decrease"), color = c("#B7DEE8", "#00B0F0", "#B7DEE8",
"#00B0F0", "#00B0F0", "#00B0F0")), .Names = c("name", "LTA",
"Changelabel", "color"), row.names = c(NA, 6L), class = "data.frame")

这里一切都很好,但是当我在这里启用图例时,无论我使用的颜色列如何,它都会给我渐变,我如何将带有 changelabel 的颜色列指定为图例,如

<20% Decrease - color (#B7DEE8)
>20% Decrease - color (#00B0F0)

最佳答案

好的。经过如此多的尝试和错误,我设法做到了这一点。以下是我的做法(在此提供以帮助 future 的读者)。

我在我的数据集中添加了一个名为 value 的列

data1 <- data1 %>% mutate(value = ifelse(Changelabel == ">20% Decrease",1,
ifelse(Changelabel == "<20% Decrease",2,
ifelse(Changelabel == "<20% Increase",3,
ifelse(Changelabel == ">20% Increase",4, 5)))))

然后我为色轴创建了一个数据类:

dclass <- data_frame(from = seq(1, 4, by = 1),
name = c(">20% Decrease","<20% Decrease","<20% Increase",">20% Increase"),
color = c("#00B0F0","#B7DEE8","#92D050","#00B050"))
dclass <- list_parse(dclass)

然后在我的图表制作代码中添加了这一行:

hc_colorAxis(dataClasses = dclass)

现在它可以像我预期的那样使用适当的图例。

关于javascript - Highmap R(或)javascript - 添加自定义图例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47451982/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com