Fill arrow on geom_curve ggplot2
Asked Answered
G

2

15

Is there a way to get the arrowhead closed on geom_curve? The same code works with geom_segment. Maybe its a bug?

library(tidyverse)

set.seed(123)

data <- data_frame(x = rnorm(10), y = rnorm(10))

# NO ARROWHEAD FILL

ggplot(data, aes(x, y)) + 
  geom_point() +
  geom_curve(aes(x = 0, y = 0, xend = 1, yend = 1),
             color = "black",
             arrow = arrow(type = "closed"))

# ARROWHEAD FILL WORKS

ggplot(data, aes(x, y)) + 
  geom_point() +
  geom_segment(aes(x = 0, y = 0, xend = 1, yend = 1),
             color = "black",
             arrow = arrow(type = "closed"))
Gaivn answered 19/12, 2017 at 1:24 Comment(1)
When/If you do file an issue, you can reference this PR I just made to get the fill back into it's proper place. github.com/tidyverse/ggplot2/pull/2375Gentlefolk
G
13

I'd call it a bug and you should file an issue. Until then:

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,
    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,
      ...
    )
  )
}

GeomCurve2 <- ggproto("GeomCurve2", GeomSegment,
  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),
        fill = alpha(trans$colour, trans$alpha),
        lwd = trans$size * .pt,
        lty = trans$linetype,
        lineend = lineend),
      arrow = arrow
    )
  }
)

Which leads to:

ggplot(data, aes(x, y)) + 
  geom_point() +
  geom_curve2(aes(x = 0, y = 0, xend = 1, yend = 1),
             color = "black",
             arrow = arrow(type = "closed")) 

and

enter image description here

Gentlefolk answered 19/12, 2017 at 3:49 Comment(2)
Dang, I was just about to paste the exact same solution. You beat me by a minute. Even called it geom_curve2 and GeomCurve2 in my code.Adolescence
thank you all for the help...i wanted to make sure I didn't file a PR until I buzzed the Stack towerGaivn
A
9

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"))

enter image description here

How to make the equivalent changes to geom_segment() is left as an exercise for the reader.

Adolescence answered 19/12, 2017 at 4:1 Comment(4)
Aye. I kinda found it weird neither had a fill as well. I'll mod my PR for this and CC you on itGentlefolk
Gosh I hope the macros auto-re-gen the aesthetic section docs on the ggplot2-automation end :-)Gentlefolk
K. I back-ported it to geom_segment as well and cc'd you on the PR.Gentlefolk
@Gentlefolk gotta keep the docs in sync with CRAN, but I agree, we've gotta figure out a system to have the dev docs up somehow too!Sect

© 2022 - 2024 — McMap. All rights reserved.