11

I'd like to create a geom_path() that has arrows pointing towards the next location in the path.

I can get the path to plot, without issue, for example:

df <- (x=1:12, y=20:31, z=1:12)
p <- ggplot(df, aes(x=x, y=y))
p + geom_point() + geom_path()

Now what I'd like to be able to do is plot that point arrows from one element in the path to the next.

Extra marks if you could tell me how to smooth the lines from one element in the path to the next.

1 Answer 1

19

geom_segment has an arrow argument. Here's a short example:

library(grid) # needed for arrow function

p <- ggplot(df, aes(x=x, y=y)) +
     geom_point() +
     geom_segment(aes(xend=c(tail(x, n=-1), NA), yend=c(tail(y, n=-1), NA)),
                  arrow=arrow(length=unit(0.3,"cm")))

library(grid) is needed for arrow() function, see here.

3
  • geom_segment worked like a charm, but I'm wondering why you chose to go with geom_segment over what seems to be geom_path's primary function? Commented Aug 6, 2010 at 18:38
  • 2
    geom_path (or geom_line) doesn't plot the arrow in each segment, there is only one arrow at the position of the last point.
    – rcs
    Commented Aug 6, 2010 at 20:21
  • @rcs added library(grid), also link to the image is broken.
    – zx8754
    Commented Apr 17, 2014 at 11:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.