Curved arrows in R

I briefly investigated how to draw curved arrows in R. Here’s a small piece of the figure that I ultimately created:

A google search for “curved arrows in R” revealed three options:

  • curvedarrow in the diagram package
  • The internal function igraph.Arrows within the igraph package (mentioned by Gabor Csardi in R help)
  • Using xspline for the shaft and adding the head “by hand” (mentioned by Greg Snow)

I wasn’t too happy with how the arrow heads looked using the diagram package, and I didn’t want to have to fiddle around with xspline, and igraph worked quite easily, so I went with that.

Plotting multiple arrows with different colors in a single call to igraph.Arrows didn’t work right (the arrow heads were all the first color), but using a loop and plotting one arrow per call worked fine.

Here’s a bit of code I played with:

plot(0,0, type="n",xaxt="n", yaxt="n", xlab="", ylab="",
     xlim=c(0,11), ylim=c(1,11))

x <- 1:10
y <- rep(2, 10)

for(k in 0:4)
  points(x, y+k*2)

iArrows <- igraph:::igraph.Arrows

mycolors <- rep(c("green", "orange", "red", "blue"), 3)

for(i in 1:9) {
  iArrows(x[i], y[i], x[i+1], y[i+1],
          h.lwd=2, sh.lwd=2, sh.col=mycolors[i],
          curve=0.5 - (i %% 2), width=1, size=0.7)

  iArrows(x[i], y[i]+2, x[i+1], y[i+1]+2,
          h.lwd=2, sh.lwd=2, sh.col=mycolors[i],
          curve=1 - (i %% 2), width=1, size=0.7)

for(i in 1:8) {
  iArrows(x[i], y[i]+4, x[i+2], y[i+2]+4,
          h.lwd=2, sh.lwd=2, sh.col=mycolors[i],
          curve=0.5 - (i %% 2), width=1, size=0.7)

  iArrows(x[i], y[i]+6, x[i+2], y[i+2]+6,
          h.lwd=2, sh.lwd=2, sh.col=mycolors[i],
          curve=1 - 2*(i %% 2), width=1, size=0.7)

x1 <- x[1:8]
x2 <- x[3:10]
y <- y[1:8]+8

iArrows(x1, y, x2, y,
        h.lwd=2, sh.lwd=2, sh.col=mycolors[1:8],
        curve=1 - 2*((1:8) %% 2), width=1, size=0.7)

And here’s the corresponding figure:

Note that the arrows at the top all have green arrow heads.

Tags: ,

4 Responses to “Curved arrows in R”

  1. cellocgw Says:

    Haven’t tried it, but I think plotrix::draw.arc will let you pass an arrowhead argument.

  2. statsinthewild Says:

    Reblogged this on Stats in the Wild.

  3. Dr.D.K.Samuel Says:

    thanks it worked as you wrote

  4. Using Bezier curves to draw flow lines « Andrew Wheeler Says:

    […] Perhaps in the future I will work on an R function to calculate Bezier curves (I’m sure they could be of some use), but hopefully for those interested this is simple enough to program your own function in whatever […]

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Get every new post delivered to your Inbox.

Join 136 other followers