1

I've been trying to make a choropleth map with hcmap from highcharter package; I obtained the polygons from my own shapefile because it's a map that is not on the list of highmap's collection.

To do so, first I managed to transform my shapefile to a GeoJson file, as described here: https://blog.exploratory.io/creating-geojson-out-of-shapefile-in-r-40bc0005857d

Later I managed to draw the map using the package geosonio as described here: http://jkunst.com/highcharter/highmaps.html#geojsonio-package

However, I can't figure out how to merge a dataframe with values into the polygons drawn in my map. All the examples availables are merging to mapdata that is in a data.frame format, which I lose when transforming to GeoJson.

Here's my code so far:

library(rgdal)
library(geojsonio)
library(highcharter)

#Get map from shapefile
Mymap <- readOGR(dsn="Mymap", "Mymap", verbose = FALSE) %>%  
                 spTransform(CRS("+proj=longlat +ellps=GRS80 +datum=WGS84"))

#Transform to geoJson
MymapJSON <- geojson_json(Mymap)

#Use geojsonio to make data compatible with hcmap
Myhcmap <- jsonlite::fromJSON(MymapJSON, simplifyVector = FALSE)
Myhcmap<- geojsonio::as.json(Myhcmap)

#Draw map:

highchart(type = "map") %>%
   hc_add_series(mapData = Myhcmap, showInLegend = T)

Result:

enter image description here

¿How can I put additional data into the GeoJson so I can draw a choropleth?

David Jorquera
  • 2,046
  • 12
  • 35
  • Hi. Here is a demo with a piece of official Highcharts Europe map. You can see the structure of this map and, at the bottom, see how I connected data using hc-key code. You can also use series.joinBy property and connect, for example, by names. Now you can check if you have correctly created map and prepared data. https://jsfiddle.net/BlackLabel/uohn5k6b – raf18seb Jun 19 '19 at 08:02
  • Is your question about making a map with `highcharter`, or about merging data between GeoJSON and shapefiles? – SymbolixAU Jul 08 '19 at 22:39
  • @SymbolixAU it was more about the latter. – David Jorquera Jul 15 '19 at 15:20

1 Answers1

2

I finally got to a solution by myself some time ago, it's was fairly simple but since it's not well documented how to add data to the GeoJSON, I will show it here:

#Work with the map until this step: 
Myhcmap <- jsonlite::fromJSON(MymapJSON, simplifyVector = FALSE)

#This part was unnecessary:
#Myhcmap<- geojsonio::as.json(Myhcmap)

#Then, write your map like this:

highchart() %>%
hc_add_series_map(Myhcmap, df, value = "value", joinBy = "ID")

Where:

dfis the dataframe you want to append

value is the column name of the data you want to color your map by

joinBy is the joining key variable

David Jorquera
  • 2,046
  • 12
  • 35