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

Highmap R (or) javascript - adding custom legend

这是我的代码片段,

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}") %>%
  })

还有我的数据,

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)

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

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

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)))))

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

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)

现在它可以按照我的预期使用正确的图例。