|
12 | 12 | #' visible. |
13 | 13 | #' @inheritDotParams guide_colourbar -nbin -raster -ticks -available_aes |
14 | 14 | #' |
| 15 | +#' @inheritSection guide_bins Use with discrete scale |
| 16 | +#' |
15 | 17 | #' @return A guide object |
16 | 18 | #' @export |
17 | 19 | #' |
@@ -54,30 +56,49 @@ guide_colorsteps <- guide_coloursteps |
54 | 56 |
|
55 | 57 | #' @export |
56 | 58 | guide_train.colorsteps <- function(guide, scale, aesthetic = NULL) { |
57 | | - if (guide$even.steps) { |
58 | | - breaks <- scale$get_breaks() |
59 | | - if (length(breaks) == 0 || all(is.na(breaks))) |
| 59 | + breaks <- scale$get_breaks() |
| 60 | + if (guide$even.steps || !is.numeric(breaks)) { |
| 61 | + if (length(breaks) == 0 || all(is.na(breaks))) { |
60 | 62 | return() |
61 | | - limits <- scale$get_limits() |
62 | | - all_breaks <- c(limits[1], breaks, limits[2]) |
63 | | - bin_at <- all_breaks[-1] - diff(all_breaks) / 2 |
| 63 | + } |
| 64 | + if (is.numeric(breaks)) { |
| 65 | + limits <- scale$get_limits() |
| 66 | + all_breaks <- c(limits[1], breaks, limits[2]) |
| 67 | + bin_at <- all_breaks[-1] - diff(all_breaks) / 2 |
| 68 | + } else { |
| 69 | + # If the breaks are not numeric it is used with a discrete scale. We check |
| 70 | + # if the breaks follow the allowed format "(<lower>, <upper>]", and if it |
| 71 | + # does we convert it into bin specs |
| 72 | + bin_at <- breaks |
| 73 | + breaks_num <- as.character(breaks) |
| 74 | + breaks_num <- strsplit(gsub("\\(|\\)|\\[|\\]", "", breaks_num), ",\\s?") |
| 75 | + breaks_num <- as.numeric(unlist(breaks_num)) |
| 76 | + if (anyNA(breaks_num)) { |
| 77 | + abort('Breaks not formatted correctly for a bin legend. Use `(<lower>, <upper>]` format to indicate bins') |
| 78 | + } |
| 79 | + all_breaks <- breaks_num[c(1, seq_along(breaks) * 2)] |
| 80 | + limits <- all_breaks[c(1, length(all_breaks))] |
| 81 | + breaks <- all_breaks[-c(1, length(all_breaks))] |
| 82 | + } |
64 | 83 | ticks <- new_data_frame(setNames(list(scale$map(breaks)), aesthetic %||% scale$aesthetics[1])) |
65 | 84 | ticks$.value <- seq_along(breaks) - 0.5 |
66 | 85 | ticks$.label <- scale$get_labels(breaks) |
67 | 86 | guide$nbin <- length(breaks) + 1 |
68 | 87 | guide$key <- ticks |
69 | 88 | guide$bar <- new_data_frame(list(colour = scale$map(bin_at), value = seq_along(bin_at) - 1), n = length(bin_at)) |
| 89 | + |
70 | 90 | if (guide$reverse) { |
71 | 91 | guide$key <- guide$key[nrow(guide$key):1, ] |
72 | 92 | guide$bar <- guide$bar[nrow(guide$bar):1, ] |
73 | 93 | } |
74 | 94 | guide$hash <- with(guide, digest::digest(list(title, key$.label, bar, name))) |
75 | 95 | } else { |
76 | 96 | guide <- NextMethod() |
| 97 | + limits <- scale$get_limits() |
77 | 98 | } |
78 | 99 | if (guide$show.limits %||% scale$show.limits %||% FALSE) { |
79 | 100 | edges <- rescale(c(0, 1), to = guide$bar$value[c(1, nrow(guide$bar))], from = c(0.5, guide$nbin - 0.5) / guide$nbin) |
80 | | - limits <- scale$get_limits() |
| 101 | + if (guide$reverse) edges <- rev(edges) |
81 | 102 | guide$key <- guide$key[c(NA, seq_len(nrow(guide$key)), NA), , drop = FALSE] |
82 | 103 | guide$key$.value[c(1, nrow(guide$key))] <- edges |
83 | 104 | guide$key$.label[c(1, nrow(guide$key))] <- scale$get_labels(limits) |
|
0 commit comments