By default the
globe_choropleth function will plot
data against an internal database of country polygons. Below use a globe4r dataset to plot the percentage of agricultural land available per country.
data(agriland) create_globe(height = "100vh") %>% globe_choropleth( coords( country = country_code, altitude = percent, cap_color = percent ), data = agriland ) %>% scale_choropleth_cap_color() %>% scale_choropleth_altitude(0.06, 0.1)
However one is not limited to use those polygons. The latter are defined by GeoJSON which can easily be obtained in R.
rasterpackage to fetch the desired map (shapefile).
rmapshaperto simplify the map as it will make it load much faster in the browser.
geojsonioto convert the file to GeoJSON
All of the packages above are available on CRAN.
# get shapefile india_sp <- raster::getData('GADM', country = 'INDIA', level = 1) # optionally reduce its size (strongly recommended) india_small <- rmapshaper::ms_simplify(india_sp, keep = 0.05) # convert to JSON india_geojson <- geojsonio::geojson_list(india_small)
Now we can build mock data to plot against our mock GeoJON. We can fetch the name of each polygon using the
level formula, or
NAME_1 in our case, this can be found under
regions <- india_geojson$features %>% purrr::map("properties") %>% purrr::map("NAME_1") %>% unlist() mock_data <- data.frame( NAME_1 = regions, value = runif(length(regions), 1, 100) ) mock_data %>% head() %>% knitr::kable()
|Andaman and Nicobar||31.231427|
|Jammu and Kashmir||3.341006|
Note that the we also use
NAME_1 in our data, this will be used internally to match data to their respective polygons.