In addition to the NMR palettes themselves,
paletteNMR
contains scale_*
functions for use
in ggplot2 graphics
Below, two figures with discrete color scales are created using
scale_nmr_color_*
variants and scale_nmr_fill
variants respectively:
iris |>
ggplot(aes(Sepal.Length, Petal.Length)) +
geom_point(aes(colour = Species),
size = rel(4), alpha = 1/1.2) +
scale_nmr_color_categorical(pal = "quad",
set = "C",
reverse = TRUE)
iris |>
ggplot(aes(Sepal.Length, Petal.Length)) +
geom_point(aes(fill = Species),
shape = 21, size = rel(4), alpha = 1/1.2) +
scale_nmr_fill_categorical(pal = "quad",
set = "C",
reverse = TRUE)
Testing cases with more discrete colors (using the
airmiles
dataset from the datasets
pkg)
airmiles |>
as.data.frame() |>
setNames("miles") |>
mutate(yr = time(airmiles) |>
parse_date_time("Y")) |>
mutate(yr_grp = floor_date(yr, "10 years") |>
year() |> factor()) |>
ggplot(aes(yr, miles)) +
geom_point(aes(color = yr_grp),
size = rel(4), alpha = 1/1.2) +
labs(x = NULL, y = "Miles traveled",
color = NULL) +
scale_x_datetime(date_breaks = "10 years",
date_labels = "%Y") +
scale_y_continuous(labels = unit_format(unit = "k", scale = 1e-3)) +
scale_nmr_color_categorical(pal = "quad",
set = "C",
reverse = TRUE)
Continuous values work too!
iris |>
ggplot(aes(Sepal.Length, Petal.Length)) +
geom_point(aes(colour = Petal.Length),
size = rel(4), alpha = 1/1.2) +
scale_nmr_color_sequential(set = "GT6",
discrete = FALSE,
reverse = TRUE) +
guides(color = "none")
iris |>
ggplot(aes(Sepal.Length, Petal.Length)) +
geom_point(aes(fill = Petal.Length),
shape = 21,
size = rel(4), alpha = 1/1.2) +
scale_nmr_fill_sequential(set = "GT6",
discrete = FALSE,
reverse = TRUE) +
guides(fill = "none")
Take care to specify the right arguments. For example, using
scale_nmr_color_sequential
assumes a divergent palette
(i.e. pal = "diverg"
), so it only requires specifying the
set
argument (e.g. set = "GT6"
)
iris |>
ggplot(aes(Sepal.Length, Petal.Length)) +
geom_point(aes(colour = Petal.Length),
size = rel(4), alpha = 1/1.2) +
scale_nmr_color_sequential(pal = "GT6",
discrete = FALSE,
reverse = TRUE) +
guides(color = "none")
#> Error in scale_nmr_color_sequential(pal = "GT6", discrete = FALSE, reverse = TRUE): Do not specify a 'pal'. This function implicitly uses pal = 'nondiverg'. Maybe you meant to specify a 'set'?