Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Highmap R (or) javascript - adding custom legend

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)
like image 478
ds_user Avatar asked Nov 23 '17 09:11

ds_user


1 Answers

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.

like image 52
ds_user Avatar answered Oct 18 '22 18:10

ds_user