Skip to content

dreamRs/r2d3maps

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

25 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

r2d3maps

Fun with r2d3 and geojsonio : draw D3 maps

Travis build status Project Status: WIP – Initial development is in progress, but there has not yet been a stable, usable release suitable for the public. lifecycle

Installation

You can install from Github:

source("https://install-github.me/dreamRs/r2d3maps")

Examples

Create D3 maps from sf objects:


Try it with NaturalEarth map data from rnaturalearth :

library( r2d3maps )
library( rnaturalearth )

### Japan
japan <- ne_states(country = "japan", returnclass = "sf")
r2d3map(shape = japan) %>%
  add_labs(title = "Japan")


### New Zealand
nz <- ne_states(country = "New Zealand", returnclass = "sf")
nz <- sf::st_crop(nz, xmin = 159.104, ymin = -48.385, xmax = 193.601, ymax = -33.669)
r2d3map(shape = nz) %>%
  add_labs(title = "New Zealand")

library( r2d3maps )
library( rnaturalearth )

### South America
south_america <- ne_countries(continent = "south america", returnclass = "sf")
r2d3map(shape = south_america) %>%
  add_labs(title = "South America")


### France
fr_dept <- ne_states(country = "france", returnclass = "sf")
fr_dept <- fr_dept[fr_dept$type_en %in% "Metropolitan department", ]

r2d3map(shape = fr_dept) %>%
  add_labs(title = "France")

With a shapefile read by sf (data from data.sfgov.org):

library( r2d3maps )
library( sf )

bay_area <- read_sf("dev/bay-area/geo_export_bb694795-f052-42b5-a0a1-01db0b2d41a6.shp")

r2d3map(shape = bay_area) %>%
  add_labs(title = "Bay Area") %>%
  add_tooltip(value = "{po_name}")

Projection

Input data must be in WGS84, but you can use a different projection with D3:

library( r2d3maps )
library( rnaturalearth )

us <- ne_states(country = "united states of america", returnclass = "sf")
us <- filter(us, !name %in% c("Alaska", "Hawaii"))

# Mercator
r2d3map(shape = us) %>%
  add_labs(title = "US (mercator)")

# Albers
r2d3map(shape = us, projection = "Albers") %>%
  add_labs(title = "US (albers)")

To bring back Alaska and Hawaii, see this script (adapted from this one by @hrbrmstr)

Simplify polygons

To draw lot of polygons, consider using rmapshaper by @ateucher:

library( sf )
library( rmapshaper )

# shapefiles from: https://data.london.gov.uk/dataset/statistical-gis-boundary-files-london

london <- read_sf("dev/London-wards-2014/London-wards-2014_ESRI/London_Ward.shp")
london <- st_transform(london, crs = 4326)

london2 <- ms_simplify(london)

# pryr::object_size(london)
# ##> 2.96 MB
# pryr::object_size(london2)
# ##> 532 kB

r2d3map(shape = london2) %>%
  add_tooltip("{NAME}") %>%
  add_labs("London city")

About

r2d3 experiment to draw maps in D3

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published