{"id":28631462,"url":"https://github.com/dreamrs/r2d3maps","last_synced_at":"2025-07-05T00:36:58.421Z","repository":{"id":95092590,"uuid":"132670512","full_name":"dreamRs/r2d3maps","owner":"dreamRs","description":"r2d3 experiment to draw maps in D3","archived":false,"fork":false,"pushed_at":"2018-09-11T08:00:24.000Z","size":3371,"stargazers_count":75,"open_issues_count":2,"forks_count":2,"subscribers_count":7,"default_branch":"master","last_synced_at":"2025-06-12T13:46:32.748Z","etag":null,"topics":["d3","draw-maps","r"],"latest_commit_sha":null,"homepage":null,"language":"R","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/dreamRs.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2018-05-08T22:09:18.000Z","updated_at":"2024-07-21T18:49:45.000Z","dependencies_parsed_at":"2023-04-22T04:19:39.923Z","dependency_job_id":null,"html_url":"https://github.com/dreamRs/r2d3maps","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/dreamRs/r2d3maps","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dreamRs%2Fr2d3maps","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dreamRs%2Fr2d3maps/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dreamRs%2Fr2d3maps/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dreamRs%2Fr2d3maps/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dreamRs","download_url":"https://codeload.github.com/dreamRs/r2d3maps/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dreamRs%2Fr2d3maps/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":259553047,"owners_count":22875601,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["d3","draw-maps","r"],"created_at":"2025-06-12T13:30:59.256Z","updated_at":"2025-07-05T00:36:58.415Z","avatar_url":"https://github.com/dreamRs.png","language":"R","funding_links":[],"categories":[],"sub_categories":[],"readme":"# r2d3maps\n\n\u003e Fun with [`r2d3`](https://github.com/rstudio/r2d3) and [`geojsonio`](https://github.com/ropensci/geojsonio) : draw D3 maps\n\n[![Travis build status](https://travis-ci.org/dreamRs/r2d3maps.svg?branch=master)](https://travis-ci.org/dreamRs/r2d3maps)\n[![Project Status: WIP – Initial development is in progress, but there has not yet been a stable, usable release suitable for the public.](http://www.repostatus.org/badges/latest/wip.svg)](http://www.repostatus.org/#wip)\n[![lifecycle](https://img.shields.io/badge/lifecycle-experimental-orange.svg)](https://www.tidyverse.org/lifecycle/#experimental)\n\n\n## Installation\n\nYou can install from Github:\n\n```r\nsource(\"https://install-github.me/dreamRs/r2d3maps\")\n\n# or with devtools:\ndevtools::install_github(\"dreamRs/r2d3maps\")\n```\n\n## Basic examples\n\nCreate D3 maps from `sf` objects, try it with NaturalEarth map data from [`rnaturalearth`](https://github.com/ropenscilabs/rnaturalearth) :\n\n```r\nlibrary( r2d3maps )\nlibrary( rnaturalearth )\n\n### Japan\njapan \u003c- ne_states(country = \"japan\", returnclass = \"sf\")\nd3_map(shape = japan) %\u003e%\n  add_labs(title = \"Japan\")\n\n### South America\nsouth_america \u003c- ne_countries(continent = \"south america\", returnclass = \"sf\")\nd3_map(shape = south_america) %\u003e%\n  add_labs(title = \"South America\")\n```\n\n![](img/japan.png)\n![](img/south_america.png)\n\n\n\n## Choropleth map\n\n### Continuous scale\n\nThere is two way to plot a continuous variable: by defining intervals or using a gradient.\n\n\n```r\n# Packages\nlibrary( r2d3maps )\nlibrary( sf )\nlibrary( CARTElette ) # devtools::install_github(\"antuki/CARTElette/CARTElette@RPackage\")\nlibrary( dplyr )\nlibrary( rmapshaper )\n\n# map data\ndept \u003c- loadMap(nivsupra = \"DEP\")      # shapes\ndept \u003c- st_transform(dept, crs = 4326) # changing coordinates\ndept \u003c- ms_simplify(dept)              # simplify shapes\n\n# add population data\ndata(\"pop_fr\", package = \"r2d3maps\")\ndept \u003c- left_join(\n  x = dept,\n  y = pop_fr,\n  by = c(\"DEP\" = \"code_departement\")\n)\n\n# draw map\nd3_map(dept) %\u003e%\n  add_continuous_breaks(var = \"population_totale\", na_color = \"#b8b8b8\") %\u003e%\n  add_legend(d3_format = \".2s\") %\u003e%\n  add_tooltip(value = \"{nom} : {population_totale}\")\n```\n![](img/choropleth_france.png)\n\nWith a gradient:\n\n```r\nd3_map(dept) %\u003e%\n  add_continuous_gradient(var = \"population_totale\") %\u003e%\n  add_legend(d3_format = \".2s\") %\u003e%\n  add_tooltip(value = \"{nom} : {population_totale}\")\n```\n![](img/choropleth_france2.png)\n\n\nYou can also use a diverging colour gradient:\n\n```r\nlibrary( r2d3maps )\nlibrary( rnaturalearth )\nlibrary( dplyr )\n\n# shapes\nireland \u003c- ne_states(country = \"ireland\", returnclass = \"sf\")\n\n# add data\ndata(\"pop_irl\")\nireland \u003c- left_join(\n  x = ireland,\n  y = pop_irl\n)\n\n# draw map\nd3_map(shape = ireland, stroke_col = \"#585858\") %\u003e%\n  add_tooltip(value = \"{woe_name}: {changes_percentage}%\") %\u003e% \n  add_continuous_gradient2(var = \"changes_percentage\", range = c(-9, 9)) %\u003e% \n  add_legend(title = \"Changes in population (2011-2016)\", suffix = \"%\") %\u003e% \n  add_labs(\n    title = \"Ireland\",\n    caption = \"Data from NaturalEarth\"\n  )\n```\n![](img/choropleth_ireland.png)\n\n\n\n### Discrete scale\n\nPlot categorical variables, you can use a color palette or manual values:\n\n```r\n# map data\nfr_dept \u003c- ne_states(country = \"france\", returnclass = \"sf\")\nfr_dept \u003c- fr_dept[fr_dept$type_en %in% \"Metropolitan department\", ]\n\n# firstnames data\ndata(\"prenoms_fr\", package = \"r2d3maps\")\nprenoms_fr_89 \u003c- prenoms_fr %\u003e%\n  filter(annais == 1989, sexe == 2) %\u003e%\n  group_by(preusuel) %\u003e%\n  mutate(n = n()) %\u003e%\n  ungroup() %\u003e%\n  mutate(prenom = if_else(n \u003c 2, \"AUTRE\", preusuel))\n\nfr_dept \u003c- left_join(\n  x = fr_dept,\n  y = prenoms_fr_89,\n  by = \"adm1_code\"\n)\n\n# draw map\nd3_map(shape = fr_dept) %\u003e%\n  add_discrete_scale(\n    var = \"prenom\", palette = \"Set2\",\n    labels_order = c(setdiff(unique(na.omit(fr_dept$prenom)), \"AUTRE\"), \"AUTRE\")\n  ) %\u003e%\n  add_tooltip(value = \"\u003cb\u003e{name}\u003c/b\u003e: {prenom}\", .na = NULL) %\u003e%\n  add_legend(title = \"Prénoms\") %\u003e%\n  add_labs(\n    title = \"Prénoms féminins les plus attribués en 1989\",\n    caption = \"Data: Insee\"\n  )\n```\n![](img/choropleth_france3.png)\n\n\n### Shiny usage\n\nA proxy method allow to update maps without re-generating them:\n\n![](img/d3maps-proxy.gif)\n\nCode:\n\n```r\nlibrary( shiny )\nlibrary( r2d3maps )\nlibrary( rnaturalearth )\nlibrary( dplyr )\n\n\n# data --------------------------------------------------------------------\n\n# shapes\nafrica \u003c- ne_countries(continent = \"Africa\", returnclass = \"sf\")\n\n# drinking water data (from WHO)\ndata(\"water_africa\")\n\n# add data to shapes\nafrica \u003c- left_join(\n  x = africa %\u003e% select(adm0_a3_is, name, geometry),\n  y = water_africa %\u003e% filter(year == 2015),\n  by = c(\"adm0_a3_is\" = \"iso3\")\n)\n\n\n\n# app ---------------------------------------------------------------------\n\nui \u003c- fluidPage(\n  fluidRow(\n    column(\n      width = 10, offset = 1,\n      tags$h2(\"Proxy example:\"),\n      d3Output(outputId = \"mymap\", width = \"600px\", height = \"500px\"),\n      radioButtons(\n        inputId = \"var\",\n        label = \"Indicator:\",\n        choices = list(\n          \"Basic\" = \"national_at_least_basic\",\n          \"Limited\" = \"national_limited_more_than_30_mins\",\n          \"Unimproved\" = \"national_unimproved\",\n          \"Surface water\" = \"national_surface_water\"\n        ),\n        inline = TRUE\n      ),\n      radioButtons(\n        inputId = \"palette\",\n        label = \"Change color palette\",\n        choices = c(\"viridis\", \"magma\", \"plasma\", \"Blues\", \"Greens\", \"Reds\"),\n        inline = TRUE\n      )\n    )\n  )\n)\n\nserver \u003c- function(input, output, session) {\n\n  output$mymap \u003c- renderD3({\n    d3_map(shape = africa) %\u003e%\n      add_continuous_breaks(var = \"national_at_least_basic\") %\u003e%\n      add_tooltip(value = \"\u003cb\u003e{name}\u003c/b\u003e: {national_at_least_basic}%\") %\u003e%\n      add_legend(title = \"Population with at least basic access\", suffix = \"%\") %\u003e%\n      add_labs(title = \"Drinking water in Africa\", caption = \"Data: https://washdata.org/\")\n  })\n\n  title_legend \u003c- list(\n    \"national_at_least_basic\" = \"basic access\",\n    \"national_limited_more_than_30_mins\" = \"limited access\",\n    \"national_unimproved\" = \"unimproved water\",\n    \"national_surface_water\" = \"surface water\"\n  )\n\n  observeEvent(list(input$var, input$palette), {\n    d3_map_proxy(shinyId = \"mymap\", data = africa) %\u003e%\n      update_continuous_breaks(var = input$var, palette = input$palette) %\u003e%\n      update_legend(title = sprintf(\n        \"Population with %s\", title_legend[[input$var]]\n      ), suffix = \"%\")\n  }, ignoreInit = TRUE)\n\n}\n\nshinyApp(ui, server)\n```\n\n\n### Make your own\n\nUse `r2d3map` with an `sf` object to convert it to topojson and use a custom JavaScript script like in `r2d3`:\n\n```r\nlibrary(r2d3maps)\nlibrary(rnaturalearth)\n\n# Data ----\nIndonesia \u003c- ne_states(country = \"Indonesia\", returnclass = \"sf\")\n\n# Map ----\nr2d3map(\n  data = Indonesia,\n  script = \"my_map.js\"\n)\n```\n\nCreate a minimal template with `use_r2d3map`, this will create 3 scripts (R, JS \u0026 CSS) to draw maps:\n\n```r\nuse_r2d3map(\"my_map.R\")\n```\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdreamrs%2Fr2d3maps","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdreamrs%2Fr2d3maps","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdreamrs%2Fr2d3maps/lists"}