To add something useful to the answer @hrbrmstr gave, I think both geom_segment()
and geom_curve()
are unnecessarily limited in that they don't let you specify the arrow fill color separately from the arrow outline. Here I provide a geom_curve2()
that allows you to do that. The changed lines (relative to ggplot2 code) are highlighted.
# copied from ggplot2 `geom_curve`
geom_curve2 <- function(mapping = NULL, data = NULL,
stat = "identity", position = "identity",
...,
curvature = 0.5,
angle = 90,
ncp = 5,
arrow = NULL,
lineend = "butt",
na.rm = FALSE,
show.legend = NA,
inherit.aes = TRUE) {
layer(
data = data,
mapping = mapping,
stat = stat,
geom = GeomCurve2, # call `GeomCurve2` instead of `GeomCurve`
position = position,
show.legend = show.legend,
inherit.aes = inherit.aes,
params = list(
arrow = arrow,
curvature = curvature,
angle = angle,
ncp = ncp,
lineend = lineend,
na.rm = na.rm,
...
)
)
}
# copied from ggplot2 `GeomCurve`
GeomCurve2 <- ggproto("GeomCurve2", GeomSegment,
# the following `default_aes =` statement is missing in ggplot2 `GeomCurve`
default_aes = aes(colour = "black", fill = "black", size = 0.5, linetype = 1, alpha = NA),
draw_panel = function(data, panel_params, coord, curvature = 0.5, angle = 90,
ncp = 5, arrow = NULL, lineend = "butt", na.rm = FALSE) {
if (!coord$is_linear()) {
warning("geom_curve is not implemented for non-linear coordinates",
call. = FALSE)
}
trans <- coord$transform(data, panel_params)
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,
gp = gpar(
col = alpha(trans$colour, trans$alpha),
# the following `fill = ` statement is missing in ggplot2 `GeomCurve`
fill = alpha(trans$fill, trans$alpha),
lwd = trans$size * .pt,
lty = trans$linetype,
lineend = lineend),
arrow = arrow
)
}
)
Now we can specify the arrow fill separately from the outline:
ggplot(data, aes(x, y)) +
geom_point() +
geom_curve2(aes(x = 0, y = 0, xend = 1, yend = 1),
color = "black", fill = "red",
arrow = arrow(type = "closed"))
How to make the equivalent changes to geom_segment()
is left as an exercise for the reader.
fill
back into it's proper place. github.com/tidyverse/ggplot2/pull/2375 – Gentlefolk