Highmaps

Basics

The easiest way to chart a map with highcharter is using hcmap function. Select a map (a url) from the highmaps collection https://code.highcharts.com/mapdata/. and use the url as a map in hcmap function. This will download the map and create a object using the info as a mapData argument.

hcmap("countries/nz/nz-all")
hcmap("custom/usa-and-canada", showInLegend = FALSE)
hcmap("countries/us/us-ca-all") %>%
  hc_title(text = "California")

Note: The copyright information is added to the chart credits by default, but please be aware that you will have to display this information somewhere else if you choose to disable chart credits. Copyright information for each map can be found as properties in the GeoJSON and Javascript files.

Choropleths

What about add data to get a choropleth? Every map downloaded from the highcharts maps collection have keys to join data. There are 2 functions to help to know what are the regions coded to know how to join the map and data:

  • download_map_data: Download the geojson data from the highcharts collection.
  • get_data_from_map: Get the properties for each region in the map, as the keys from the map data.
library(dplyr)

mapdata <- get_data_from_map(download_map_data("countries/us/us-all"))
glimpse(mapdata)
## Observations: 52
## Variables: 19
## $ hc-group    <chr> "admin1", "admin1", "admin1", "admin1", "admin1", ...
## $ hc-middle-x <dbl> 0.36, 0.56, 0.51, 0.47, 0.41, 0.43, 0.71, 0.46, 0....
## $ hc-middle-y <dbl> 0.47, 0.52, 0.67, 0.52, 0.38, 0.40, 0.67, 0.38, 0....
## $ hc-key      <chr> "us-ma", "us-wa", "us-ca", "us-or", "us-wi", "us-m...
## $ hc-a2       <chr> "MA", "WA", "CA", "OR", "WI", "ME", "MI", "NV", "N...
## $ labelrank   <chr> "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", ...
## $ hasc        <chr> "US.MA", "US.WA", "US.CA", "US.OR", "US.WI", "US.M...
## $ woe-id      <chr> "2347580", "2347606", "2347563", "2347596", "23476...
## $ state-fips  <chr> "25", "53", "6", "41", "55", "23", "26", "32", "35...
## $ fips        <chr> "US25", "US53", "US06", "US41", "US55", "US23", "U...
## $ postal-code <chr> "MA", "WA", "CA", "OR", "WI", "ME", "MI", "NV", "N...
## $ name        <chr> "Massachusetts", "Washington", "California", "Oreg...
## $ country     <chr> "United States of America", "United States of Amer...
## $ region      <chr> "Northeast", "West", "West", "West", "Midwest", "N...
## $ longitude   <chr> "-71.99930000000001", "-120.361", "-119.591", "-12...
## $ woe-name    <chr> "Massachusetts", "Washington", "California", "Oreg...
## $ latitude    <chr> "42.3739", "47.4865", "36.7496", "43.8333", "44.37...
## $ woe-label   <chr> "Massachusetts, US, United States", "Washington, U...
## $ type        <chr> "State", "State", "State", "State", "State", "Stat...

set.seed(1234)

data_fake <- mapdata %>% 
  select(code = `hc-a2`) %>% 
  mutate(value = 1e5 * abs(rt(nrow(.), df = 10)))

glimpse(data_fake)
## Observations: 52
## Variables: 2
## $ code  <chr> "MA", "WA", "CA", "OR", "WI", "ME", "MI", "NV", "NM", "C...
## $ value <dbl> 119426.518, 255662.317, 3668.575, 122246.930, 79283.064,...

If we compare this 2 data frames the hc-key is same code that code. So we’ll use these columns as keys:

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

Adding More Data

cities <- data_frame(
  name = c("London", "Birmingham", "Glasgow", "Liverpool"),
  lat = c(51.507222, 52.483056,  55.858, 53.4),
  lon = c(-0.1275, -1.893611, -4.259, -3),
  z = c(1, 2, 3, 2)
  )

glimpse(cities)
## Observations: 4
## Variables: 4
## $ name <chr> "London", "Birmingham", "Glasgow", "Liverpool"
## $ lat  <dbl> 51.50722, 52.48306, 55.85800, 53.40000
## $ lon  <dbl> -0.127500, -1.893611, -4.259000, -3.000000
## $ z    <dbl> 1, 2, 3, 2

hcmap("countries/gb/gb-all", showInLegend = FALSE) %>% 
  hc_add_series(data = cities, type = "mapbubble", name = "Cities", maxSize = '10%') %>% 
  hc_mapNavigation(enabled = TRUE) 

Advanced Maps

Sometime you will need to use other source of maps. You can load geojson as a list. For example a good resource for geojson countries is https://github.com/johan/world.geo.json. Just ensure use geojson = TRUE to internally transform the list in a format usable for highcharts.

Let’s download some geojson file and make a map.

getContent <- function(url) {
  library(httr)
  content(GET(url))
}

world <- getContent("https://raw.githubusercontent.com/johan/world.geo.json/master/countries.geo.json")
# is text
world <- jsonlite::fromJSON(world, simplifyVector = FALSE)

# http://cedeusdata.geosteiniger.cl/layers/geonode:mundo_corrientes_maritimas
marine <- getContent("http://cedeusdata.geosteiniger.cl/geoserver/wfs?srsName=EPSG%3A4326&typename=geonode%3Amundo_corrientes_maritimas&outputFormat=json&version=1.0.0&service=WFS&request=GetFeature")
# marine <- geojsonio::as.json(marine)


# http://cedeusdata.geosteiniger.cl/layers/geonode:mundo_limites_placas
plates <- getContent("http://cedeusdata.geosteiniger.cl/geoserver/wfs?srsName=EPSG%3A4326&typename=geonode%3Amundo_limites_placas&outputFormat=json&version=1.0.0&service=WFS&request=GetFeature")
# plates <- geojsonio::as.json(plates)

# http://cedeusdata.geosteiniger.cl/layers/geonode:mundo_volcanes
volcano <- getContent("http://cedeusdata.geosteiniger.cl/geoserver/wfs?srsName=EPSG%3A4326&typename=geonode%3Amundo_volcanes&outputFormat=json&version=1.0.0&service=WFS&request=GetFeature")
# volcano <- geojsonio::as.json(volcano)

The data is ready. Remember you can keep using the rest of the API to customize your map.

highchart(type = "map") %>%
  hc_chart(backgroundColor = "#161C20") %>% 
  hc_add_series(mapData = world, showInLegend = FALSE, nullColor = "#424242",
                borderWidth = 0) %>%
  hc_add_series(data = marine, type = "mapline", geojson = TRUE,
                color = "#2980b9", name = "Marine currents",
                tooltip = list(pointFormat = "{point.properties.NOMBRE}")) %>%
  hc_add_series(data = plates, type = "mapline", lineWidth = 2, zIndex = -1, geojson = TRUE,
                color = "#d35400", name = "Plates",
                tooltip = list(pointFormat = "{point.properties.TIPO}")) %>%
  hc_add_series(data = volcano, type = "mappoint", color = hex_to_rgba("#f1c40f", 0.4),
                geojson = TRUE, name = "Volcanos",
                tooltip = list(pointFormat = "{point.properties.NOMBRE}"),
                marker = list(lineWidth = 0, radius = 2))