Search code examples
rhighchartsr-highcharter

Equivalent of highcharts map with {highcharter}


Can someone help me make the equivalent of this card with the highcharter package ?
There are not too many indications in the documentation...

https://www.highcharts.com/maps/demo/data-class-ranges

Many thanks in advance !


Solution

  • You can do this with the helper function built-in to HighChater:

    library(tidyverse)
    library(viridis)
    library(highcharter)
    
    mapdata <- get_data_from_map(download_map_data("countries/us/us-all"))
    set.seed(1234)
    
    data_fake <- mapdata %>% 
      select(code = `hc-a2`) %>% 
      mutate(value = 1e5 * abs(rt(nrow(.), df = 10)))
    
    color_classes(c(0, 100000, 200000, 500000))
    
    hcmap("countries/us/us-all", data = data_fake, value = "value",
          joinBy = c("hc-a2", "code"), name = "Fake data",
          dataLabels = list(enabled = TRUE, format = '{point.name}'),
          borderColor = "#FAFAFA", borderWidth = 0.1,
          tooltip = list(valueDecimals = 2, valuePrefix = "$", valueSuffix = " USD")
          ) %>% 
      hc_colorAxis(
        minColor = "gray",
        maxColor = "yellow",
        dataClasses = color_classes(c(0, 100000, 200000, 500000))
      )
    

    The important part is that color_classes list. In this example I have 3 segments made (0-100k, 100k-200k, and 200k-500k). You can set the colors themselves for those in the color array or by some other means.