Skip to content
This repository was archived by the owner on Jan 30, 2025. It is now read-only.

Commit

Permalink
Add examples
Browse files Browse the repository at this point in the history
  • Loading branch information
nfrerebeau committed Jun 27, 2024
1 parent 2197a76 commit b8f54a4
Show file tree
Hide file tree
Showing 9 changed files with 116 additions and 17 deletions.
3 changes: 3 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ export(info)
export(palette_color_continuous)
export(palette_color_discrete)
export(palette_color_picker)
export(palette_colour_continuous)
export(palette_colour_discrete)
export(palette_colour_picker)
export(palette_line)
export(palette_shape)
export(palette_size_range)
Expand Down
15 changes: 14 additions & 1 deletion R/palettes.R
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ palette_color_picker <- function(scheme, domain = NULL, midpoint = NULL,
fun
}

#' @export
#' @rdname palette_color_picker
palette_colour_picker <- palette_color_picker

#' Color Mapping (continuous)
#'
#' Maps continuous values to an interpolated colors gradient.
Expand All @@ -66,7 +70,7 @@ palette_color_picker <- function(scheme, domain = NULL, midpoint = NULL,
#' A palette [`function`] that when called with a single argument
#' (a [`numeric`] vector of continuous values) returns a [`character`] vector
#' of colors.
#' @note Designed after scales
#' @example inst/examples/ex-palette-continuous.R
#' @family palettes
#' @export
palette_color_continuous <- function(colors = NULL, domain = NULL,
Expand Down Expand Up @@ -105,6 +109,10 @@ palette_color_continuous <- function(colors = NULL, domain = NULL,
}
}

#' @export
#' @rdname palette_color_continuous
palette_colour_continuous <- palette_color_continuous

#' Color Mapping (discrete)
#'
#' Maps categorical values to colors.
Expand All @@ -118,6 +126,7 @@ palette_color_continuous <- function(colors = NULL, domain = NULL,
#' @return
#' A palette [`function`] that when called with a single argument
#' (a vector of categorical values) returns a [`character`] vector of colors.
#' @example inst/examples/ex-palette-discrete.R
#' @family palettes
#' @export
palette_color_discrete <- function(colors = NULL, domain = NULL,
Expand Down Expand Up @@ -156,6 +165,10 @@ palette_color_discrete <- function(colors = NULL, domain = NULL,
}
}

#' @export
#' @rdname palette_color_discrete
palette_colour_discrete <- palette_color_discrete

## Symbol ----------------------------------------------------------------------
#' Symbol Mapping
#'
Expand Down
12 changes: 5 additions & 7 deletions inst/examples/ex-compare.R
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
# Trichromat
pal <- colour("bright")

(deltaE <- compare(pal(5)))
summary(deltaE)
compare(pal(5))

# Deuteranopia
deu <- convert(pal, mode = "deuteranopia")
deu <- change(pal, mode = "deuteranopia")
compare(deu(5))

# Protanopia
pro <- convert(pal, mode = "protanopia")
pro <- change(pal, mode = "protanopia")
compare(pro(5))

# Tritanopia
tri <- convert(pal, mode = "tritanopia")
tri <- change(pal, mode = "tritanopia")
compare(tri(5))

# Achromatopsia
ach <- convert(pal, mode = "achromatopsia")
ach <- change(pal, mode = "achromatopsia")
compare(ach(5))
14 changes: 14 additions & 0 deletions inst/examples/ex-palette-continuous.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
## Visualize a simple DEM model
## Distribution of elevation values
elevation <- hist(volcano)

## Where are breaks?
elevation$breaks

## Build palette functions
BuRd <- color("BuRd")
ramp_BuRd <- palette_color_continuous(colors = BuRd(10))

## Plot image
image(volcano, col = ramp_BuRd(elevation$breaks))
legend("topright", legend = elevation$breaks, fill = ramp_BuRd(elevation$breaks))
16 changes: 16 additions & 0 deletions inst/examples/ex-palette-discrete.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
## Build palette functions
bright <- c(versicolor = "#4477AA", virginica = "#EE6677", setosa = "#228833")
ramp_bright <- palette_color_discrete(colors = bright)

## Plot
plot(
x = iris$Petal.Length,
y = iris$Sepal.Length,
pch = 16,
col = ramp_bright(iris$Species),
xlab = "Petal length",
ylab = "Sepal length",
panel.first = grid(),
las = 1
)
legend("topleft", legend = names(bright), col = bright, pch = 16)
12 changes: 5 additions & 7 deletions man/compare.Rd

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

25 changes: 23 additions & 2 deletions man/palette_color_continuous.Rd

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

26 changes: 26 additions & 0 deletions man/palette_color_discrete.Rd

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

10 changes: 10 additions & 0 deletions man/palette_color_picker.Rd

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

0 comments on commit b8f54a4

Please sign in to comment.