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)
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!
    – storaged
    Commented Nov 23, 2017 at 10:49
  • Yes it’s just a output. And you can use nz/nz-all map provided by highmap. updated
    – ds_user
    Commented Nov 23, 2017 at 16:51

1 Answer 1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.