From 341476eda25eed15acd96bbd84f59164ea70a669 Mon Sep 17 00:00:00 2001 From: Thomas Lin Pedersen Date: Thu, 22 Apr 2021 13:19:22 +0200 Subject: [PATCH] move last row back down after reversing --- NEWS.md | 3 +++ R/guide-bins.R | 8 +++++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/NEWS.md b/NEWS.md index 279f0df9a8..61e6c9e583 100644 --- a/NEWS.md +++ b/NEWS.md @@ -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) diff --git a/R/guide-bins.R b/R/guide-bins.R index 41f3b3b3fe..2395595ce0 100644 --- a/R/guide-bins.R +++ b/R/guide-bins.R @@ -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 { @@ -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] + } guide$key <- key guide$hash <- with(