J.R. Lockwood wrote:
> An alternative that I've used a few times is the jpg() function to
> create the sequence of images, and then converting these to an mpeg
> movie using "mencoder" distributed with "mplayer".  This works on both
> windows and linux.  I have a pretty self-contained example file
> written up that I can send to anyone who is interested.  Oddly, the
> most challenging part was creating a sequence of file names that would
> be correctly ordered - for this I use:
>
> lex <- function(N){
>   ## produce vector of N lexicograpically ordered strings
>   ndig <- nchar(N)
>   substr(formatC((1:N)/10^ndig,digits=ndig,format="f"),3,10000000)
> }
>   
Hi,

Or you could have asked the `filename` argument of `jpeg` to do the job 
for you, ie :
filename = "something%04d" as documented in ?jpeg

jpeg(filename = "something%04.jpg", onefile = FALSE)
for(i in 1:10){
  plot(i)
}

Cheers,

Romain

PS : For those who have ideas of "movies", I once started a website "R 
Movies Gallery" as a little sister of "R Graph(ics) Gallery" ... you may 
want to send me code to populate the website, or populate the wiki with 
such examples. The idea is not to produce pretty science fiction type 
movies with R, but use R abilities to create some useful animation that 
could highlight some statistical concepts such as the LCT, ...
Plus, there's a place where grid could show its full power.

