Skip to content
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
10 changes: 6 additions & 4 deletions R/geom-curve.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
GeomCurve <- ggproto(
"GeomCurve", GeomSegment,

draw_panel = function(data, panel_params, coord, curvature = 0.5, angle = 90,
ncp = 5, arrow = NULL, arrow.fill = NULL, lineend = "butt", na.rm = FALSE) {
draw_panel = function(data, panel_params, coord, curvature = 0.5, angle = 90, ncp = 5, shape = 0.5,
arrow = NULL, arrow.fill = NULL, lineend = "butt", na.rm = FALSE) {

if (!coord$is_linear()) {
cli::cli_warn("{.fn geom_curve} is not implemented for non-linear coordinates")
Expand All @@ -31,11 +31,13 @@ GeomCurve <- ggproto(

arrow.fill <- arrow.fill %||% trans$colour

square <- (ncp == 1 && angle == 90)

curveGrob(
trans$x, trans$y, trans$xend, trans$yend,
default.units = "native",
curvature = curvature, angle = angle, ncp = ncp,
square = FALSE, squareShape = 1, inflect = FALSE, open = TRUE,
curvature = curvature, angle = angle, ncp = ncp, shape = shape,
square = square, squareShape = 1, inflect = FALSE, open = TRUE,
gp = gg_par(
col = alpha(trans$colour, trans$alpha),
fill = alpha(arrow.fill, trans$alpha),
Expand Down
17 changes: 17 additions & 0 deletions R/geom-segment.R
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,23 @@ GeomSegment <- ggproto(
#' arrow = arrow(length = unit(0.03, "npc"))
#' )
#'
#' # The `shape` and `ncp` arguments of geom_curve control the sharpness of the spline
#' b +
#' geom_curve(
#' aes(x = x1, y = y1, xend = x2, yend = y2, colour = "ncp = 5"),
#' data = df,
#' curvature = 1,
#' shape = 0,
#' ncp = 5
#' ) +
#' geom_curve(
#' aes(x = x1, y = y1, xend = x2, yend = y2, colour = "ncp = 1"),
#' data = df,
#' curvature = 1,
#' shape = 0,
#' ncp = 1
#' )
#'
#' if (requireNamespace('maps', quietly = TRUE)) {
#' ggplot(seals, aes(long, lat)) +
#' geom_segment(aes(xend = long + delta_long, yend = lat + delta_lat),
Expand Down
22 changes: 22 additions & 0 deletions man/geom_segment.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

77 changes: 77 additions & 0 deletions tests/testthat/_snaps/geom-curve/multishape-geom-curve.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
39 changes: 39 additions & 0 deletions tests/testthat/test-geom-curve.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,42 @@ test_that("geom_curve flipping works", {
expect_doppelganger("flipped geom_curve", p + scale_y_reverse())

})

test_that("geom_curve shape works", {

df <- data.frame(x = c(1, 3), xend = c(2, 4), y = c(0, 1), yend = c(2, 1.5))

p <- ggplot(df) +
geom_curve(
aes(x, y, xend = xend, yend = yend, color = "square"),
curvature = 1,
shape = 0,
ncp = 1
) +
geom_curve(
# This layer will use `square = FALSE` in curveGrob because angle != 90
aes(x, y, xend = xend, yend = yend, color = "square tilted"),
angle = 60,
curvature = 1,
shape = 0,
ncp = 1
) +
geom_curve(
aes(x, y, xend = xend, yend = yend, color = "spline cubic"),
curvature = -.5,
angle = 40,
shape = 1,
ncp = 1
) +
geom_curve(
aes(x, y, xend = xend, yend = yend, color = "spline interpolating"),
curvature = -.5,
angle = 40,
shape = -1,
ncp = 1
) +
NULL

expect_doppelganger("multishape geom_curve", p)

})