Skip to content

side effect errors since 3.4.0 : scale_x_continuous() with user provided trans object now explicitly calls the transform function with domain edges as input #5105

Closed
@phauchamps

Description

@phauchamps

Hi,

Since 3.4.0 I am getting error messages from some extensions of scale_x_continuous() that are implemented in third parties packages. For the record, the 3rd party package I am using is ggcyto and the function I am using is scale_x_logicle() which just provides a specific transformation function to scale_x_continuous using trans = scales::trans_new(ggcyto_object).

Digging into the issue, I found that when displaying the plot, ggplot2 now calls, among the numerous calls to the transformation function or its inverse, the transformation function explicitely with the domain edges as input, which is by default c(-Inf, +Inf). This has as side effect that the transformation functions that are not robust for extreme values, may crash or raise an error message. This explicit call to the transform function, with the domain edges values as input, was not done in version 3.3.6, so I don't get why this might now be necessary.

Herebelow is a minimal reprex, using a dummy transformation function, which just implements the identity function, but raises an error when extreme values are passed. This example works without any concern with ggplot2 version 3.3.6, but raises the error message "extreme value passed => convergence problem!" with ggplot2 version 3.4.0

Could you possibly have a look and advise ?

Thx,

Philippe

# reprex
library(ggplot2)

myDummyTrans <- function(x) {
    if (any(x < -1.0e99 | x > 1.0e99, na.rm = TRUE))
        stop("extreme value passed => convergence problem!")
    return(x)    
}

myDummyInverseTrans <- function(x) {
    return(x)
}

myTrans <- scales::trans_new("myDummy",
                             transform = myDummyTrans,
                             inverse = myDummyInverseTrans)

set.seed(1)
values <- rnorm(10000, mean = 0, sd = 1)
myData <- data.frame(value = values)

p <- ggplot(data = myData,
            aes(x = value)) + 
    geom_density()

#debug(myDummyTrans)

sc <- scale_x_continuous(limits = c(-6, 6),
                         trans = myTrans)

p + sc

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions