R - show movement of points

Suppose I have data of the following form

t1 = data.frame(a=c(2,3,3,1,5),b=c(6,4,5,2,1))
t2 = data.frame(a=c(3,4,4,1,8),b=c(5,5,5,3,3))


If you consider plot(t1$a,t1$b) and plot(t2$a,t2$b), you can imagine that the second plot is produced by taking every point on the first plot and moving it to its new location in the second plot.

I'd like some nice way of visualizing this. One way I thought of was to represent each t1 (a,b) as a vector reaching to its corresponding t2 (a,b) point, and then plotting a bunch of vectors, sort of like what I used to do in differential equations (I'm forgetting the technical name for this type of plot). Basically, it would be a plot of a bunch of little arrows that originate from each t1 (a,b) and point to each t2 (a,b), respectively.

Any ideas of how to do this in R, or any other ideas for visualizing and looking at how each dot moves.

NOTE: in the real version of what I hope to accomplish, I have lots of data, maybe 200 points, and I'm trying to show that in general, points are moving towards smaller a values and smaller b values from t1 to t2. But I really want to show this some other way besides something like a histogram of the difference between t2 and t1 average of a and b.

In terms of data visualization more generally, you probably want to use something like arrows going from the first set to the second set (and this is what I assume you intend by 'vectors' in any case).

You should also distinguish the two sets of data in some way. I used both colours and made the 'source' symbols open circles and the destination symbols smaller, closed circles (so coincident points of two different colors are readily seen).

That is, I suggest a plot something like this:

Drawing arrows is pretty straightforward in R (?arrows)

t1 <- data.frame(a=c(2,3,3,1,5),b=c(6,4,5,2,1))
t2 <- data.frame(a=c(3,4,4,1,8),b=c(5,5,5,3,3))

xlm <- range(c(t1$a,t2$a))
ylm <- range(c(t1$b,t2$b))

plot(b~a,t1,col=4,cex=1.1,xlim=xlm,ylim=ylm)
points(b~a,t2,col=2,pch=16,cex=0.9)
arrows(t1$a,t1$b,t2$a,t2$b,length=0.12)


If, as David Marx suggests, you want a jittered version, here's a way to do that:

xlm <- range(c(t1$a,t2$a))
ylm <- range(c(t1$b,t2$b))

jf <- function(x) jitter(x,amount=0.13)
t1j <- as.data.frame(t(apply(t1,1,jf)))
t2j <- as.data.frame(t(apply(t2,1,jf)))

plot(b~a,t1j,col=4,cex=1.1,xlim=xlm,ylim=ylm)
points(b~a,t2j,col=2,pch=16,cex=0.9)
arrows(t1j$a,t1j$b,t2j$a,t2j$b,length=0.12)

• +1 for exactly how I would do it down to the colors in the plot. One thing: you should consider jittering your points (or at least suggesting the option to OP). Look at point (3,5): it is both a start and an end point, which makes the transition it takes look strange, like a two step move when it's really two single step moves (presumably). – David Marx Nov 21 '13 at 4:17
• @DavidMarx I considered jittering but I figured that between the arrows and the colors it should already be clear enough. I'll add a little information on how to do a jittered version. – Glen_b Nov 21 '13 at 4:34
• So, I actually have like 200 arrows, and it looks like a big clump of black lines. Any ideas about how I could color code or smooth out to make like an average arrow, or something like that? This is getting a bit advanced, but maybe you have further ideas for visualization with so many arrows. – user164846 Nov 21 '13 at 16:07
• @user164846: It depends on what you mean by "big clump". If you mean you can't tell which end is which, that's a problem. If it means it's hard to get a feel for lines overlapping, you could try adjusting each line's (arrow's) alpha. – Wayne Nov 21 '13 at 16:32
• Thanks for the tip. Yes, I mean it just loos like one huge big mess of black lines lol. – user164846 Nov 21 '13 at 23:52

If you want to move outside R, consider d3.js: You can animate scatterplots easily, using this library and any modern browser. There is an interactive example in the online version of Scott Murry's " Interactive Data Visualization for the Web An Introduction to Designing with D3 " here.

• +1 Indeed, animation is a good idea. In R that can be done, for example, via the animation package. There's also Shiny. – Glen_b Nov 21 '13 at 11:59
• @Shiny, I've been digging around trying to find how to do the animation in R, can you provide some code? – user164846 Nov 21 '13 at 16:21
• @user164846: Depending on how your data looks, if you go in the d3 direction you can do something like: mbostock.github.io/d3/talk/20111116/bundle.html which is pretty amazing. – Wayne Nov 21 '13 at 16:36
• @user164846 - you just called me @Shiny, but that's the name of software (check the link in my previous comment to see more on it). The help, demos and examples that come with the animation package give pretty good coverage, and the Journal of Statistical Software article provides a useful introduction (if not quite up to date). There's also this post – Glen_b Nov 21 '13 at 23:22