gradient-scale: Add gradient scale to a map

Description Usage Arguments Examples

Description

Create a two colour gradient (low-high) or a diverging colour gradient (low-mid-high) based on a continuous variable.

Usage

1
2
3
4
5
add_continuous_gradient(map, var, low = "#132B43", high = "#56B1F7",
  range = NULL, na_color = "#b8b8b8")

add_continuous_gradient2(map, var, low = muted("red"), mid = "white",
  high = muted("blue"), range = NULL, na_color = "#b8b8b8")

Arguments

map

A d3_map htmlwidget object.

var

Variable to map.

low, high

Colours for low and high ends of the gradient.

range

A length two vector to force range of data.

na_color

Color to use for missing value(s).

mid

Colour for mid point.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
library( r2d3maps )
library( rnaturalearth )

# shapes
africa <- ne_countries(continent = "Africa", returnclass = "sf")

# drinking water data
data("water_africa")
wa2015 <- water_africa[water_africa$year == "2015", ]

# merge with sf object
africa <- merge(
  x = africa[, c("adm0_a3_is", "name", "geometry")],
  y = wa2015[, c("iso3", "national_at_least_basic")],
  by.x = "adm0_a3_is", by.y = "iso3"
)

africa$national_at_least_basic <- round(africa$national_at_least_basic)

# two colour gradient
d3_map(shape = africa) %>%
  add_continuous_gradient(
    var = "national_at_least_basic",
    range = c(0, 100)
  ) %>%
  add_tooltip(value = "<b>{name}</b>: {national_at_least_basic}%") %>%
  add_legend(title = "Population with at least basic access", suffix = "%") %>%
  add_labs(title = "Drinking water in Africa", caption = "Data: https://washdata.org/")


# three colour gradient
d3_map(shape = africa, stroke_col = "#585858") %>%
  add_continuous_gradient2(
    var = "national_at_least_basic",
    range = c(0, 100)
  ) %>%
  add_tooltip(value = "<b>{name}</b>: {national_at_least_basic}%") %>%
  add_legend(title = "Population with at least basic access", suffix = "%") %>%
  add_labs(title = "Drinking water in Africa", caption = "Data: https://washdata.org/")

dreamRs/r2d3maps documentation built on May 25, 2019, 8:17 a.m.