R: how do I draw a line with multiple arrows in it?

前端 未结 5 2017
梦如初夏
梦如初夏 2020-12-04 02:25

I was wondering if anyone could help me plot lines in R with multiple arrows in them, like this:

--->--->--->--->

Thanks in advance!

5条回答
  •  -上瘾入骨i
    2020-12-04 03:11

    There's a sneaky way to do this without having to plot a bunch of line segments or arrows.
    Start with x and y data as above:

    plot(x, y, t='l')
    

    Then calculate the local slopes from x[1], y[1] to x[2], y[2] and so on, using some canned routine. Convert the slopes to angles in degrees, and then (calling the vector of slopes angslopes )

    text(x, y, labels=">", srt=angslopes)  # throws error
    

    Sorry, that won't work because, so far as I can tell, srt must be a single value. So:

    diflen <- length(x)-1
    sapply(1:diflen, function(xind) text(x[xind+1], y[xind+1], labels='>', cex=2.5, srt=angslopes[xind]))
    

    There may well be a much easier way to do this in the lattice package.

    This approach has the cosmetic advantage of allowing you to specify the arrowhead size via par(cex) as well as the color - which could be different from the line color, or even varying along the curve.

提交回复
热议问题