Skip to content

Improved support for aesthetic aliases #2649

Closed

Description

@clauswilke

The current ggplot2 has support for aesthetic aliases, such as color instead of colour. However, there are two places where this support could be improved, and they should be tackled at the same time, I think.

1. Make aliases configurable

Currently the available aliases are hardcoded here:

ggplot2/R/aes.r

Lines 10 to 23 in eecc450

.base_to_ggplot <- c(
"col" = "colour",
"color" = "colour",
"pch" = "shape",
"cex" = "size",
"lty" = "linetype",
"lwd" = "size",
"srt" = "angle",
"adj" = "hjust",
"bg" = "fill",
"fg" = "colour",
"min" = "ymin",
"max" = "ymax"
)

I suspect most users are not even aware of some of those, e.g. "srt" = "angle". More importantly, it is currently not possible to create new aliases. So, if a package creates a new aesthetic, e.g. point_colour, it cannot create an alias point_color. The solution would be to provide functions that can add (and subtract?) aliases to (from) the default list. I would like to point out that this is conceptually similar to #2540 (user-defined theme elements), and similar techniques and user interfaces could be used in both cases.

2. Apply aliases to scales as well

Aliases are not applied to aesthetics arguments of scales. This leads to confusing behavior such as the following:

library(ggplot2)
# works
ggplot(iris, aes(Sepal.Length, Sepal.Width, color = Species)) +
  geom_point() +
  scale_color_viridis_d(aesthetics = "colour")

# does not work
ggplot(iris, aes(Sepal.Length, Sepal.Width, color = Species)) +
  geom_point() +
  scale_color_viridis_d(aesthetics = "color")

Created on 2018-05-21 by the reprex package (v0.2.0).

The solution is to rename aesthetics in the scales, just as it is done in the layers. There are only two places where this needs to happen, here:

ggplot2/R/scale-.r

Lines 554 to 571 in eecc450

continuous_scale <- function(aesthetics, scale_name, palette, name = waiver(),
breaks = waiver(), minor_breaks = waiver(), labels = waiver(), limits = NULL,
rescaler = rescale, oob = censor, expand = waiver(), na.value = NA_real_,
trans = "identity", guide = "legend", position = "left", super = ScaleContinuous) {
check_breaks_labels(breaks, labels)
position <- match.arg(position, c("left", "right", "top", "bottom"))
if (is.null(breaks) && !is_position_aes(aesthetics) && guide != "none") {
guide <- "none"
}
trans <- as.trans(trans)
if (!is.null(limits)) {
limits <- trans$transform(limits)
}

and here:

ggplot2/R/scale-.r

Lines 620 to 632 in eecc450

discrete_scale <- function(aesthetics, scale_name, palette, name = waiver(),
breaks = waiver(), labels = waiver(), limits = NULL, expand = waiver(),
na.translate = TRUE, na.value = NA, drop = TRUE,
guide = "legend", position = "left", super = ScaleDiscrete) {
check_breaks_labels(breaks, labels)
position <- match.arg(position, c("left", "right", "top", "bottom"))
if (is.null(breaks) && !is_position_aes(aesthetics) && guide != "none") {
guide <- "none"
}

Activity

  1. added
    feature
    on May 24, 2018
  2. clauswilke commented on Jul 7, 2018

    @clauswilke
    MemberAuthor

    @hadley expressed concern about user-modifiable aesthetics. I can see how that could be an issue, if e.g. point_color is sometimes converted into point_colour and sometimes not, depending on which package is loaded.

    An alternative approach might be to simply define a much larger set of aesthetic aliases in ggplot2. Real problems (such as #2674) arise only from the British/US spelling of color/colour, so all we'd have to do is define variants of colour, e.g. point_colour, line_colour, frame_colour, and maybe colour1, colour2, colour3. We would not have to do the same for fill, size, shape, etc. A second advantage of this approach is that it would steer package developers towards a unified set of aesthetics names.

  3. clauswilke commented on Jul 9, 2018

    @clauswilke
    MemberAuthor

    Two more possibilities, brought up here:

    1. Make it possible to specify aesthetics in themes
    2. Make it possible to specify aesthetics in layers

    Regardless of other pros and cons of these options, I think they both suffer from the same technical problem: Aesthetics are renamed in the aes() function:

    ggplot2/R/aes.r

    Lines 77 to 83 in c1908f1

    aes <- function(x, y, ...) {
    exprs <- rlang::enquos(x = x, y = y, ...)
    is_missing <- vapply(exprs, rlang::quo_is_missing, logical(1))
    aes <- new_aes(exprs[!is_missing], env = parent.frame())
    rename_aes(aes)
    }

    and aes() is usually called early in plot construction, before themes or layers are defined. So it's not clear how aesthetic definitions in the theme or layer could be made available to aes() in, e.g., the ggplot() call.

  4. hadley commented on Jul 9, 2018

    @hadley
    Member

    Does renaming matter though? I think it’s important for historical reasons, but I don’t think it’s needed for new aesthetics.

  5. clauswilke commented on Jul 9, 2018

    @clauswilke
    MemberAuthor

    It's all about the color/colour spelling differences. Currently color will be renamed to colour internally, but point_color will not be renamed to point_colour. No other nonstandard aesthetics need any support that isn't there yet. E.g., I can use point_size today without any problems.

    On further reflection, maybe the right solution is to just use a regular expression and replace any substring color in the name of an aesthetic with colour.

  6. added a commit that references this issue on Jul 11, 2018
    948f100
  7. added a commit that references this issue on Jul 23, 2018
    510b4b4
  8. lock commented on Jan 19, 2019

    @lock

    This old issue has been automatically locked. If you believe you have found a related problem, please file a new issue (with reprex) and link to this issue. https://reprex.tidyverse.org/

  9. locked and limited conversation to collaborators on Jan 19, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    featureinternals 🔎

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions