Skip to contents

Introduction

You will find 215 maps in the companion package echarts4r.maps. However, since you may need different kinds of maps i.e.: at the citiy or county level that are not included in this package, you may need to get the detailed map data from third-party source such as gadm.org. This document shows how to make your own GeoJSON file which can be used in e_map_register.

Packages

These are packages to help you building such maps

  • sp: spatial data management package.
  • raster: get spatial data from gadm.org.
  • geojsonio: convert spatial data into json format.
  • rmapshaper: simplify the spatial data.
library(echarts4r)
library(sp)
library(raster)
library(geojsonio)
#> Registered S3 method overwritten by 'geojsonsf':
#>   method        from   
#>   print.geojson geojson
#> 
#> Attaching package: 'geojsonio'
#> The following object is masked from 'package:base':
#> 
#>     pretty
library(rmapshaper)

Example

Get India map data from gadm website, this command need network available, it will download the rds data to the current directory.

india_sp <- raster::getData('GADM', country = 'INDIA', level = 2) 
#> Warning in raster::getData("GADM", country = "INDIA", level = 2): getData will be removed in a future version of raster
#> . Please use the geodata package instead
india_sp |> 
  head() |> 
  knitr::kable()
GID_0 NAME_0 GID_1 NAME_1 NL_NAME_1 GID_2 NAME_2 VARNAME_2 NL_NAME_2 TYPE_2 ENGTYPE_2 CC_2 HASC_2
1 IND India IND.1_1 Andaman and Nicobar NA IND.1.1_1 Nicobar Islands NA NA District District NA IN.AN.NI
2 IND India IND.1_1 Andaman and Nicobar NA IND.1.2_1 North and Middle Andaman NA NA District District NA IN.AN.NM
3 IND India IND.1_1 Andaman and Nicobar NA IND.1.3_1 South Andaman NA NA District District NA IN.AN.SA
214 IND India IND.2_1 Andhra Pradesh NA IND.2.1_1 Anantapur Anantpur, Ananthapur NA District District NA IN.AD.AN
219 IND India IND.2_1 Andhra Pradesh NA IND.2.2_1 Chittoor Chitoor|Chittor NA District District NA IN.AD.CH
220 IND India IND.2_1 Andhra Pradesh NA IND.2.3_1 East Godavari NA NA District District NA IN.AD.EG

Note that you can then combine maps with raster::union(map1, map2) or with the em_maps function from the echarts4r.maps package. Then convert the SpatialPolygonsDataFrame into GeoJSON with geojsonio: this will take a long time as the map is very detailed.

india_json <- geojsonio::geojson_list(india_sp)
print(object.size(india_json), units = "Mb")
#> 59.6 Mb

Therefore we can simplify the map to make it smaller.

india_small <- rmapshaper::ms_simplify(india_sp, keep = 0.05) 
india_json_small <- geojsonio::geojson_list(india_small)
print(object.size(india_json_small), units = "Mb") 
#> 6.8 Mb

The function raster::getData gives each polygon a NAME_* property (where * is the level specified) rather than just name. The tooltip will not display properly unless we use the nameMap to point to that property, by default echarts expects the name property.

Now we can use the GeoJSON with e_map_register.

# plot 
e_charts() |>
  e_map_register("India", india_json_small) |>
  e_map(map = "India", nameProperty = "NAME_2")