I have written the following code:
library(ggplot2)
data <- structure(list(x = c(1L, 6L, 3L, 4L, 2L, 3L, 6L, 1L, 5L, 2L,
1L, 5L), y = c(
geom_path
should do the trick:
p <- ggplot(data, aes(x=x, y=y)) +
geom_point(aes(colour=year), shape=16, size=6) +
geom_path(aes(group=matching),
arrow=arrow(length=unit(0.15,"cm")),
colour="black", size=1) +
xlab("x") + ylab("y") +
scale_colour_manual("year", values=colors) +
scale_x_continuous(limits=c(1,7), breaks=seq(1,7, by=1)) +
scale_y_continuous(limits=c(1,7), breaks=seq(1,7, by=1))
print(p)
There is probably a more efficient way to do this, but one approach is to use geom_segment()
instead of geom_line()
. This will allow you to specify the beginning and ending points of the line with ease. We have to restructure the data so that we can specify x, y, xend, and yend. I will restructure with merge, though you could probably do this with cast or reshape.
zz <- merge(data[data$year == 2010 ,], data[data$year == 2011 ,]
, by = "matching", suffixes = 1:2)
matching x1 y1 year1 x2 y2 year2
1 person1 1 1 2010 6 6 2011
2 person2 6 7 2010 1 2 2011
3 person3 3 5 2010 5 5 2011
4 person4 4 6 2010 2 6 2011
5 person5 2 3 2010 1 5 2011
6 person6 3 4 2010 5 2 2011
We will then use two datasets in our call to ggplot
:
ggplot() + #Blank call to ggplot
geom_point(data = data, aes(x=x, y=y, colour=year), shape=16, size=6) + #Points
geom_segment(data = zz, aes(x = x1, y = y1, xend = x2, yend = y2), #Segments
arrow = arrow(length = unit(0.15, "cm")), colour = "black", size = 1) +
xlab("x") + ylab("y") +
scale_colour_manual("year", values=colors) +
scale_x_continuous(limits=c(1,7), breaks=seq(1,7, by=1)) +
scale_y_continuous(limits=c(1,7), breaks=seq(1,7, by=1))