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

When x values contain 0, using coord_trans("sqrt") removes the axis line #5919

Open
kdarras opened this issue May 30, 2024 · 4 comments
Open
Labels
bug an unexpected problem or unintended behavior coord 🗺️

Comments

@kdarras
Copy link

kdarras commented May 30, 2024

ggplot(iris,aes(Petal.Length,Sepal.Width))+
  geom_point()+
  coord_trans(x="sqrt")+
  theme(axis.line.x = element_line(color="black"))

image

iris$Petal.Length[1]=0
ggplot(iris,aes(Petal.Length,Sepal.Width))+
  geom_point()+
  coord_trans(x="sqrt")+
  theme(axis.line.x = element_line(color="black"))

image

I believe the axis line should stay preserved, since sqrt(0)=0, or is there a conceptual error in my reasoning?

@teunbrand
Copy link
Collaborator

You're right that the axis should be preserved.
I think it probably has an issue with calculating the left-most side of the axis that is smaller than 0.
I'll have to dig a little bit to see what is going on.

@teunbrand
Copy link
Collaborator

This isn't an issue with the axis, it happens as well with regular lines.
Notice how the line disappears when 0 is included in the limits.

library(ggplot2)

p <- ggplot(mpg, aes(displ, hwy)) +
  geom_point() +
  coord_trans(x = "sqrt")

p + annotate("line", x = c(-Inf, 4), y = 30, colour = "red", linewidth = 5)

last_plot() +
  xlim(c(0, NA))

Created on 2024-05-31 with reprex v2.1.0

It is most likely a coord munching artefact.

@teunbrand teunbrand added coord 🗺️ bug an unexpected problem or unintended behavior labels May 31, 2024
@teunbrand
Copy link
Collaborator

I can't currently see an elegant solution to this problem. Essentially, the scale expension produces negative values that cannot be transformed properly. We faced this in scale_x_sqrt() break calculations too, but for coord transforms we cannot simply squish the limits to the domain of the tranformation.

Instead, I'll suggest the following workaround, where we make a new transformation that will just be sqrt() for all positive values, but sign-preserves sqrt(abs(x)) for negative values. This was previously suggested here as well.

library(ggplot2)
library(scales)

sqrt2 <- new_transform(
  "sqrt2",
  transform = \(x) sign(x) * sqrt(abs(x)),
  inverse = \(x) sign(x) * abs(x)^2
)

plot(sqrt2, xlim = c(-5, 5))

ggplot(iris,aes(Petal.Length,Sepal.Width))+
  geom_point()+
  coord_trans(x=sqrt2, xlim = c(0, NA))+
  theme(axis.line.x = element_line(color="black"))

Created on 2024-06-03 with reprex v2.1.0

@kdarras
Copy link
Author

kdarras commented Jun 4, 2024

This did the trick, thanks Teun!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug an unexpected problem or unintended behavior coord 🗺️
Projects
None yet
Development

No branches or pull requests

2 participants