Curved arrows in R

[This article was first published on The stupidest thing... » R, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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:

library(igraph)
par(mar=rep(1,4))
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.


To leave a comment for the author, please follow the link and comment on their blog: The stupidest thing... » R.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)