Skip to content

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

Closed
@yutannihilation

Description

@yutannihilation

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugan unexpected problem or unintended behaviorlayers 📈

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions