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

geom_abline 3.3.4 odd y limits #4514

Closed
arnaud-feldmann opened this issue Jun 16, 2021 · 16 comments
Closed

geom_abline 3.3.4 odd y limits #4514

arnaud-feldmann opened this issue Jun 16, 2021 · 16 comments
Labels
bug an unexpected problem or unintended behavior
Milestone

Comments

@arnaud-feldmann
Copy link

arnaud-feldmann commented Jun 16, 2021

Hi,

I am aware of #4024 and #3741 that modify geom_abline. Through, the abline modification in 3.3.4 has had some strange results for the default y limits, maintening my modest package disaggR and in particular its autoplot method for in_scatter. I can't decide if it's a feature or a bug, I'm sorry if I make a mistake to report that.

object <- in_scatter(twoStepsBenchmark(turnover,construction))
n <- nrow(object)
df <- data.frame(Time=as.numeric(time(object))[-1L],
                 `High-frequency serie` = object[-n,2L],
                 `Low-frequency serie` = object[-n,1L],
                 xend = object[-1L,2L],
                 yend = object[-1L,1L], check.names = FALSE)

ggplot() +
  geom_abline(intercept = attr(object,"coefficients")["constant"],
                       slope = attr(object,"coefficients")[names(attr(object,"coefficients")) != "constant"],
                       lty = "solid", colour = "red", size = 1)  +
  geom_segment(data=df,
               aes(x = `High-frequency serie`, y = `Low-frequency serie`,
                   xend = xend, yend = yend,
                   colour = Time),
               arrow=arrow(angle = 15,
                           ends = "last",
                           type = "closed",
                           length = unit(0.1,"inches")),
               na.rm = TRUE)

On 3.3.4 The default y limits seems to be very odd to me
image

on 3.3.3 the behavior was as expected
image

@arnaud-feldmann arnaud-feldmann changed the title geom_abline 3.3.4 odd y scale geom_abline 3.3.4 odd y limits Jun 16, 2021
@clauswilke
Copy link
Member

clauswilke commented Jun 16, 2021

Could you please provide a reproducible example that depends on nothing but base R and ggplot2? And please simplify the example so all extraneous information is removed. This will make it easier to reason about the issue. Also, if you can, please use geom_point() instead of geom_segment(). However, if the problem only shows up for geom_segment() that would be good to know also.

@arnaud-feldmann
Copy link
Author

arnaud-feldmann commented Jun 16, 2021

Sorry for the bother,

@clauswilke Here is a very simpler example. Is this new default y limit intended ? Should we adapt our code ?

Thanks a lot for your work.

Arnaud

library(ggplot2)
df <- data.frame(X = c(670.964799810616, 738.482178701357, 774.152678327829,
                       816.022561905182, 877.503579439891, 933.507292448021),
                 Y = c(138.4, 143.9, 147.3, 155.9, 169.3, 181.4))
ggplot() +
  geom_abline(intercept = 44.28163,
              slope = 0.1410183)  +
  geom_point(data=df,
             aes(x = X, y = Y))

ggplot2 3.3.3 :
image

ggplot2 3.3.4 :
image

@clauswilke
Copy link
Member

clauswilke commented Jun 16, 2021

Looks like geom_abline() extends the line all the way to x = 0, no matter how far that is from the data. That definitely doesn't look like intended behavior to me.

library(ggplot2)
library(patchwork)

df <- data.frame(
  x = c(10000, 10001, 10002),
  y = c(500, 502, 501)
)

p <- ggplot(df) +
  geom_abline(
    intercept = -9500,
    slope = 1
  )  +
  geom_point(aes(x, y))

p | p + xlim(0, 10010)

Created on 2021-06-16 by the reprex package (v1.0.0)

@ptoche
Copy link

ptoche commented Jun 16, 2021

Is it related to this? where the range of the line was extended to fix another issue:

e127cc9

@arnaud-feldmann
Copy link
Author

arnaud-feldmann commented Jun 16, 2021

after having tried with and without, it is actually linked to that commit 041e287

Seems like the intercept being told that way as a known aesthetic for y, it behaves as @clauswilke told

@clauswilke clauswilke added the bug an unexpected problem or unintended behavior label Jun 16, 2021
@clauswilke clauswilke added this to the ggplot2 3.4.0 milestone Jun 16, 2021
@clauswilke
Copy link
Member

Yes, in hindsight, this was a problematic change. We want the intercept to get transformed but we don't want it to enter the limit calculations. We may need to separate out those two functions, or let the geoms/stats declare which aesthetics should be used for each of these calculations. The latter approach might be cleaner but is more work.

@thomasp85
Copy link
Member

I'm leaning on actually rolling this change back and doing a quick patch release.. thoughts, @clauswilke and @yutannihilation ?

@clauswilke
Copy link
Member

I agree. The newly introduced bug is more problematic than the bug that was fixed, in my opinion.

@andrefaure
Copy link

I am also having trouble with geom_abline 3.3.4, noticed this issue and think it could be related?
https://stackoverflow.com/questions/68068628/gglot2geom-abline-fails-with-default-settings-in-log-log-plot-ggplot2-v3-3-4

@yutannihilation
Copy link
Member

It sounds good to me to roll the change back. Also, I think the fix for the ggsave() issues (i.e., #4524 and #4521) might be worth including in the patch release.

@thomasp85
Copy link
Member

Agreed on the ggsave issues — was planning on including that as well. Will work on this today

@yutannihilation
Copy link
Member

Thanks. For #4521, my pr #4523 should fix the issue, I just need to confirm that with my old Windows. But feel free to take over :)

@thomasp85
Copy link
Member

#4525

@arnaud-feldmann
Copy link
Author

Hi,

Just to confirm that, as for me, ggplot2 3.3.5 solved the issue.

@billdenney
Copy link
Contributor

The fix for 3.3.5 only partly fixes the issue for me (at least, I believe that the issue below is the same as the initially-reported issue here). Specifically, when adding one log10 scale, the abline disappears now, but adding both puts it back:

library(ggplot2)

p <-
  ggplot(data.frame(x=1, y=1), aes(x=x, y=y)) +
  geom_point() +
  geom_abline(slope=1, intercept=0)

# basic plot shows up as expected
p

# adding scale_x_log10() drops the line which is not expected
p + scale_x_log10()

# adding scale_y_log10() drops the line which is not expected
p + scale_y_log10()

# adding both log10 scales puts the line back as desired
p + scale_x_log10() + scale_y_log10()

Created on 2021-07-14 by the reprex package (v2.0.0)

@thomasp85
Copy link
Member

@billdenney I think this is the original issue we tried to solve in 3.3.4 but reverted in 3.3.5 we still need to figure out a fix for that

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
Projects
None yet
Development

No branches or pull requests

7 participants