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

8
这是我的片段:
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)

第一段代码无法解析(右括号太多),output$map 可以简写为 map,对吧?最后,region_map 的名称也找不到。我必须说:这些地图问题非常有趣! - storaged
是的,这只是一个输出。您可以使用Highmap提供的nz / nz-all地图。更新了。 - ds_user
1个回答

9

好的。经过多次尝试和错误,我成功地完成了这件事。以下是我的做法(为帮助未来读者提供)。

我在数据集中添加了一个名为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)

现在它能够按照我期望的正确显示图例。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接