Skip to content

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

Merged
merged 4 commits into from
Jan 8, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions R/guide-bins.R
Original file line number Diff line number Diff line change
Expand Up @@ -123,13 +123,25 @@ guide_train.bins <- function(guide, scale, aesthetic = NULL) {
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
# in the key data frame, use either the aesthetic provided as
# argument to this function or, as a fall back, the first in the vector
# of possible aesthetics handled by the scale
aes_column_name <- aesthetic %||% scale$aesthetics[1]

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 {
Copy link
Member

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 when breaks 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?

bin_at <- breaks
breaks <- as.character(breaks)
breaks <- strsplit(gsub("\\(|\\)|\\[|\\]", "", breaks), ",\\s?")
breaks <- as.numeric(unlist(breaks))
if (anyNA(breaks)) {
abort('Breaks not formatted correctly for a bin legend. Use `(<lower>, <upper>]` format to indicate bins')
}
all_breaks <- breaks[c(1, seq_along(bin_at) * 2)]
}
key <- new_data_frame(setNames(list(c(scale$map(bin_at), NA)), aes_column_name))
key$.label <- scale$get_labels(all_breaks)
guide$show.limits <- guide$show.limits %||% scale$show_limits %||% FALSE
Expand Down
30 changes: 23 additions & 7 deletions R/guide-colorsteps.R
Original file line number Diff line number Diff line change
Expand Up @@ -54,30 +54,46 @@ 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 {
Copy link
Member

Choose a reason for hiding this comment

The 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.

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)
Expand Down
1 change: 0 additions & 1 deletion R/scale-.r
Original file line number Diff line number Diff line change
Expand Up @@ -848,7 +848,6 @@ ScaleDiscrete <- ggproto("ScaleDiscrete", Scale,
}

if (is.waive(self$labels)) {
breaks <- self$get_breaks()
if (is.numeric(breaks)) {
# Only format numbers, because on Windows, format messes up encoding
format(breaks, justify = "none")
Expand Down