Skip to content

Commit 16ed4d0

Browse files
authored
refactor show.legend code (#3652)
* refactor show.legend code * code cleanup and simplifiation * eliminate unnecessary return()
1 parent 913e936 commit 16ed4d0

File tree

5 files changed

+58
-81
lines changed

5 files changed

+58
-81
lines changed

R/guide-bins.R

+11-28
Original file line numberDiff line numberDiff line change
@@ -161,38 +161,21 @@ guide_geom.bins <- function(guide, layers, default_mapping) {
161161
guide$geoms <- lapply(layers, function(layer) {
162162
matched <- matched_aes(layer, guide, default_mapping)
163163

164-
if (length(matched) > 0) {
165-
# This layer contributes to the legend
166-
167-
# check if this layer should be included, different behaviour depending on
168-
# if show.legend is a logical or a named logical vector
169-
if (!is.null(names(layer$show.legend))) {
170-
layer$show.legend <- rename_aes(layer$show.legend)
171-
include <- is.na(layer$show.legend[matched]) ||
172-
layer$show.legend[matched]
173-
} else {
174-
include <- is.na(layer$show.legend) || layer$show.legend
175-
}
164+
# check if this layer should be included
165+
include <- include_layer_in_guide(layer, matched)
176166

177-
if (include) {
178-
# Default is to include it
167+
if (!include) {
168+
return(NULL)
169+
}
179170

180-
# Filter out set aesthetics that can't be applied to the legend
181-
n <- vapply(layer$aes_params, length, integer(1))
182-
params <- layer$aes_params[n == 1]
171+
if (length(matched) > 0) {
172+
# Filter out set aesthetics that can't be applied to the legend
173+
n <- vapply(layer$aes_params, length, integer(1))
174+
params <- layer$aes_params[n == 1]
183175

184-
data <- layer$geom$use_defaults(guide$key[matched], params)
185-
} else {
186-
return(NULL)
187-
}
176+
data <- layer$geom$use_defaults(guide$key[matched], params)
188177
} else {
189-
# This layer does not contribute to the legend
190-
if (is.na(layer$show.legend) || !layer$show.legend) {
191-
# Default is to exclude it
192-
return(NULL)
193-
} else {
194-
data <- layer$geom$use_defaults(NULL, layer$aes_params)[rep(1, nrow(guide$key)), ]
195-
}
178+
data <- layer$geom$use_defaults(NULL, layer$aes_params)[rep(1, nrow(guide$key)), ]
196179
}
197180

198181
# override.aes in guide_legend manually changes the geom

R/guide-colorbar.r

+2-15
Original file line numberDiff line numberDiff line change
@@ -249,21 +249,8 @@ guide_geom.colorbar <- function(guide, layers, default_mapping) {
249249
return(NULL)
250250
}
251251

252-
# check if this layer should be included, different behaviour depending on
253-
# if show.legend is a logical or a named logical vector
254-
if (is_named(layer$show.legend)) {
255-
layer$show.legend <- rename_aes(layer$show.legend)
256-
show_legend <- layer$show.legend[matched]
257-
# we cannot use `isTRUE(is.na(show_legend))` here because
258-
# 1. show_legend can be multiple NAs
259-
# 2. isTRUE() was not tolerant for a named TRUE
260-
show_legend <- show_legend[!is.na(show_legend)]
261-
include <- length(show_legend) == 0 || any(show_legend)
262-
} else {
263-
include <- isTRUE(is.na(layer$show.legend)) || isTRUE(layer$show.legend)
264-
}
265-
266-
if (include) {
252+
# check if this layer should be included
253+
if (include_layer_in_guide(layer, matched)) {
267254
layer
268255
} else {
269256
NULL

R/guide-legend.r

+13-34
Original file line numberDiff line numberDiff line change
@@ -246,42 +246,21 @@ guide_geom.legend <- function(guide, layers, default_mapping) {
246246
guide$geoms <- lapply(layers, function(layer) {
247247
matched <- matched_aes(layer, guide, default_mapping)
248248

249+
# check if this layer should be included
250+
include <- include_layer_in_guide(layer, matched)
251+
252+
if (!include) {
253+
return(NULL)
254+
}
255+
249256
if (length(matched) > 0) {
250-
# This layer contributes to the legend
251-
252-
# check if this layer should be included, different behaviour depending on
253-
# if show.legend is a logical or a named logical vector
254-
if (is_named(layer$show.legend)) {
255-
layer$show.legend <- rename_aes(layer$show.legend)
256-
show_legend <- layer$show.legend[matched]
257-
# we cannot use `isTRUE(is.na(show_legend))` here because
258-
# 1. show_legend can be multiple NAs
259-
# 2. isTRUE() was not tolerant for a named TRUE
260-
show_legend <- show_legend[!is.na(show_legend)]
261-
include <- length(show_legend) == 0 || any(show_legend)
262-
} else {
263-
include <- isTRUE(is.na(layer$show.legend)) || isTRUE(layer$show.legend)
264-
}
265-
266-
if (include) {
267-
# Default is to include it
268-
269-
# Filter out set aesthetics that can't be applied to the legend
270-
n <- vapply(layer$aes_params, length, integer(1))
271-
params <- layer$aes_params[n == 1]
272-
273-
data <- layer$geom$use_defaults(guide$key[matched], params)
274-
} else {
275-
return(NULL)
276-
}
257+
# Filter out set aesthetics that can't be applied to the legend
258+
n <- vapply(layer$aes_params, length, integer(1))
259+
params <- layer$aes_params[n == 1]
260+
261+
data <- layer$geom$use_defaults(guide$key[matched], params)
277262
} else {
278-
# This layer does not contribute to the legend
279-
if (isTRUE(is.na(layer$show.legend)) || !isTRUE(layer$show.legend)) {
280-
# Default is to exclude it
281-
return(NULL)
282-
} else {
283-
data <- layer$geom$use_defaults(NULL, layer$aes_params)[rep(1, nrow(guide$key)), ]
284-
}
263+
data <- layer$geom$use_defaults(NULL, layer$aes_params)[rep(1, nrow(guide$key)), ]
285264
}
286265

287266
# override.aes in guide_legend manually changes the geom

R/guides-.r

+32
Original file line numberDiff line numberDiff line change
@@ -363,3 +363,35 @@ matched_aes <- function(layer, guide, defaults) {
363363
matched <- setdiff(matched, names(layer$geom_params))
364364
setdiff(matched, names(layer$aes_params))
365365
}
366+
367+
# This function is used by guides in guide_geom.* to determine whether
368+
# a given layer should be included in the guide
369+
# `matched` is the set of aesthetics that match between the layer and the guide
370+
include_layer_in_guide <- function(layer, matched) {
371+
if (!is.logical(layer$show.legend)) {
372+
warning("`show.legend` must be a logical vector.", call. = FALSE)
373+
layer$show.legend <- FALSE # save back to layer so we don't issue this warning more than once
374+
return(FALSE)
375+
}
376+
377+
if (length(matched) > 0) {
378+
# This layer contributes to the legend
379+
380+
# check if this layer should be included, different behaviour depending on
381+
# if show.legend is a logical or a named logical vector
382+
if (is_named(layer$show.legend)) {
383+
layer$show.legend <- rename_aes(layer$show.legend)
384+
show_legend <- layer$show.legend[matched]
385+
# we cannot use `isTRUE(is.na(show_legend))` here because
386+
# 1. show_legend can be multiple NAs
387+
# 2. isTRUE() was not tolerant for a named TRUE
388+
show_legend <- show_legend[!is.na(show_legend)]
389+
return(length(show_legend) == 0 || any(show_legend))
390+
}
391+
return(all(is.na(layer$show.legend)) || isTRUE(layer$show.legend))
392+
}
393+
394+
# This layer does not contribute to the legend.
395+
# Default is to exclude it, except if it is explicitly turned on
396+
isTRUE(layer$show.legend)
397+
}

R/layer.r

-4
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,6 @@ layer <- function(geom = NULL, stat = NULL,
8080
show.legend <- params$show_guide
8181
params$show_guide <- NULL
8282
}
83-
if (!is.logical(show.legend)) {
84-
warning("`show.legend` must be a logical vector.", call. = FALSE)
85-
show.legend <- FALSE
86-
}
8783

8884
# we validate mapping before data because in geoms and stats
8985
# the mapping is listed before the data argument; this causes

0 commit comments

Comments
 (0)