Using the animation package and geom_segment
this is reasonably straight forward
My only issue thus far is getting a scale for the size to work reasonable
I've saved the talking data.frame as talking
library(animation)
library(RColorBrewer)
library(grid) ## for arrow
library(ggplot2)
# scale the duration (not ideal)
talking$scale_duration <-scale(talking$duration, center = FALSE)
# ensure that we have different colours for each speaker
ss <- levels(talking$speaker)
speakerCol <- scale_colour_manual(values = setNames(brewer.pal(n=length(ss), 'Set2' ), ss), guide = 'none')
# the base plot with the table and speakers (and `talking` base dataset)
base <- ggplot(data = talking, aes(colour = speaker)) +
geom_point(data=mapping, aes(x=x, y=y), size=10, inherit.aes = FALSE) +
geom_text(data=mapping, aes(x=x, y=y, label=as.character(person)),
inherit.aes = FALSE, color="blue") +
ylim(-.2, 1.2) + xlim(-.2, 1.2) +
geom_rect(data=the_table, aes(xmax = xmax, xmin=xmin,
ymin=ymin, ymax = ymax), fill="gray80", inherit.aes = FALSE) +
speakerCol
oopt <- ani.options(interval = 0.5)
# a function to create the animation
pp <- function(){
print(base)
interval = ani.options("interval")
for(n in rep(seq_along(talking$duration), each = talking$duration))){
# a segment for each row
tn <- geom_segment(aes(x= speaker.x, y= speaker.y, xend = receiver.x, yend = receiver.y), arrow = arrow(),
data =talking[n, ,drop = FALSE])
print(base + tn)
ani.pause()
}
}
use saveGIF(pp(), interval = 0.1)
to export a GIF animation etc