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.

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) # 
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). Then the SpatialPolygonsDataFrame into GeoJSON with geojsonio: this will take a long time as the map is very detailed.

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.9 Mb

Now we can use the GeoJSON with e_map_register.

# plot 
e_charts() %>%
  e_map_register("India_small", india_json_small) %>%
  e_map(map = "India_small")