Skip to content

Can geom_abline() draw lines a bit longer than the actual Coord range? #4024

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

Closed
yutannihilation opened this issue May 24, 2020 · 4 comments · Fixed by #4422
Closed

Can geom_abline() draw lines a bit longer than the actual Coord range? #4024

yutannihilation opened this issue May 24, 2020 · 4 comments · Fixed by #4422
Labels
bug an unexpected problem or unintended behavior layers 📈
Milestone

Comments

@yutannihilation
Copy link
Member

Someone told me geom_abline() doesn't look nice when we increase the size. To be fair, geom_function() seems better at the first example, but it has the same problem when the range of the scale is shorter or equal to the coord's range. I think this is intentional, but it might be nice if there's some option to draw lines to outside of the plot area.

(Btw, maybe this is a good chance to replace GeomAbline with GeomFunction...?)

library(ggplot2)
library(tibble)
library(patchwork)

p <- ggplot(tibble(x = c(-5, 5), y = c(-4, 6))) +
  geom_line(aes(x, y), size = 10, color = "#cc0000") +
  coord_fixed(xlim = c(-1, 1), ylim = c(-1, 1), expand = FALSE) +
  theme_bw()

p1 <- p +
  geom_abline(slope = 1, intercept = 1, size = 10, color = "#000088", alpha = 0.7) +
  ggtitle("abline")

p2 <- p +
  geom_function(fun = ~ .x + 1, size = 10, color = "#000088", alpha = 0.7) +
  ggtitle("function")

p1 * p2

Created on 2020-05-24 by the reprex package (v0.3.0)

library(ggplot2)

ggplot() +
  geom_function(fun = ~ .x + 1, size = 10, color = "#000088", alpha = 0.7) +
  xlim(-1, 1) +
  ggtitle("function") + 
  coord_fixed(xlim = c(-1, 1), ylim = c(-1, 1), expand = FALSE) +
  theme_bw()

Created on 2020-05-24 by the reprex package (v0.3.0)

@thomasp85
Copy link
Member

I think both layers should calculate the lines on a very expanded scale to avoid this. Both layers are scale-less and this should be reflected in the look

@thomasp85 thomasp85 added bug an unexpected problem or unintended behavior layers 📈 labels Aug 31, 2020
@clauswilke
Copy link
Member

Just a thought, if somebody wants to tackle this: It may be necessary to make expansion configurable, in case somebody wants to draw an abline but turn off clipping. Expansion could also cause weird effects in nonlinear coordinate systems.

@thomasp85 thomasp85 added this to the ggplot2 3.3.4 milestone Mar 25, 2021
@thomasp85
Copy link
Member

Hmm... so this is a bit more involved than how it looks on the outside, at least for geom_function(). Since the coordinates used by geom_function() is calculated by stat_function() any values outside the range of the scales will be censored and removed by default - circumventing this will require some nasty hacks that aren't warranted I think...

It is easier for geom_abline() because the very modest calculations all happens in the draw methods of the geom so the scale doesn't have a chance to censor the values.

I propose we fix this for geom_abline() only - to address @clauswilke's point we could simply only expand the range if the coord is clipping and not polar

@thomasp85
Copy link
Member

An interesting observation regarding this. The graphics engine will cut geometries to fit into the drawing area before passing it off to the device. This is done for historical reasons since some of the early graphic devices couldn't handle coordinates outside of their drawing area. I knew about this and have asked Paul to allow opt-out of this (mostly for performance reasons). A side effect of this behaviour can be seen here:
image

If the panel is very close to the edge and the line width is very large there is nothing we can do for graphic devices that doesn't opt out

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

Successfully merging a pull request may close this issue.

3 participants