-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathramp.R
93 lines (75 loc) · 2.3 KB
/
ramp.R
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# a linear colour ramp for continuous input
color_gradient <- function(palette) {
UseMethod("color_gradient")
}
#' @export
color_gradient.character <- function(palette) {
tidyassert::assert(is_rgba_color(palette))
color_gradient(function(n) palette)
}
#' @export
color_gradient.function <- function(palette) {
# function is a ramp built with scales::colour_ramp
if (isTRUE(attr(palette, "safe_palette_func"))) return(palette)
# assume palette takes a length param
function(x) {
colors <- suppressWarnings(palette(256))
ramp <- scales::colour_ramp(colors[!is.na(colors)])
ramp(x)
}
}
# a linear colour ramp for discrete input
color_categories <- function(palette) {
UseMethod("color_categories")
}
#' @export
color_categories.character <- function(palette) {
tidyassert::assert(is_rgba_color(palette))
color_categories(scales::manual_pal(palette))
}
#' @export
color_categories.function <- function(palette) {
rescale <- function(x, levels) scales::rescale(match(x, levels))
# function is a ramp built with scales::colour_ramp
if (isTRUE(attr(palette, "safe_palette_func"))) {
ramp <- function(x) {
levels <- get_levels(x)
palette(rescale(x, levels))
}
return(ramp)
}
# assume palette takes a length param
function(x) {
levels <- get_levels(x)
colors <- palette(length(levels))
ramp <- scales::colour_ramp(colors[!is.na(colors)])
ramp(rescale(x, levels))
}
}
# a linear numeric ramp for continous input
number_gradient <- function(seq) {
UseMethod("number_gradient")
}
#' @export
number_gradient.numeric <- function(seq) {
tidyassert::assert(all_finite(seq) && length(seq) >= 2)
stats::approxfun(scales::rescale(seq_along(seq)), seq)
}
#' @export
number_gradient.integer <- number_gradient.numeric
# a linear numeric ramp for discrete input
number_categories <- function(seq) UseMethod("number_categories")
#' @export
number_categories.numeric <- function(seq) {
tidyassert::assert(all_finite(seq) && length(seq) >= 2)
seq_pal <- scales::manual_pal(seq)
function(x) {
levels <- get_levels(x)
# pull at least 2 values
values <- seq_pal(max(length(levels), 2))
ramp <- number_gradient(values[!is.na(values)])
ramp(scales::rescale(match(x, levels)))
}
}
#' @export
number_categories.integer <- number_categories.numeric