Skip to content

Commit f13d9ab

Browse files
authored
Fix expression labels in guide_coloursteps() and guide_bins() (#6007)
* cast expressions to lists * additional workaround for expressions * linearise discrete `get_labels()` logic * scales cast expressions as lists * alleviate wrangling label expressions * fix deprecated argument name * add news bullets * revert even.steps logic
1 parent f9b9703 commit f13d9ab

File tree

5 files changed

+33
-42
lines changed

5 files changed

+33
-42
lines changed

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# ggplot2 (development version)
22

3+
* Binned guides now accept expressions as labels (@teunbrand, #6005)
4+
* (internal) `Scale$get_labels()` format expressions as lists.
35
* In non-orthogonal coordinate systems (`coord_sf()`, `coord_polar()` and
46
`coord_radial()`), using 'AsIs' variables escape transformation when
57
both `x` and `y` is an 'AsIs' variable (@teunbrand, #6205).

R/guide-.R

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -225,13 +225,8 @@ Guide <- ggproto(
225225

226226
mapped <- scale$map(breaks)
227227
labels <- scale$get_labels(breaks)
228-
# {vctrs} doesn't play nice with expressions, convert to list.
229-
# see also https://github.com/r-lib/vctrs/issues/559
230-
if (is.expression(labels)) {
231-
labels <- as.list(labels)
232-
}
233228

234-
key <- data_frame(mapped, .name_repair = ~ aesthetic)
229+
key <- data_frame(!!aesthetic := mapped)
235230
key$.value <- breaks
236231
key$.label <- labels
237232

R/guide-axis-theta.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ GuideAxisTheta <- ggproto(
110110
# labels of these positions
111111
ends_apart <- (key$theta[n] - key$theta[1]) %% (2 * pi)
112112
if (n > 0 && ends_apart < 0.05 && !is.null(key$.label)) {
113-
if (is.expression(key$.label)) {
113+
if (is.expression(key$.label[[1]])) {
114114
combined <- substitute(
115115
paste(a, "/", b),
116116
list(a = key$.label[[1]], b = key$.label[[n]])

R/guide-bins.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ GuideBins <- ggproto(
161161
key$.show <- NA
162162

163163
labels <- scale$get_labels(breaks)
164-
if (is.character(scale$labels) || is.numeric(scale$labels)) {
164+
if (is.character(scale$labels) || is.numeric(scale$labels) || is.expression(scale$labels)) {
165165
limit_lab <- c(NA, NA)
166166
} else {
167167
limit_lab <- scale$get_labels(limits)
@@ -265,7 +265,7 @@ GuideBins <- ggproto(
265265

266266
list(labels = flip_element_grob(
267267
elements$text,
268-
label = key$.label,
268+
label = validate_labels(key$.label),
269269
x = unit(key$.value, "npc"),
270270
margin_x = FALSE,
271271
margin_y = TRUE,

R/scale-.R

Lines changed: 27 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -861,12 +861,9 @@ ScaleContinuous <- ggproto("ScaleContinuous", Scale,
861861
labels[lengths(labels) == 0] <- ""
862862
# Make sure each element is scalar
863863
labels <- lapply(labels, `[`, 1)
864-
865-
if (any(vapply(labels, is.language, logical(1)))) {
866-
labels <- inject(expression(!!!labels))
867-
} else {
868-
labels <- unlist(labels)
869-
}
864+
}
865+
if (is.expression(labels)) {
866+
labels <- as.list(labels)
870867
}
871868

872869
labels
@@ -1074,48 +1071,42 @@ ScaleDiscrete <- ggproto("ScaleDiscrete", Scale,
10741071
return(NULL)
10751072
}
10761073

1077-
if (is.null(self$labels)) {
1074+
labels <- self$labels
1075+
if (is.null(labels)) {
10781076
return(NULL)
10791077
}
10801078

1081-
if (identical(self$labels, NA)) {
1079+
if (identical(labels, NA)) {
10821080
cli::cli_abort(
10831081
"Invalid {.arg labels} specification. Use {.code NULL}, not {.code NA}.",
10841082
call = self$call
10851083
)
10861084
}
10871085

1088-
if (is.waiver(self$labels)) {
1086+
if (is.waiver(labels)) {
10891087
if (!is.null(names(breaks))) {
1090-
return(names(breaks))
1091-
}
1092-
if (is.numeric(breaks)) {
1088+
labels <- names(breaks)
1089+
} else if (is.numeric(breaks)) {
10931090
# Only format numbers, because on Windows, format messes up encoding
1094-
format(breaks, justify = "none")
1091+
labels <- format(breaks, justify = "none")
10951092
} else {
1096-
as.character(breaks)
1093+
labels <- as.character(breaks)
10971094
}
1098-
} else if (is.function(self$labels)) {
1099-
self$labels(breaks)
1100-
} else {
1101-
if (!is.null(names(self$labels))) {
1102-
# If labels have names, use them to match with breaks
1103-
labels <- breaks
1104-
1105-
map <- match(names(self$labels), labels, nomatch = 0)
1106-
labels[map] <- self$labels[map != 0]
1107-
labels
1108-
} else {
1109-
labels <- self$labels
1095+
} else if (is.function(labels)) {
1096+
labels <- labels(breaks)
1097+
} else if (!is.null(names(labels))) {
1098+
# If labels have names, use them to match with breaks
1099+
map <- match(names(self$labels), breaks, nomatch = 0)
1100+
labels <- replace(breaks, map, labels[map != 0])
1101+
} else if (!is.null(attr(breaks, "pos"))) {
1102+
# Need to ensure that if breaks were dropped, corresponding labels are too
1103+
labels <- labels[attr(breaks, "pos")]
1104+
}
11101105

1111-
# Need to ensure that if breaks were dropped, corresponding labels are too
1112-
pos <- attr(breaks, "pos")
1113-
if (!is.null(pos)) {
1114-
labels <- labels[pos]
1115-
}
1116-
labels
1117-
}
1106+
if (is.expression(labels)) {
1107+
labels <- as.list(labels)
11181108
}
1109+
labels
11191110
},
11201111

11211112
clone = function(self) {
@@ -1351,6 +1342,9 @@ ScaleBinned <- ggproto("ScaleBinned", Scale,
13511342
call = self$call
13521343
)
13531344
}
1345+
if (is.expression(labels)) {
1346+
labels <- as.list(labels)
1347+
}
13541348
labels
13551349
},
13561350

0 commit comments

Comments
 (0)