Skip to content
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
Expand Up @@ -62,6 +62,9 @@

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

* Only one warning is issued when asking for too many levels in
`scale_discrete()` (#1674)

# ggplot2 2.1.0

## New features
Expand Down
11 changes: 10 additions & 1 deletion R/scale-.r
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,8 @@ ScaleContinuous <- ggproto("ScaleContinuous", Scale,
ScaleDiscrete <- ggproto("ScaleDiscrete", Scale,
drop = TRUE,
na.value = NA,
n.breaks.cache = NULL,
palette.cache = NULL,

is_discrete = function() TRUE,

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

map = function(self, x, limits = self$get_limits()) {
n <- sum(!is.na(limits))
pal <- self$palette(n)
if (!is.null(self$n.breaks.cache) && self$n.breaks.cache == n) {
pal <- self$palette.cache
} else {
if (!is.null(self$n.breaks.cache)) warning("Cached palette does not match requested", call. = FALSE)
pal <- self$palette(n)
self$palette.cache <- pal
self$n.breaks.cache <- n
}

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