Skip to content

Commit

Permalink
minor fix + readme
Browse files Browse the repository at this point in the history
  • Loading branch information
pvictor committed May 31, 2018
1 parent d058b22 commit d91ffe5
Show file tree
Hide file tree
Showing 10 changed files with 118 additions and 95 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Package: r2d3maps
Version: 0.0.0.9100
Version: 0.0.0.9200
Title: Create Maps with D3
Description: Tools to create interactive maps in D3 with 'r2d3'.
Authors@R: c(
Expand Down
2 changes: 1 addition & 1 deletion R/scale-continuous.R
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ add_continuous_gradient2 <- function(map, var, low = muted("red"), mid = "white"
na_color = na_color,
legend_label = append(
x = range(var_, na.rm = TRUE),
values = diff(range(var_, na.rm = TRUE))/2,
values = diff(abs(range(var_, na.rm = TRUE)))/2,
after = 1
),
gradient_id = paste0("gradient-", sample.int(1e9, 1))
Expand Down
8 changes: 7 additions & 1 deletion R/scale-discrete.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#' If 1, the default, colors are ordered from darkest to lightest.
#' If -1, the order of colors is reversed.
#' @param na_color Color to use for missing values.
#' @param labels_order Vector of unique values for changing order of labels in legend.
#'
#' @export
#'
Expand Down Expand Up @@ -56,13 +57,18 @@
#' add_discrete_scale(var = "region", palette = "Set1") %>%
#' add_legend(title = "County")
#'
add_discrete_scale <- function(map, var, palette = "viridis", direction = 1, na_color = "#D8D8D8") {
add_discrete_scale <- function(map, var, palette = "viridis",
direction = 1, na_color = "#D8D8D8",
labels_order = NULL) {
if (is.null(map$x$options$data))
stop("No data !", call. = FALSE)
var_ <- map$x$options$data[[var]]
if (is.null(var_))
stop("Invalid variable supplied to continuous scale !", call. = FALSE)
values <- if (is.factor(var_)) levels(var_) else unique(var_[!is.na(var_)])
if (!is.null(labels_order)) {
values <- values[match(values, labels_order)]
}
na <- anyNA(var_)
n <- length(values)
if (palette %in% c("viridis", "magma", "plasma", "inferno", "cividis")) {
Expand Down
187 changes: 99 additions & 88 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,14 @@ You can install from Github:

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

## Examples

Create D3 maps from `sf` objects:

![](img/africa_water_access.png)

# or with devtools:
devtools::install_github("dreamRs/r2d3maps")
```

<br>
## Basic examples

Try it with NaturalEarth map data from [`rnaturalearth`](https://github.com/ropenscilabs/rnaturalearth) :
Create D3 maps from `sf` objects, try it with NaturalEarth map data from [`rnaturalearth`](https://github.com/ropenscilabs/rnaturalearth) :

```r
library( r2d3maps )
Expand All @@ -35,116 +31,131 @@ japan <- ne_states(country = "japan", returnclass = "sf")
d3_map(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)
d3_map(shape = nz) %>%
add_labs(title = "New Zealand")
```

![](img/japan.png)
![](img/new_zealand.png)



```r
library( r2d3maps )
library( rnaturalearth )

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

![](img/japan.png)
![](img/south_america.png)

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

d3_map(shape = fr_dept) %>%
add_labs(title = "France")
```

![](img/south_america.png)
![](img/france.png)
## Choropleth map

### Continuous scale

There is two way to plot a continuous variable: by defining intervals or using a gradient.

With a shapefile read by `sf` (data from [data.sfgov.org](https://data.sfgov.org/Geographic-Locations-and-Boundaries/Bay-Area-ZIP-Codes/u5j3-svi6)):

```r
# Packages
library( r2d3maps )
library( sf )
library( CARTElette ) # devtools::install_github("antuki/CARTElette/CARTElette@RPackage")
library( dplyr )
library( rmapshaper )

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

d3_map(shape = bay_area) %>%
add_labs(title = "Bay Area") %>%
add_tooltip(value = "{po_name}")
# map data
dept <- loadMap(nivsupra = "DEP") # shapes
dept <- st_transform(dept, crs = 4326) # changing coordinates
dept <- ms_simplify(dept) # simplify shapes

# add population data
data("pop_fr", package = "r2d3maps")
dept <- left_join(
x = dept,
y = pop_fr,
by = c("DEP" = "code_departement")
)

# draw map
d3_map(dept) %>%
add_continuous_breaks(var = "population_totale", na_color = "#b8b8b8") %>%
add_legend(d3_format = ".2s") %>%
add_tooltip(value = "{nom} : {population_totale}")
```
![](img/choropleth_france.png)

![](img/bay_area.png)

With a gradient:

```r
d3_map(dept) %>%
add_continuous_gradient(var = "population_totale") %>%
add_legend(d3_format = ".2s") %>%
add_tooltip(value = "{nom} : {population_totale}")
```
![](img/choropleth_france2.png)

## Projection

Input data must be in WGS84, but you can use a different projection with D3:
You can also use a diverging colour gradient:

```r
library( r2d3maps )
library( rnaturalearth )

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

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

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

# shapes
ireland <- ne_states(country = "ireland", returnclass = "sf")

# add data
data("pop_irl")
ireland <- left_join(
x = ireland,
y = pop_irl
)

# draw map
d3_map(shape = ireland, stroke_col = "#585858") %>%
add_tooltip(value = "{woe_name}: {changes_percentage}%") %>%
add_continuous_gradient2(var = "changes_percentage", range = c(-9, 9)) %>%
add_legend(title = "Changes in population (2011-2016)", suffix = "%") %>%
add_labs(
title = "Ireland",
caption = "Data from NaturalEarth"
)
```

![](img/us_mercator.png)
![](img/us_albers.png)


To bring back Alaska and Hawaii, see this [script](https://github.com/dreamRs/r2d3maps/blob/master/dev/us.R) (adapted from this [one](https://rud.is/b/2014/11/16/moving-the-earth-well-alaska-hawaii-with-r/) by [@hrbrmstr](https://github.com/hrbrmstr))


![](img/usaea_albers.png)

![](img/choropleth_ireland.png)


## Simplify polygons

To draw lot of polygons, consider using [`rmapshaper`](https://github.com/ateucher/rmapshaper) by [@ateucher](https://github.com/ateucher):
### Discrete scale

Plot categorical variables, you can use a color palette or manual values:

```r
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
# map data
fr_dept <- ne_states(country = "france", returnclass = "sf")
fr_dept <- fr_dept[fr_dept$type_en %in% "Metropolitan department", ]

d3_map(shape = london2) %>%
add_tooltip("{NAME}") %>%
add_labs("London city")
# firstnames data
data("prenoms_fr", package = "r2d3maps")
prenoms_fr_89 <- prenoms_fr %>%
filter(annais == 1989, sexe == 2) %>%
group_by(preusuel) %>%
mutate(n = n()) %>%
ungroup() %>%
mutate(prenom = if_else(n < 2, "AUTRE", preusuel))

fr_dept <- left_join(
x = fr_dept,
y = prenoms_fr_89,
by = "adm1_code"
)

# draw map
d3_map(shape = fr_dept) %>%
add_discrete_scale(
var = "prenom", palette = "Set2",
labels_order = c(setdiff(unique(na.omit(fr_dept$prenom)), "AUTRE"), "AUTRE")
) %>%
add_tooltip(value = "<b>{name}</b>: {prenom}", .na = NULL) %>%
add_legend(title = "Prénoms") %>%
add_labs(
title = "Prénoms féminins les plus attribués en 1989",
caption = "Data: Insee"
)
```

![](img/london.gif)


![](img/choropleth_france3.png)

10 changes: 7 additions & 3 deletions dev/france_firstnames_map.R
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,13 @@ fr_dept <- left_join(
)



# map
d3_map(shape = fr_dept) %>%
add_discrete_scale(var = "prenom", palette = "viridis") %>%
add_discrete_scale(
var = "prenom", palette = "Set2",
labels_order = c(setdiff(unique(na.omit(fr_dept$prenom)), "AUTRE"), "AUTRE")
) %>%
add_tooltip(value = "<b>{name}</b>: {prenom}", .na = NULL) %>%
add_legend(title = "Prénoms") %>%
add_labs(
Expand All @@ -100,8 +104,8 @@ d3_map(shape = fr_dept) %>%
"JULIE" = "cornflowerblue",
"LAURA" = "gold",
"MARION" = "mediumpurple",
"AUTRE" = "grey",
"AURÉLIE" = "forestgreen"
"AURÉLIE" = "forestgreen",
"AUTRE" = "grey"
)
) %>%
add_tooltip(value = "<b>{name}</b>: {prenom}", .na = NULL) %>%
Expand Down
Binary file added img/choropleth_france.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/choropleth_france2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/choropleth_france3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/choropleth_ireland.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 3 additions & 1 deletion man/discrete-scale.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit d91ffe5

Please sign in to comment.