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

Could geom_abline and geom_qq_line always stop at the limits, regardless of other settings? #6081

Closed
davidhodge931 opened this issue Sep 6, 2024 · 7 comments

Comments

@davidhodge931
Copy link

Could geom_abline and geom_qq_line always stop at the limits, regardless of other settings?

So kind of act in the same way that geom_hline/geom_vline behave.

library(tidyverse)
library(palmerpenguins)

penguins |> 
  ggplot() +
  coord_cartesian(clip = "off") +
  scale_x_continuous(limits = c(-2, 2), oob = scales::oob_keep, expand = c(0, 0)) +
  scale_y_continuous(limits = c(2000, 6500), oob = scales::oob_keep, expand = c(0, 0)) +
  geom_qq(aes(sample = body_mass_g)) +
  geom_vline(xintercept = 1.5) + #good 
  geom_hline(yintercept = 3000) + #good
  geom_abline(intercept = 3000, slope = 250, colour = "blue") +
  geom_abline(intercept = 3000, slope = 1000, colour = "green") +
  geom_qq_line(aes(sample = body_mass_g), colour = "red") +
  theme(plot.margin = margin(r = 100))
#> Warning: Removed 2 rows containing non-finite outside the scale range
#> (`stat_qq()`).
#> Warning: Removed 2 rows containing non-finite outside the scale range
#> (`stat_qq_line()`).

Created on 2024-09-06 with reprex v2.1.1

@joranE
Copy link
Contributor

joranE commented Sep 6, 2024

I think the behavior of geom_qq_line & geom_qq should be the same when coord_cartesian(clip = "off"), not different. And I think it would be confusing if geom_qq clipped points to the plotting area even when you've specified coord_cartesian(clip = "off"), so I think that's a bad idea.

I'm less certain what geom_abline should do.

@davidhodge931
Copy link
Author

My logic is that you always want ablines and qq_lines to fall within the plot, but you .want flexibility in how to apply the coord_cartesian clip for the other geoms.

The clipping within coord_cartesian relates to all layers. So it can be helpful if you have the freedom to clip layers without affecting qq_lines and ablines (and hlines/vlines).

For example, you might want a scatter plot with set limits and a point might land on the outer limit. If you use "clip = off", this is nice because it keeps these points on the limits whole.

But then if you add a abline/qq_line, you are forced to change something, because it will exceed the boundaries of the plot

@clauswilke
Copy link
Member

qq lines span the data. I think it's Ok that the lines extend beyond the plot panel if you force data to fall outside of it as well. (Btw., this seems like a very contrived example to me. Who would do this outside of a bug report? Can you provide an actual example where this is a problem?)

ablines should probably be clipped to the plot panel, just like hlines and vlines.

Btw., you can force qq lines to be clipped to the panel by setting fullrange = TRUE. I know it's counterintuitive but it makes sense if you think about how that option is implemented.

library(tidyverse)
library(palmerpenguins)

penguins |> 
  ggplot() +
  coord_cartesian(clip = "off") +
  scale_x_continuous(limits = c(-2, 2), oob = scales::oob_keep, expand = c(0, 0)) +
  scale_y_continuous(limits = c(2000, 6500), oob = scales::oob_keep, expand = c(0, 0)) +
  geom_qq(aes(sample = body_mass_g)) +
  geom_qq_line(aes(sample = body_mass_g), colour = "red", fullrange = TRUE) +
  theme(plot.margin = margin(r = 100))
#> Warning: Removed 2 rows containing non-finite outside the scale range
#> (`stat_qq()`).
#> Warning: Removed 2 rows containing non-finite outside the scale range
#> (`stat_qq_line()`).

Created on 2024-09-05 with reprex v2.1.1

@davidhodge931
Copy link
Author

davidhodge931 commented Sep 6, 2024

Interesting, re fullrange = TRUE - agree the naming seems a bit unintuitive.

Maybe a better example/use-case.. A common thing to do is to set the limits of the plot to get rid of part of a qq_line (or abline). However, users will have to understand how the oob argument works for them to be able to actually achieve this. The oob concept and argument is not widely known/understood - and this could cause confusion. It'd be nicer if it worked in this case without having to know about this.

In essence, I feel qq_line and abline are just the same as vline/hline in that you always want it only in the panel.

library(tidyverse)
library(palmerpenguins)
penguins |> 
    ggplot(aes(sample = body_mass_g)) +
    geom_qq() +
    geom_qq_line(colour = "red") 
#> Warning: Removed 2 rows containing non-finite outside the scale range
#> (`stat_qq()`).
#> Warning: Removed 2 rows containing non-finite outside the scale range
#> (`stat_qq_line()`).

penguins |> 
  ggplot(aes(sample = body_mass_g)) +
  geom_qq() +
  geom_qq_line(colour = "red") +
  scale_y_continuous(limits = c(2500, 6500), expand = c(0, 0))
#> Warning: Removed 2 rows containing non-finite outside the scale range (`stat_qq()`).
#> Removed 2 rows containing non-finite outside the scale range
#> (`stat_qq_line()`).
#> Warning: Removed 2 rows containing missing values or values outside the scale range
#> (`geom_path()`).

penguins |> 
  ggplot(aes(sample = body_mass_g)) +
  geom_qq() +
  geom_qq_line(colour = "red") +
  scale_y_continuous(limits = c(2500, 6500), expand = c(0, 0), oob = scales::oob_keep)
#> Warning: Removed 2 rows containing non-finite outside the scale range
#> (`stat_qq()`).
#> Warning: Removed 2 rows containing non-finite outside the scale range
#> (`stat_qq_line()`).

Created on 2024-09-06 with reprex v2.1.1

@teunbrand
Copy link
Collaborator

Just as a note; the reason geom_abline() does not extends beyond the boundaries of the plot is #4024.

@clauswilke
Copy link
Member

@teunbrand I think the point here is that geom_abline() clips on the right and the left but not on the top and the bottom.

@davidhodge931 I feel we're discussing two entirely different things here. Could you close this issue and open two new ones, one for geom_abline() and one for geom_qq_line()? In particular, geom_qq_line() is inherently linked to the data (hence arguments such as fullrange) whereas geom_abline() just draws a line.

@clauswilke
Copy link
Member

@davidhodge931 Looking over your last set of examples, do I understand that here the problem is the line disappears entirely? If that's the issue then I agree this is a problem and we should discuss strategies to avoid it. But again, as I said in my other comment, entirely different from the geom_abline() issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants