Skip to content

Commit bc605b6

Browse files
fmarottateunbrand
andauthored
Expose shape argument in geom_curve() (#6523)
* Expose shape argument in geom_curve() (#5998) * Add tests for the shape argument in geom_curve() * Add docs for shape argument in geom_curve() * Add example for shape and ncp params in geom_curve() * Apply suggestion to improve geom_curve() test * Add snapshot for new geom_curve() test * add news bullet --------- Co-authored-by: Teun van den Brand <tahvdbrand@gmail.com>
1 parent 00c2667 commit bc605b6

File tree

6 files changed

+163
-4
lines changed

6 files changed

+163
-4
lines changed

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
* `get_layer_data()` and `get_layer_grob()` now accept layer names as index
44
(@lgaborini, #6724)
5+
* Added new argument `geom_curve(shape)` that will be passed down to
6+
`grid::curveGrob()` (@fmarotta, #5998).
57

68
# ggplot2 4.0.1
79

R/geom-curve.R

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
GeomCurve <- ggproto(
77
"GeomCurve", GeomSegment,
88

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

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

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

34+
square <- (ncp == 1 && angle == 90)
35+
3436
curveGrob(
3537
trans$x, trans$y, trans$xend, trans$yend,
3638
default.units = "native",
37-
curvature = curvature, angle = angle, ncp = ncp,
38-
square = FALSE, squareShape = 1, inflect = FALSE, open = TRUE,
39+
curvature = curvature, angle = angle, ncp = ncp, shape = shape,
40+
square = square, squareShape = 1, inflect = FALSE, open = TRUE,
3941
gp = gg_par(
4042
col = alpha(trans$colour, trans$alpha),
4143
fill = alpha(arrow.fill, trans$alpha),

R/geom-segment.R

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,23 @@ GeomSegment <- ggproto(
8888
#' arrow = arrow(length = unit(0.03, "npc"))
8989
#' )
9090
#'
91+
#' # The `shape` and `ncp` arguments of geom_curve control the sharpness of the spline
92+
#' b +
93+
#' geom_curve(
94+
#' aes(x = x1, y = y1, xend = x2, yend = y2, colour = "ncp = 5"),
95+
#' data = df,
96+
#' curvature = 1,
97+
#' shape = 0,
98+
#' ncp = 5
99+
#' ) +
100+
#' geom_curve(
101+
#' aes(x = x1, y = y1, xend = x2, yend = y2, colour = "ncp = 1"),
102+
#' data = df,
103+
#' curvature = 1,
104+
#' shape = 0,
105+
#' ncp = 1
106+
#' )
107+
#'
91108
#' if (requireNamespace('maps', quietly = TRUE)) {
92109
#' ggplot(seals, aes(long, lat)) +
93110
#' geom_segment(aes(xend = long + delta_long, yend = lat + delta_lat),

man/geom_segment.Rd

Lines changed: 22 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 77 additions & 0 deletions
Loading

tests/testthat/test-geom-curve.R

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,42 @@ test_that("geom_curve flipping works", {
99
expect_doppelganger("flipped geom_curve", p + scale_y_reverse())
1010

1111
})
12+
13+
test_that("geom_curve shape works", {
14+
15+
df <- data.frame(x = c(1, 3), xend = c(2, 4), y = c(0, 1), yend = c(2, 1.5))
16+
17+
p <- ggplot(df) +
18+
geom_curve(
19+
aes(x, y, xend = xend, yend = yend, color = "square"),
20+
curvature = 1,
21+
shape = 0,
22+
ncp = 1
23+
) +
24+
geom_curve(
25+
# This layer will use `square = FALSE` in curveGrob because angle != 90
26+
aes(x, y, xend = xend, yend = yend, color = "square tilted"),
27+
angle = 60,
28+
curvature = 1,
29+
shape = 0,
30+
ncp = 1
31+
) +
32+
geom_curve(
33+
aes(x, y, xend = xend, yend = yend, color = "spline cubic"),
34+
curvature = -.5,
35+
angle = 40,
36+
shape = 1,
37+
ncp = 1
38+
) +
39+
geom_curve(
40+
aes(x, y, xend = xend, yend = yend, color = "spline interpolating"),
41+
curvature = -.5,
42+
angle = 40,
43+
shape = -1,
44+
ncp = 1
45+
) +
46+
NULL
47+
48+
expect_doppelganger("multishape geom_curve", p)
49+
50+
})

0 commit comments

Comments
 (0)