discrete-scale: Add discrete scale to a map

Description Usage Arguments Examples

Description

Display a discrete value on a map. add_discrete_scale is for using a color palette, add_discrete_scale2 is to attach custom colors to data levels.

Usage

1
2
3
4
add_discrete_scale(map, var, palette = "viridis", direction = 1,
  na_color = "#D8D8D8", labels_order = NULL)

add_discrete_scale2(map, var, values, na_color = "#D8D8D8")

Arguments

map

A r2d3map htmlwidget object.

var

Variable to map

palette

Color palette, you can use Viridis or Brewer color palette.

direction

Sets the order of colors in the scale. If 1, the default, colors are ordered from darkest to lightest. If -1, the order of colors is reversed.

na_color

Color to use for missing values.

labels_order

Vector of unique values for changing order of labels in legend.

values

Named list mapping data values to colors. It's recommended to use Hex color code without alpha, e.g. # followed by 6 chars [0-9a-f].

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
library( r2d3maps )
library( rnaturalearth )

# data
japan <- ne_states(country = "japan", returnclass = "sf")

# Japan's regions
d3_map(shape = japan) %>%
  add_discrete_scale(var = "region")

# different color palette
d3_map(shape = japan) %>%
  add_discrete_scale(var = "region", palette = "Set2")

# custom colors
d3_map(shape = japan) %>%
  add_discrete_scale2(
    var = "region",
    values = list(
      "Chugoku" = "#000080",
      "Kyushu" = "#6B8E23",
      "Shikoku" = "#DDA0DD",
      "Chubu" = "#4169E1",
      "Kinki" = "#2E8B57",
      "Hokkaido" = "#4682B4",
      "Kanto" = "#FFA07A",
      "Tohoku" = "#F08080",
      "Okinawa" = "red"
    ),
    na_color = "#000"
  )

# with legend
d3_map(shape = japan) %>%
  add_discrete_scale(var = "region", palette = "Set1") %>%
  add_legend(title = "County")

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