Description Usage Arguments Examples
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.
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")
|
map |
A |
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. |
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")
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.