簡體   English   中英

Highmap R(或)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)

好。 經過這么多試驗和錯誤,我設法做到了這一點。 我是這樣做的(這里是為了幫助未來的讀者)。

我在我的數據集中添加了一個名為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)

現在它按照我的預期使用正確的圖例。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM