-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Make bin guides compatible with discrete scales #3703
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
42e56a9
c470fd3
2ad83bc
352b9f9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,8 @@ | |
#' visible. | ||
#' @inheritDotParams guide_colourbar -nbin -raster -ticks -available_aes | ||
#' | ||
#' @inheritSection guide_bins Use with discrete scale | ||
#' | ||
#' @return A guide object | ||
#' @export | ||
#' | ||
|
@@ -54,30 +56,49 @@ guide_colorsteps <- guide_coloursteps | |
|
||
#' @export | ||
guide_train.colorsteps <- function(guide, scale, aesthetic = NULL) { | ||
if (guide$even.steps) { | ||
breaks <- scale$get_breaks() | ||
if (length(breaks) == 0 || all(is.na(breaks))) | ||
breaks <- scale$get_breaks() | ||
if (guide$even.steps || !is.numeric(breaks)) { | ||
if (length(breaks) == 0 || all(is.na(breaks))) { | ||
return() | ||
limits <- scale$get_limits() | ||
all_breaks <- c(limits[1], breaks, limits[2]) | ||
bin_at <- all_breaks[-1] - diff(all_breaks) / 2 | ||
} | ||
if (is.numeric(breaks)) { | ||
limits <- scale$get_limits() | ||
all_breaks <- c(limits[1], breaks, limits[2]) | ||
bin_at <- all_breaks[-1] - diff(all_breaks) / 2 | ||
} else { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See my previous comment. Add some explanation to the code here as well. |
||
# If the breaks are not numeric it is used with a discrete scale. We check | ||
# if the breaks follow the allowed format "(<lower>, <upper>]", and if it | ||
# does we convert it into bin specs | ||
bin_at <- breaks | ||
breaks_num <- as.character(breaks) | ||
breaks_num <- strsplit(gsub("\\(|\\)|\\[|\\]", "", breaks_num), ",\\s?") | ||
breaks_num <- as.numeric(unlist(breaks_num)) | ||
if (anyNA(breaks_num)) { | ||
abort('Breaks not formatted correctly for a bin legend. Use `(<lower>, <upper>]` format to indicate bins') | ||
} | ||
all_breaks <- breaks_num[c(1, seq_along(breaks) * 2)] | ||
limits <- all_breaks[c(1, length(all_breaks))] | ||
breaks <- all_breaks[-c(1, length(all_breaks))] | ||
} | ||
ticks <- new_data_frame(setNames(list(scale$map(breaks)), aesthetic %||% scale$aesthetics[1])) | ||
ticks$.value <- seq_along(breaks) - 0.5 | ||
ticks$.label <- scale$get_labels(breaks) | ||
guide$nbin <- length(breaks) + 1 | ||
guide$key <- ticks | ||
guide$bar <- new_data_frame(list(colour = scale$map(bin_at), value = seq_along(bin_at) - 1), n = length(bin_at)) | ||
|
||
if (guide$reverse) { | ||
guide$key <- guide$key[nrow(guide$key):1, ] | ||
guide$bar <- guide$bar[nrow(guide$bar):1, ] | ||
} | ||
guide$hash <- with(guide, digest::digest(list(title, key$.label, bar, name))) | ||
} else { | ||
guide <- NextMethod() | ||
limits <- scale$get_limits() | ||
} | ||
if (guide$show.limits %||% scale$show.limits %||% FALSE) { | ||
edges <- rescale(c(0, 1), to = guide$bar$value[c(1, nrow(guide$bar))], from = c(0.5, guide$nbin - 0.5) / guide$nbin) | ||
limits <- scale$get_limits() | ||
if (guide$reverse) edges <- rev(edges) | ||
guide$key <- guide$key[c(NA, seq_len(nrow(guide$key)), NA), , drop = FALSE] | ||
guide$key$.value[c(1, nrow(guide$key))] <- edges | ||
guide$key$.label[c(1, nrow(guide$key))] <- scale$get_labels(limits) | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
else
branch is sufficiently complicated that a comments would be helpful. In particular, explain that whenbreaks
is not numeric it's expected to be in the(<lower>, <upper>]
format. I was very confused by the regex until I figured that out. Also, should this format be mentioned somewhere in the documentation?