Skip to content

Commit 3899709

Browse files
committed
linearise discrete get_labels() logic
1 parent 1899f49 commit 3899709

File tree

1 file changed

+18
-27
lines changed

1 file changed

+18
-27
lines changed

R/scale-.R

Lines changed: 18 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1074,47 +1074,38 @@ ScaleDiscrete <- ggproto("ScaleDiscrete", Scale,
10741074
return(NULL)
10751075
}
10761076

1077-
if (is.null(self$labels)) {
1077+
labels <- self$labels
1078+
if (is.null(labels)) {
10781079
return(NULL)
10791080
}
10801081

1081-
if (identical(self$labels, NA)) {
1082+
if (identical(labels, NA)) {
10821083
cli::cli_abort(
10831084
"Invalid {.arg labels} specification. Use {.code NULL}, not {.code NA}.",
10841085
call = self$call
10851086
)
10861087
}
10871088

1088-
if (is.waiver(self$labels)) {
1089+
if (is.waiver(labels)) {
10891090
if (!is.null(names(breaks))) {
1090-
return(names(breaks))
1091-
}
1092-
if (is.numeric(breaks)) {
1091+
labels <- names(breaks)
1092+
} else if (is.numeric(breaks)) {
10931093
# Only format numbers, because on Windows, format messes up encoding
1094-
format(breaks, justify = "none")
1094+
labels <- format(breaks, justify = "none")
10951095
} else {
1096-
as.character(breaks)
1096+
labels <- as.character(breaks)
10971097
}
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
1098+
} else if (is.function(labels)) {
1099+
labels <- labels(breaks)
1100+
} else if (!is.null(names(labels))) {
1101+
# If labels have names, use them to match with breaks
1102+
map <- match(names(self$labels), breaks, nomatch = 0)
1103+
labels <- replace(breaks, map, labels[map != 0])
1104+
} else if (!is.null(attr(breaks, "pos"))) {
1105+
# Need to ensure that if breaks were dropped, corresponding labels are too
1106+
labels <- labels[attr(breaks, "pos")]
1107+
}
11101108

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-
}
11181109
}
11191110
},
11201111

0 commit comments

Comments
 (0)