-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Rename aesthetics in scales and consistently convert US to British spelling #2750
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
fde313e
b89eb83
948f100
1e5d36f
941d586
057110c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,9 +7,9 @@ NULL | |
#' properties (aesthetics) of geoms. Aesthetic mappings can be set in | ||
#' [ggplot2()] and in individual layers. | ||
#' | ||
#' This function also standardises aesthetic names by performing partial | ||
#' matching, converting color to colour, and translating old style R names to | ||
#' ggplot names (e.g. pch to shape, cex to size). | ||
#' This function also standardises aesthetic names by converting `color` to `colour` | ||
#' (also in substrings, e.g. `point_color` to `point_colour`) and translating old style | ||
#' R names to ggplot names (eg. `pch` to `shape`, `cex` to `size`). | ||
#' | ||
#' @section Quasiquotation: | ||
#' | ||
|
@@ -143,13 +143,34 @@ print.uneval <- function(x, ...) { | |
new_aes(NextMethod()) | ||
} | ||
|
||
# Rename American or old-style aesthetics name | ||
rename_aes <- function(x) { | ||
# Convert prefixes to full names | ||
full <- match(names(x), ggplot_global$all_aesthetics) | ||
names(x)[!is.na(full)] <- ggplot_global$all_aesthetics[full[!is.na(full)]] | ||
#' Standardise aesthetic names | ||
#' | ||
#' This function standardises aesthetic names by converting `color` to `colour` | ||
#' (also in substrings, e.g. `point_color` to `point_colour`) and translating old style | ||
#' R names to ggplot names (eg. `pch` to `shape`, `cex` to `size`). | ||
#' @param x Character vector of aesthetics names, such as `c("colour", "size", "shape")`. | ||
#' @return Character vector of standardised names. | ||
#' @keywords internal | ||
#' @export | ||
standardise_aes_names <- function(x) { | ||
# convert US to UK spelling of colour | ||
x <- sub("color", "colour", x, fixed = TRUE) | ||
|
||
plyr::rename(x, ggplot_global$base_to_ggplot, warn_missing = FALSE) | ||
# convert old-style aesthetics names to ggplot version | ||
plyr::revalue(x, ggplot_global$base_to_ggplot, warn_missing = FALSE) | ||
} | ||
|
||
# x is a list of aesthetic mappings, as generated by aes() | ||
rename_aes <- function(x) { | ||
names(x) <- standardise_aes_names(names(x)) | ||
duplicated_names <- names(x)[duplicated(names(x))] | ||
if (length(duplicated_names) > 0L) { | ||
duplicated_message <- paste0(unique(duplicated_names), collapse = ", ") | ||
warning( | ||
"Duplicated aesthetics after name standardisation: ", duplicated_message, call. = FALSE | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we make this an error? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll leave this to your decision. My own philosophy is it's only an error if the software cannot recover. Here, the duplicated aesthetics are simply ignored and a plot is produced: library(ggplot2)
df <- data.frame(x = 1:10, y = 1:10)
ggplot(df, aes(x, y, shape = "*", pch = "a")) + geom_point()
#> Warning: Duplicated aesthetics after name standardisation: shape It's not that different from this case, which doesn't even create a warning: ggplot(df, aes(x, y, shape = "*")) + geom_point(aes(pch = "a")) Created on 2018-07-13 by the reprex package (v0.2.0). |
||
) | ||
} | ||
x | ||
} | ||
|
||
# Look up the scale that should be used for a given aesthetic | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Where does this get called from?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The main user is the
aes()
function, but it's actually called from many places:ggplot2/R/aes.r
Lines 77 to 83 in 17b45f9
ggplot2/R/geom-defaults.r
Line 17 in 528b0ce
ggplot2/R/labels.r
Line 57 in 4299917
ggplot2/R/quick-plot.r
Line 86 in c1908f1
ggplot2/R/guides-.r
Line 63 in 3129bf8
ggplot2/R/layer.r
Lines 99 to 100 in 17b45f9
ggplot2/R/guide-legend.r
Line 183 in 17b45f9