Skip to content

Commit 510b4b4

Browse files
authored
Rename aesthetics in scales and consistently convert US to British spelling (#2750)
* more consistent support of US spelling for colour aesthetics. * update docs, add regression tests * Add news item. Closes #2649 * remove spurious namespace prefix * improved error message for duplicated aesthetics * fix unit test
1 parent 21fdbc8 commit 510b4b4

File tree

7 files changed

+75
-12
lines changed

7 files changed

+75
-12
lines changed

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,7 @@ export(scale_y_time)
506506
export(sec_axis)
507507
export(set_last_plot)
508508
export(should_stop)
509+
export(standardise_aes_names)
509510
export(stat)
510511
export(stat_bin)
511512
export(stat_bin2d)

NEWS.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313
feed data columns into `aes()` or into parameters of geoms or stats. However,
1414
doing so remains discouraged (@clauswilke).
1515

16+
* Aesthetic names are now consistently standardised both in `aes()` and in the
17+
`aesthetics` argument of scale functions. Also, the US spelling "color"
18+
is now always internally converted to "colour", even when part of a longer
19+
aesthetic name (e.g., `point_color`) (@clauswilke, #2649).
1620

1721
# ggplot2 3.0.0
1822

R/aes.r

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ NULL
77
#' properties (aesthetics) of geoms. Aesthetic mappings can be set in
88
#' [ggplot2()] and in individual layers.
99
#'
10-
#' This function also standardises aesthetic names by performing partial
11-
#' matching, converting color to colour, and translating old style R names to
12-
#' ggplot names (e.g. pch to shape, cex to size).
10+
#' This function also standardises aesthetic names by converting `color` to `colour`
11+
#' (also in substrings, e.g. `point_color` to `point_colour`) and translating old style
12+
#' R names to ggplot names (eg. `pch` to `shape`, `cex` to `size`).
1313
#'
1414
#' @section Quasiquotation:
1515
#'
@@ -143,13 +143,34 @@ print.uneval <- function(x, ...) {
143143
new_aes(NextMethod())
144144
}
145145

146-
# Rename American or old-style aesthetics name
147-
rename_aes <- function(x) {
148-
# Convert prefixes to full names
149-
full <- match(names(x), ggplot_global$all_aesthetics)
150-
names(x)[!is.na(full)] <- ggplot_global$all_aesthetics[full[!is.na(full)]]
146+
#' Standardise aesthetic names
147+
#'
148+
#' This function standardises aesthetic names by converting `color` to `colour`
149+
#' (also in substrings, e.g. `point_color` to `point_colour`) and translating old style
150+
#' R names to ggplot names (eg. `pch` to `shape`, `cex` to `size`).
151+
#' @param x Character vector of aesthetics names, such as `c("colour", "size", "shape")`.
152+
#' @return Character vector of standardised names.
153+
#' @keywords internal
154+
#' @export
155+
standardise_aes_names <- function(x) {
156+
# convert US to UK spelling of colour
157+
x <- sub("color", "colour", x, fixed = TRUE)
151158

152-
plyr::rename(x, ggplot_global$base_to_ggplot, warn_missing = FALSE)
159+
# convert old-style aesthetics names to ggplot version
160+
plyr::revalue(x, ggplot_global$base_to_ggplot, warn_missing = FALSE)
161+
}
162+
163+
# x is a list of aesthetic mappings, as generated by aes()
164+
rename_aes <- function(x) {
165+
names(x) <- standardise_aes_names(names(x))
166+
duplicated_names <- names(x)[duplicated(names(x))]
167+
if (length(duplicated_names) > 0L) {
168+
duplicated_message <- paste0(unique(duplicated_names), collapse = ", ")
169+
warning(
170+
"Duplicated aesthetics after name standardisation: ", duplicated_message, call. = FALSE
171+
)
172+
}
173+
x
153174
}
154175

155176
# Look up the scale that should be used for a given aesthetic

R/scale-.r

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -556,6 +556,8 @@ continuous_scale <- function(aesthetics, scale_name, palette, name = waiver(),
556556
rescaler = rescale, oob = censor, expand = waiver(), na.value = NA_real_,
557557
trans = "identity", guide = "legend", position = "left", super = ScaleContinuous) {
558558

559+
aesthetics <- standardise_aes_names(aesthetics)
560+
559561
check_breaks_labels(breaks, labels)
560562

561563
position <- match.arg(position, c("left", "right", "top", "bottom"))
@@ -622,6 +624,8 @@ discrete_scale <- function(aesthetics, scale_name, palette, name = waiver(),
622624
na.translate = TRUE, na.value = NA, drop = TRUE,
623625
guide = "legend", position = "left", super = ScaleDiscrete) {
624626

627+
aesthetics <- standardise_aes_names(aesthetics)
628+
625629
check_breaks_labels(breaks, labels)
626630

627631
position <- match.arg(position, c("left", "right", "top", "bottom"))

man/aes.Rd

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/standardise_aes_names.Rd

Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-aes.r

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,19 @@ test_that("labelling doesn't cause error if aesthetic is NULL", {
9898
expect_null(p$labels$x)
9999
})
100100

101+
test_that("aes standardises aesthetic names", {
102+
# test a few common cases
103+
expect_identical(aes(color = x), aes(colour = x))
104+
expect_identical(aes(pch = x), aes(shape = x))
105+
106+
# US to British spelling in substrings
107+
expect_identical(aes(point_color = x), aes(point_colour = x))
108+
expect_identical(aes(color_point = x), aes(colour_point = x))
109+
110+
# warning when standardisation creates duplicates
111+
expect_warning(aes(color = x, colour = y), "Duplicated aesthetics")
112+
})
113+
101114

102115
# Visual tests ------------------------------------------------------------
103116

0 commit comments

Comments
 (0)