Highmap R (or) javascript - adding custom legend
Asked Answered
S

1

8

Here is my snippet,

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

And my data 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")

Everything is fine here, but when i enable the legend here, it is giving me the gradient irrespective of the color column I am using, how do i specify the color column with changelabel as legend like

<20% Decrease - color (#B7DEE8)
>20% Decrease - color (#00B0F0)
Sholokhov answered 23/11, 2017 at 9:32 Comment(2)
The first chunk of code does not parse (too many right brackets), output$map can be just map, right? Finally, the region_map name is also not found. I have to say: these maps questions are quite interesting!Endoplasm
Yes it’s just a output. And you can use nz/nz-all map provided by highmap. updatedSholokhov
S
9

Ok. After so many trial and errors, I managed to do this. Here is how I did (providing here to help future readers).

I added a column called value in my dataset

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

And then I created a data class for color axis:

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)

Then in my chart making code I added this line:

hc_colorAxis(dataClasses = dclass)

Now it works with proper legend as I expected.

Sholokhov answered 26/11, 2017 at 9:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.