> On Fri, 22 Sep 2006, Jeffrey Horner wrote:
>
>   
>> Date: Fri, 22 Sep 2006 13:46:52 -0500
>> From: Jeffrey Horner <[EMAIL PROTECTED]>
>> To: Lorenzo Isella <[EMAIL PROTECTED]>, r-help@stat.math.ethz.ch
>> Subject: Re: [R] Creating Movies with R
>>
>> If you run R on Linux, then you can run the ImageMagick command called 
>> convert. I place this in an R function to use a sequence of PNG plots as 
>> movie frames:
>>
>> make.mov.plotcol3d <- function(){
>>      unlink("plotcol3d.mpg")
>>      system("convert -delay 10 plotcol3d*.png plotcol3d.mpg")
>> }
>>
>> Examples can be seen here:
>>
>> http://biostat.mc.vanderbilt.edu/JrhRgbColorSpace
>>
>> Look for the 'Download Movie' links.
>>
>> Cheers,
>>
>> Jeff
>>
>> Lorenzo Isella wrote:
>>     
>>> Dear All,
>>>
>>> I'd like to know if it is possible to create animations with R.
>>> To be specific, I attach a code I am using for my research to plot
>>> some analytical results in 3D using the lattice package. It is not
>>> necessary to go through the code.
>>> Simply, it plots some 3D density profiles at two different times
>>> selected by the user.
>>> I wonder if it is possible to use the data generated for different
>>> times to create something like an .avi file.
>>>
>>> Here is the script:
>>>
>>> rm(list=ls())
>>> library(lattice)
>>>
>>> # I start defining the analytical functions needed to get the density
>>> as a function of time
>>>
>>> expect_position <- function(t,lam1,lam2,pos_ini,vel_ini)
>>> {1/(lam1-lam2)*(lam1*exp(lam2*t)-lam2*exp(lam1*t))*pos_ini+
>>> 1/(lam1-lam2)*(exp(lam1*t)-exp(lam2*t))*vel_ini
>>> }
>>>
>>> sigma_pos<-function(t,q,lam1,lam2)
>>> {
>>> q/(lam1-lam2)^2*(
>>> (exp(2*lam1*t)-1)/(2*lam1)-2/(lam1+lam2)*(exp(lam1*t+lam2*t)-1) +
>>> (exp(2*lam2*t)-1)/(2*lam2) )
>>> }
>>>
>>> rho_x<-function(x,expect_position,sigma_pos)
>>> {
>>> 1/sqrt(2*pi*sigma_pos)*exp(-1/2*(x-expect_position)^2/sigma_pos)
>>> }
>>>
>>> #### Now the physical parameters
>>> tau<-0.1
>>> beta<-1/tau
>>> St<-tau ### since I am in dimensionless units and tau is already in
>>> units of 1/|alpha|
>>> D=2e-2
>>> q<-2*beta^2*D
>>> ############### Now the grid in space and time
>>> time<-5  # time extent
>>> tsteps<-501 # time steps
>>> newtime<-seq(0,time,len=tsteps)
>>> #### Now the things specific for the dynamics along x
>>> lam1<- -beta/2*(1+sqrt(1+4*St))
>>> lam2<- -beta/2*(1-sqrt(1+4*St))
>>> xmin<- -0.5
>>> xmax<-0.5
>>> x0<-0.1
>>> vx0<-x0
>>> nx<-101 ## grid intervals along x
>>> newx<-seq(xmin,xmax,len=nx) # grid along x
>>>
>>> # M1 <- do.call("g", c(list(x = newx), mypar))
>>>
>>>
>>> mypar<-c(q,lam1,lam2)
>>> sig_xx<-do.call("sigma_pos",c(list(t=newtime),mypar))
>>> mypar<-c(lam1,lam2,x0,vx0)
>>> exp_x<-do.call("expect_position",c(list(t=newtime),mypar))
>>>
>>> #rho_x<-function(x,expect_position,sigma_pos)
>>>
>>> #NB: at t=0, the density blows up, since I have a delta as the initial 
>>> state!
>>> # At any t>0, instead, the result is finite.
>>> #for this reason I now redefine time by getting rid of the istant t=0
>>> to work out
>>> # the density
>>>
>>>
>>> rho_x_t<-matrix(ncol=nx,nrow=tsteps-1)
>>> for (i in 2:tsteps)
>>> {mypar<-c(exp_x[i],sig_xx[i])
>>> myrho_x<-do.call("rho_x",c(list(x=newx),mypar))
>>> rho_x_t[ i-1, ]<-myrho_x
>>> }
>>>
>>> ### Now I also define a scaled density
>>>
>>> rho_x_t_scaled<-matrix(ncol=nx,nrow=tsteps-1)
>>> for (i in 2:tsteps)
>>> {mypar<-c(exp_x[i],sig_xx[i])
>>> myrho_x<-do.call("rho_x",c(list(x=newx),mypar))
>>> rho_x_t_scaled[ i-1, ]<-myrho_x/max(myrho_x)
>>> }
>>>
>>> ###########Now I deal with the dynamics along y
>>>
>>> lam1<- -beta/2*(1+sqrt(1-4*St))
>>> lam2<- -beta/2*(1-sqrt(1-4*St))
>>> ymin<- 0
>>> ymax<- 1
>>> y0<-ymax
>>> vy0<- -y0
>>>
>>> mypar<-c(q,lam1,lam2)
>>> sig_yy<-do.call("sigma_pos",c(list(t=newtime),mypar))
>>> mypar<-c(lam1,lam2,y0,vy0)
>>> exp_y<-do.call("expect_position",c(list(t=newtime),mypar))
>>>
>>>
>>> # now I introduce the function giving the density along y: this has to
>>> include the BC of zero
>>> # density at wall
>>>
>>> rho_y<-function(y,expect_position,sigma_pos)
>>> {
>>> 1/sqrt(2*pi*sigma_pos)*exp(-1/2*(y-expect_position)^2/sigma_pos)-
>>> 1/sqrt(2*pi*sigma_pos)*exp(-1/2*(y+expect_position)^2/sigma_pos)
>>> }
>>>
>>> newy<-seq(ymin,ymax,len=nx) # grid along y with the same # of points
>>> as the one along x
>>>
>>>
>>> rho_y_t<-matrix(ncol=nx,nrow=tsteps-1)
>>> for (i in 2:tsteps)
>>> {mypar<-c(exp_y[i],sig_yy[i])
>>> myrho_y<-do.call("rho_y",c(list(y=newy),mypar))
>>> rho_y_t[ i-1, ]<-myrho_y
>>> }
>>>
>>> rho_y_t_scaled<-matrix(ncol=nx,nrow=tsteps-1)
>>> for (i in 2:tsteps)
>>> {mypar<-c(exp_y[i],sig_yy[i])
>>> myrho_y<-do.call("rho_y",c(list(y=newy),mypar))
>>> rho_y_t_scaled[ i-1, ]<-myrho_y/max(myrho_y)
>>> }
>>>
>>>
>>> # The following 2 plots are an example of the plots I'd like to use to
>>> make an animation
>>>
>>>
>>> g <- expand.grid(x = newx, y = newy)
>>>
>>> instant<-100
>>> mydens<-rho_x_t[ instant, ]%o%rho_y_t[ instant, ]/(max(rho_x_t[
>>> instant, ]%o%rho_y_t[ instant, ]))
>>>
>>>
>>> lentot<-nx^2
>>> dim(mydens)<-c(lentot,1)
>>>
>>> g$z<-mydens
>>> jpeg("dens-t-3.jpeg")
>>> print(wireframe(z ~ x * y, g, drape = TRUE,shade=TRUE,
>>> scales = list(arrows = FALSE),pretty=FALSE, aspect = c(1,1), colorkey = TRUE
>>> ,zoom=0.8, main=expression("Density at t=2"), zlab =
>>> list(expression("density"),rot = 90),distance=0.0,
>>> perspective=TRUE,#screen = list(z = 150, x = -55,y= 0)
>>> ,zlim=range(c(0,1))))
>>> dev.off()
>>>
>>>
>>> instant<-300
>>> mydens<-rho_x_t[ instant, ]%o%rho_y_t[ instant, ]/(max(rho_x_t[
>>> instant, ]%o%rho_y_t[ instant, ]))
>>>
>>>
>>> lentot<-nx^2
>>> dim(mydens)<-c(lentot,1)
>>>
>>> g$z<-mydens
>>> jpeg("dens-t-3.jpeg")
>>> print(wireframe(z ~ x * y, g, drape = TRUE,shade=TRUE,
>>> scales = list(arrows = FALSE),pretty=FALSE, aspect = c(1,1), colorkey = TRUE
>>> ,zoom=0.8, main=expression("Density at t=3"), zlab =
>>> list(expression("density"),rot = 90),distance=0.0,
>>> perspective=TRUE,#screen = list(z = 150, x = -55,y= 0)
>>> ,zlim=range(c(0,1))))
>>> dev.off()
>>>
>>>
>>>
>>>
>>> Kind Regards
>>>
>>> Lorenzo
>>>
>>> ______________________________________________
>>> R-help@stat.math.ethz.ch mailing list
>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>>> and provide commented, minimal, self-contained, reproducible code.
>>>       
>> -- 
>> http://biostat.mc.vanderbilt.edu/JeffreyHorner
>>
>> ______________________________________________
>> R-help@stat.math.ethz.ch mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>
>>     
>
> J.R. Lockwood
> 412-683-2300 x4941
> [EMAIL PROTECTED]
> http://www.rand.org/statistics/bios/
>
> --------------------
>
> This email message is for the sole use of the intended recip...{{dropped}}
>
> ______________________________________________
> R-help@stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
>   


-- 
*mangosolutions*
/data analysis that delivers/

Tel   +44 1249 467 467
Fax   +44 1249 467 468

______________________________________________
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to