----- Forwarded message from [email protected] -----
Date: Mon, 11 Jun 2012 09:24:32 -0400
From: [email protected]
Reply-To: [email protected]
Subject: creating and saving animated gif of shape change in R
To: [email protected]
Hi all,
in a past post Paola Nicolosi asked if it was possible
to export animations from tps for the visualization of
shape change associated to RWs (or PCscores).
I created a procedure for that in R by:
-slighlty adapting to 2d the function lineplot3() of
the package Morpho (Schlager, 2012) to visualize links
-using a procSym object of the package Morpho
-using tpsgrid() function of the package shapes
(Dryden, 2012)
-using saveMovie() of the package animation
1-install the packages "Morpho", "shapes" and "animation"
2-load the source (in attach) "vischange2d.r" where
the needed functions are present
3-create a links list,i.e.just a list of vectors of
pairs of landmarks to link each other, i.e.
mylinks<-list(c(1,2),c(2,3)....etc)
the function vischange2d() wants 5 arguments: a
procSym object, an integer indicating the n° of PC to
visualize, an integer indicating the magnification for
shape change, an integer indicating in how many frames
you want to visualize such change and the object of
links.
COPY AND PAST IN R ALL THAT BELOW TO TEST THE
PROCEDURE USING RANDOM DATA
put the source "vischange2d.r" in the desired working
R directory
library(shapes)
library(animation)
library(Morpho)
source("vischange2d.r")
#create an array 100 random squares
x1<-jitter(rep(1,100),factor=3)
y1<-jitter(rep(2,100),,factor=3)
x2<-jitter(rep(2,100))
y2<-jitter(rep(2,100))
x3<-jitter(rep(2,100),,factor=3)
y3<-jitter(rep(1,100),,factor=3)
x4<-jitter(rep(1,100))
y4<-jitter(rep(1,100))
mat<-cbind(x1,y1,x2,y2,x3,y3,x4,y4)
squares<-read.inn(mat,4,2)## END CREATION RANDOM SQUARES
links<-list(c(1,2),c(2,3),c(3,4),c(4,1)) #create links
myprocobject<-procSym(squares)# perform GPA
vischange2d(myprocobject,1,2,20,links) # create animation
saveMovie(vischange2d(myprocobject,1,2,20,links),movie.name="myanimation.gif",interval=0.2,outdir
= getwd()) #save animation
## adjust the "interval" parameter to obtain the
desired speed of the animated gif
--
Paolo Piras
Center for Evolutionary Ecology
and
Dipartimento di Scienze Geologiche, UniversitĂ Roma Tre
Largo San Leonardo Murialdo, 1, 00146 Roma
Tel: +390657338000
email: [email protected]
----- End forwarded message -----
vischange2d<-function(proc.fit,pcn,mag,frames,links){
for(i in seq(-mag,mag,length.out=frames)){
sd<-sd(proc.fit$PCscores[,pcn]) ##get sd of first PC scores
shapeposition<-proc.fit$mshape+i*sd*matrix(proc.fit$PCs[,pcn],length(proc.fit$PCs[,pcn])/2,2)
shapemax<-proc.fit$mshape+max(seq(-mag,mag,length.out=frames))*sd*matrix(proc.fit$PCs[,pcn],length(proc.fit$PCs[,pcn])/2,2)
tpsgrid(shapemax,shapeposition,opt=1,ngrid=15,ext=0.1)
lineplot2(shapeposition,links,col=1,lwd=2)
}
}
lineplot2<-function (x, point, col = 1, lwd = 1, line_antialias = FALSE)
{
if (is.list(point) == TRUE) {
for (i in 1:length(point)) {
lines(x[point[[i]], 1], x[point[[i]], 2], col = col, lwd = lwd,
line_antialias = line_antialias)
}
}
else {
lines(x[point, 1], x[point, 2], col = col,
lwd = lwd, line_antialias = line_antialias)
}
}
### "k" equals the number of landmarks, while "m" the number of dimensions
read.inn<-function (matrix, k, m)
{
vectorr<-as.vector(t(matrix))
tem <- vectorr
n <- length(tem)/(k * m)
tem <- array(tem, c(m, k, n))
tem <- aperm(tem, c(2, 1, 3))
x <- tem
x
}