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

Clipping in geom_abline() #6109

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions R/geom-abline.R
Original file line number Diff line number Diff line change
Expand Up @@ -132,12 +132,16 @@ GeomAbline <- ggproto("GeomAbline", Geom,
# Ensure the line extends well outside the panel to avoid visible line
# ending for thick lines
ranges$x <- ranges$x + c(-1, 1) * diff(ranges$x)
ranges$y <- ranges$y + c(-1, 1) * diff(ranges$y)
}

data$x <- ranges$x[1]
data$xend <- ranges$x[2]
data$y <- ranges$x[1] * data$slope + data$intercept
data$yend <- ranges$x[2] * data$slope + data$intercept
# Restrict 'x' to where 'y' is in range: x = (y - intercept) / slope
x <- sweep(outer(ranges$y, data$intercept, FUN = "-"), 2, data$slope, FUN = "/")

data$x <- pmax(ranges$x[1], pmin(x[1, ], x[2, ]))
data$xend <- pmin(ranges$x[2], pmax(x[1, ], x[2, ]))
data$y <- data$x * data$slope + data$intercept
data$yend <- data$xend * data$slope + data$intercept

GeomSegment$draw_panel(unique0(data), panel_params, coord, lineend = lineend)
},
Expand Down
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Visually identical, as is the flipped one.

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note the purple segment at the top is now gone. It should be gone because coord_polar() has 0 y-axis expansions, so it was actually out-of-bounds before.

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
19 changes: 19 additions & 0 deletions tests/testthat/test-geom-hline-vline-abline.R
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,25 @@ test_that("curved lines in map projections", {
)
})

test_that("geom_abline is clipped to x/y ranges", {

df <- data.frame(slope = c(-0.2, -1, -5, 5, 1, 0.2))

p <- ggplot(df) +
geom_abline(aes(slope = slope, intercept = 0)) +
scale_x_continuous(limits = c(-1, 1), expand = FALSE) +
scale_y_continuous(limits = c(-1, 1), expand = FALSE) +
coord_cartesian(clip = "off")

data <- layer_grob(p)[[1]]

x <- c(as.numeric(data$x0), as.numeric(data$x1))
expect_true(all(x >= 0 & x <= 1))

y <- c(as.numeric(data$y0), as.numeric(data$y1))
expect_true(all(y >= 0 & y <= 1))
})

# Warning tests ------------------------------------------------------------

test_that("warnings are thrown when parameters cause mapping and data to be ignored", {
Expand Down
Loading