-
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
42e56a9
Ensure that guide_bin and guide_colourbar can be used with discrete s…
thomasp85 c470fd3
remove browser()
thomasp85 2ad83bc
Better docs and comments
thomasp85 352b9f9
make contour_filled create an ordered factor (should result in a bett…
thomasp85 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 { | ||
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. |
||
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) | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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?