Skip to content

Ensure all keys in guide_bins(..., reverse = TRUE) #4442

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 1 commit into from
Apr 27, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# ggplot2 (development version)

* Fix a bug in `guide_bins()` where keys would disappear if the guide was
reversed (@thomasp85, #4210)

* Fix a bug in legend justification where justification was lost of the legend
dimensions exceeded the available size (@thomasp85, #3635)

Expand Down
8 changes: 7 additions & 1 deletion R/guide-bins.R
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ guide_train.bins <- function(guide, scale, aesthetic = NULL) {

if (is.numeric(breaks)) {
limits <- scale$get_limits()
breaks <- breaks[!breaks %in% limits]
all_breaks <- c(limits[1], breaks, limits[2])
bin_at <- all_breaks[-1] - diff(all_breaks) / 2
} else {
Expand All @@ -163,7 +164,12 @@ guide_train.bins <- function(guide, scale, aesthetic = NULL) {
key$.label <- scale$get_labels(all_breaks)
guide$show.limits <- guide$show.limits %||% scale$show_limits %||% FALSE

if (guide$reverse) key <- key[nrow(key):1, ]
if (guide$reverse) {
key <- key[rev(seq_len(nrow(key))), ]
# Move last row back to last
aesthetics <- setdiff(names(key), ".label")
key[, aesthetics] <- key[c(seq_len(nrow(key))[-1], 1), aesthetics]
}
Comment on lines +167 to +172
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we reverse the bins before appending NA at the bottom?

  bin_at <- scale$map(bin_at)
  if (guide$reverse) {
    bin_at <- rev(bin_at)
  }
  key <- new_data_frame(setNames(list(c(bin_at, NA)), aes_column_name))
  key$.label <- scale$get_labels(all_breaks)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The issue is that bin_at and the derivatives gets calculated at two different branches so we would have to duplicate the logic there

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bin_at and the derivatives gets calculated at two different branches

I meant something like this, not inside the if branches.

master...yutannihilation:poc/pr-4442

Since I needed some minutes to figure out what the added code does, I was hoping to make the logic simple, but it seems my attempt is no shorter than this pull request... Please ignore my comments.


guide$key <- key
guide$hash <- with(
Expand Down