Skip to content

Square-root transforms happily inverse-transforms negative values #214

Closed

Description

@paleolimbot

I'm not sure if this should be fixed, but it does lead to some confusing behaviour (tidyverse/ggplot2#980):

library(scales)

trans <- sqrt_trans()
trans$inverse(-2)
#> [1] 4
trans$transform(4)
#> [1] 2

trans <- sqrt_trans()
trans$inverse(-2)
#> [1] 4

# this is what I would expect to happen
trans2 <- trans_new("sqrt2", sqrt, function(x) ifelse(x < 0, NA_real_, x^2), domain = c(0, Inf))
trans2$inverse(-2)
#> [1] NA

Created on 2019-06-25 by the reprex package (v0.2.1)

Activity

  1. hadley commented on Nov 5, 2019

    @hadley
    Member

    Reverting the fix because it causes a failure in ggplot2

  2. reopened this on Nov 5, 2019
  3. zeehio commented on Nov 19, 2019

    @zeehio
    Contributor

    I would expect something like sign(x) * sqrt(abs(x)) with a reverse transform like sign(x)*x^2.

    Edit: I just saw Hadley had the same idea on the ggplot2 issue

    Edit2: I want to add that I would find a sqrt scale that handles negative values useful for visualization purposes regardless of the linked ggplot2 issue.

  4. added
    bugan unexpected problem or unintended behavior
    on Apr 3, 2020
  5. thomasp85 commented on Nov 2, 2023

    @thomasp85
    Member

    Changes in ggplot2 means we can now reapply the fix

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

    bugan unexpected problem or unintended behavior

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions