Skip to content

Commit

Permalink
clip geom_abline() in y-direction
Browse files Browse the repository at this point in the history
  • Loading branch information
teunbrand committed Sep 17, 2024
1 parent 842e6be commit e5664f6
Showing 1 changed file with 8 additions and 4 deletions.
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

0 comments on commit e5664f6

Please sign in to comment.