Skip to content
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

Set common scale parameters outside of the scale function #4269

Open
clauswilke opened this issue Nov 17, 2020 · 1 comment · May be fixed by #6196 or #5444
Open

Set common scale parameters outside of the scale function #4269

clauswilke opened this issue Nov 17, 2020 · 1 comment · May be fixed by #6196 or #5444
Labels
feature a feature request or enhancement scales 🐍

Comments

@clauswilke
Copy link
Member

clauswilke commented Nov 17, 2020

A problem that pops up repeatedly in different contexts is that we may want to adjust certain scale parameters without explicitly calling a scale function. We already have some support for this, e.g. with xlim() to set limits on the x position scale or xlab() to set its title, but there are numerous other scenarios where the same problem arises. Here are some examples:

It would be good if we could come up with a generic approach to these issues. I note that xlim() and xlab() use entirely different implementation approaches currently, so that's not promising:

  • xlim() and ylim() use the ggplot2:::limits() function to automatically generate a scale function and give it the appropriate parameter settings:

    ggplot2/R/limits.r

    Lines 112 to 160 in 3be0acc

    limits <- function(lims, var) UseMethod("limits")
    #' @export
    limits.numeric <- function(lims, var) {
    if (length(lims) != 2) {
    abort("`lims` must be a two-element vector")
    }
    if (!any(is.na(lims)) && lims[1] > lims[2]) {
    trans <- "reverse"
    } else {
    trans <- "identity"
    }
    make_scale("continuous", var, limits = lims, trans = trans)
    }
    make_scale <- function(type, var, ...) {
    scale <- match.fun(paste("scale_", var, "_", type, sep = ""))
    scale(...)
    }
    #' @export
    limits.character <- function(lims, var) {
    make_scale("discrete", var, limits = lims)
    }
    #' @export
    limits.factor <- function(lims, var) {
    make_scale("discrete", var, limits = as.character(lims))
    }
    #' @export
    limits.Date <- function(lims, var) {
    if (length(lims) != 2) {
    abort("`lims` must be a two-element vector")
    }
    make_scale("date", var, limits = lims)
    }
    #' @export
    limits.POSIXct <- function(lims, var) {
    if (length(lims) != 2) {
    abort("`lims` must be a two-element vector")
    }
    make_scale("datetime", var, limits = lims)
    }
    #' @export
    limits.POSIXlt <- function(lims, var) {
    if (length(lims) != 2) {
    abort("`lims` must be a two-element vector")
    }
    make_scale("datetime", var, limits = as.POSIXct(lims))
    }

  • xlab() and ylab() ultimately feed their info to the update_labels() function, which writes to a special plot$labels field that the layout reads from:

    ggplot2/R/labels.r

    Lines 13 to 17 in 3aa2937

    update_labels <- function(p, labels) {
    p <- plot_clone(p)
    p$labels <- defaults(labels, p$labels)
    p
    }

    plot_table <- layout$render(geom_grobs, data, theme, plot$labels)

    ggplot2/R/layout.R

    Lines 107 to 113 in b76fa96

    labels <- self$coord$labels(
    list(
    x = self$xlabel(labels),
    y = self$ylabel(labels)
    ),
    self$panel_params[[1]]
    )

Neither approach generalizes directly to the other use cases. I also have concerns about the way xlim() and ylim() are implemented, because the fact that they add explicit scales functions is not obvious and can lead to strange behavior. See e.g. the following reprex, where in the first plot the call to scale_x_continuous() overwrites the xlim() call but in the second plot the xlab() call is not overwritten:

library(ggplot2)

ggplot(mtcars, aes(disp, mpg)) +
  geom_point() +
  xlim(100, 300) +
  scale_x_continuous(name = "Displacement")
#> Scale for 'x' is already present. Adding another scale for 'x', which will
#> replace the existing scale.

ggplot(mtcars, aes(disp, mpg)) +
  geom_point() +
  xlab("Displacement") +
  scale_x_continuous(limits = c(100, 300))
#> Warning: Removed 16 rows containing missing values (geom_point).

Created on 2020-11-17 by the reprex package (v0.3.0)

@DanChaltiel
Copy link

To add my two cents, the very message can be misleading as well as unnecessary:

ggplot(mtcars, aes(disp, mpg)) +
  geom_point() +
  xlim(100, 300) +
  scale_x_continuous(name = "Displacement")
#> Scale for 'x' is already present. Adding another scale for 'x', which will
#> replace the existing scale.

Even if it is not a warning, the message suggests that something has been replaced, thus overwritten. However, the two scales have only been merged since the name is intact.

A user could think that something is wrong although everything is fine.

@teunbrand teunbrand linked a pull request Sep 28, 2023 that will close this issue
@teunbrand teunbrand linked a pull request Nov 22, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature a feature request or enhancement scales 🐍
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants