Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

plot

r

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

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

Thanks in advance!

like image 423
Abdel Avatar asked Dec 04 '22 20:12

Abdel


1 Answers

Based on the clarifications to the original question, I think a general answer should consider

  • calculating the total length of the broken curve specified by (x,y) points

  • splitting apart intermediate segments so as to ensure equal curvilinear length between arrow heads

  • take care of minor details such as initial "phase", end-point, whether the arrows heads should be followed by a thin space, etc.

arrows

Below is a rough stab at it.

arrowLine <- function(x, y, N=10, ...){
  lengths <- c(0, sqrt(diff(x)^2 + diff(y)^2))
  l <- cumsum(lengths)
  tl <- l[length(l)]
  el <- seq(0, to=tl, length=N+1)[-1]

  plot(x, y, t="l", ...)

  for(ii in el){

    int <- findInterval(ii, l)
    xx <- x[int:(int+1)]
    yy <- y[int:(int+1)]

    ## points(xx,yy, col="grey", cex=0.5)

    dx <- diff(xx)
    dy <- diff(yy)
    new.length <- ii - l[int]
    segment.length <- lengths[int+1]

    ratio <- new.length / segment.length

    xend <- x[int] + ratio * dx
    yend <- y[int] + ratio * dy
    points(xend,yend, col="white", pch=19)
    arrows(x[int], y[int], xend, yend, length=0.1)

}

}

set.seed(123)
x = sort(c(0, runif(200, 0,2* pi), 2*pi))
y=sin(x)

arrowLine(x, y, N=20)
like image 86
baptiste Avatar answered Dec 17 '22 12:12

baptiste