Skip to content

Commit ae19d0b

Browse files
committed
Fixes #1674
Cache palette and n Add news bullet
1 parent b492d55 commit ae19d0b

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

NEWS.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@
6262

6363
* Class of aesthetic mapping is preserved when adding `aes()` objects. (#1624)
6464

65+
* Only one warning is issued when asking for too many levels in
66+
`scale_discrete()` (#1674)
67+
6568
# ggplot2 2.1.0
6669

6770
## New features

R/scale-.r

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,8 @@ ScaleContinuous <- ggproto("ScaleContinuous", Scale,
345345
ScaleDiscrete <- ggproto("ScaleDiscrete", Scale,
346346
drop = TRUE,
347347
na.value = NA,
348+
n.breaks.cache = NULL,
349+
palette.cache = NULL,
348350

349351
is_discrete = function() TRUE,
350352

@@ -359,7 +361,14 @@ ScaleDiscrete <- ggproto("ScaleDiscrete", Scale,
359361

360362
map = function(self, x, limits = self$get_limits()) {
361363
n <- sum(!is.na(limits))
362-
pal <- self$palette(n)
364+
if (!is.null(self$n.breaks.cache) && self$n.breaks.cache == n) {
365+
pal <- self$palette.cache
366+
} else {
367+
if (!is.null(self$n.breaks.cache)) warning("Cached palette does not match requested", call. = FALSE)
368+
pal <- self$palette(n)
369+
self$palette.cache <- pal
370+
self$n.breaks.cache <- n
371+
}
363372

364373
if (is.null(names(pal))) {
365374
pal_match <- pal[match(as.character(x), limits)]

0 commit comments

Comments
 (0)