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

时间:2017-11-23 09:32:37

标签: javascript r highcharts highmaps r-highcharter

这是我的代码段,

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)

1 个答案:

答案 0 :(得分:8)

确定。经过这么多试验和错误,我设法做到了这一点。我就是这样做的(这里是为了帮助未来的读者)。

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

if (@available(iOS 11.0, *)) {
    toolbarHeight += UIApplication.sharedApplication.keyWindow.safeAreaInsets.bottom;
}

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

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)

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