Skip to content

Commit 4aba305

Browse files
hrbrmstrhadley
authored andcommitted
added fill to gpar in geom_curve() (#2375)
* added fill to gpar in geom_curve() * also added a aesthetic to geom_segment and geom_curve to enable filling in the arrow heads * Changed `fill` to `arrow.fill` and switched it from being an `aes()` to a param * Modified `NEWS.md`
1 parent dd04d8b commit 4aba305

File tree

4 files changed

+27
-7
lines changed

4 files changed

+27
-7
lines changed

NEWS.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@
4444
of padding between elements (@karawoo, #2143). A `reverse` parameter allows
4545
you to reverse the placement order of bars and boxes (@karawoo, #2171).
4646

47+
* The `geom_segment()` and `geom_curve()` geoms both have a new
48+
`arrow.fill` parameter which enables specifying a separate fill colour for
49+
closed arrowheads. (@hrbrmstr and @clauswilke, #2375).
50+
4751
* The `expand` argument for `scale_*_continuous()` and `scale_*_discrete()`
4852
now accepts separate expansion values for the lower and upper range
4953
limits. The expansion limits can be specified using the convenience

R/geom-curve.r

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ geom_curve <- function(mapping = NULL, data = NULL,
88
angle = 90,
99
ncp = 5,
1010
arrow = NULL,
11+
arrow.fill = NULL,
1112
lineend = "butt",
1213
na.rm = FALSE,
1314
show.legend = NA,
@@ -22,6 +23,7 @@ geom_curve <- function(mapping = NULL, data = NULL,
2223
inherit.aes = inherit.aes,
2324
params = list(
2425
arrow = arrow,
26+
arrow.fill = arrow.fill,
2527
curvature = curvature,
2628
angle = angle,
2729
ncp = ncp,
@@ -38,21 +40,27 @@ geom_curve <- function(mapping = NULL, data = NULL,
3840
#' @usage NULL
3941
#' @export
4042
GeomCurve <- ggproto("GeomCurve", GeomSegment,
43+
default_aes = aes(colour = "black", size = 0.5, linetype = 1, alpha = NA),
4144
draw_panel = function(data, panel_params, coord, curvature = 0.5, angle = 90,
42-
ncp = 5, arrow = NULL, lineend = "butt", na.rm = FALSE) {
45+
ncp = 5, arrow = NULL, arrow.fill, lineend = "butt", na.rm = FALSE) {
46+
4347
if (!coord$is_linear()) {
4448
warning("geom_curve is not implemented for non-linear coordinates",
4549
call. = FALSE)
4650
}
51+
4752
trans <- coord$transform(data, panel_params)
4853

54+
arrow.fill <- arrow.fill %||% trans$colour
55+
4956
curveGrob(
5057
trans$x, trans$y, trans$xend, trans$yend,
5158
default.units = "native",
5259
curvature = curvature, angle = angle, ncp = ncp,
5360
square = FALSE, squareShape = 1, inflect = FALSE, open = TRUE,
5461
gp = gpar(
5562
col = alpha(trans$colour, trans$alpha),
63+
fill = alpha(arrow.fill, trans$alpha),
5664
lwd = trans$size * .pt,
5765
lty = trans$linetype,
5866
lineend = lineend),

R/geom-segment.r

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
#' @inheritParams layer
1313
#' @inheritParams geom_point
1414
#' @param arrow specification for arrow heads, as created by arrow().
15+
#' @param arrow.fill fill color to use for the arrow head (if closed). `NULL`
16+
#' means use `colour` aesthetic.
1517
#' @param lineend Line end style (round, butt, square).
1618
#' @param linejoin Line join style (round, mitre, bevel).
1719
#' @seealso [geom_path()] and [geom_line()] for multi-
@@ -67,6 +69,7 @@ geom_segment <- function(mapping = NULL, data = NULL,
6769
stat = "identity", position = "identity",
6870
...,
6971
arrow = NULL,
72+
arrow.fill = NULL,
7073
lineend = "butt",
7174
linejoin = "round",
7275
na.rm = FALSE,
@@ -82,6 +85,7 @@ geom_segment <- function(mapping = NULL, data = NULL,
8285
inherit.aes = inherit.aes,
8386
params = list(
8487
arrow = arrow,
88+
arrow.fill = arrow.fill,
8589
lineend = lineend,
8690
linejoin = linejoin,
8791
na.rm = na.rm,
@@ -99,7 +103,7 @@ GeomSegment <- ggproto("GeomSegment", Geom,
99103
non_missing_aes = c("linetype", "size", "shape"),
100104
default_aes = aes(colour = "black", size = 0.5, linetype = 1, alpha = NA),
101105

102-
draw_panel = function(data, panel_params, coord, arrow = NULL,
106+
draw_panel = function(data, panel_params, coord, arrow = NULL, arrow.fill = NULL,
103107
lineend = "butt", linejoin = "round", na.rm = FALSE) {
104108

105109
data <- remove_missing(data, na.rm = na.rm,
@@ -109,11 +113,12 @@ GeomSegment <- ggproto("GeomSegment", Geom,
109113

110114
if (coord$is_linear()) {
111115
coord <- coord$transform(data, panel_params)
116+
arrow.fill <- arrow.fill %||% coord$colour
112117
return(segmentsGrob(coord$x, coord$y, coord$xend, coord$yend,
113118
default.units = "native",
114119
gp = gpar(
115120
col = alpha(coord$colour, coord$alpha),
116-
fill = alpha(coord$colour, coord$alpha),
121+
fill = alpha(arrow.fill, coord$alpha),
117122
lwd = coord$size * .pt,
118123
lty = coord$linetype,
119124
lineend = lineend,

man/geom_segment.Rd

Lines changed: 7 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)