Re: [R] Creating Movies with R

2006-09-26 Thread Romain Francois
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,1000)
 }
   
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 t0, 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

Re: [R] Creating Movies with R

2006-09-25 Thread Gregor Gorjanc
Hello!

J.R. Lockwood lockwood at rand.org writes:
 
 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,1000)
 }
 

RWiki[1] would be a very nice place for such explanation. I am looking forwrad 
to it!

Gregor

[1]http://wiki.r-project.org/

__
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.


Re: [R] Creating Movies with R

2006-09-23 Thread Henrik Bengtsson
On 9/22/06, Gabor Grothendieck [EMAIL PROTECTED] wrote:
 See the flag= argument on formatC:

 n - 10
 formatC(1:n, dig = nchar(n)-1, flag = 0)

 # Here is another way:

 n - 10
 sprintf(paste(%0, nchar(n), .0f, sep = ), 1:n)

sprintf(%0*.0f, nchar(n), 1:n)

or even

sprintf(%0*d, nchar(n), 1:n)

/H


 On 9/22/06, J.R. Lockwood [EMAIL PROTECTED] 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,1000)
  }
 
 
  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 t0, 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

[R] Creating Movies with R

2006-09-22 Thread Lorenzo Isella
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 t0, 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.


Re: [R] Creating Movies with R

2006-09-22 Thread David Barron
You can make animated gifs using the write.gif function in the caTools package.

On 22/09/06, Lorenzo Isella [EMAIL PROTECTED] 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 t0, 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 

Re: [R] Creating Movies with R

2006-09-22 Thread Duncan Murdoch
On 9/22/2006 3:23 AM, 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.

There's an example of an animation in the rgl package (see 
?rgl.snapshot); it needs ImageMagick to put the frames together.  It 
supports GIF; I think it also supports MPEG and maybe some other 
animated formats.

Duncan Murdoch

 
 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 t0, 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()
 
 
 
 

Re: [R] Creating Movies with R

2006-09-22 Thread TEMPL Matthias
There is also a write.gif function in package caTools.
See the example there.

Best, 
Matthias

 
 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 t0, 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

Re: [R] Creating Movies with R

2006-09-22 Thread Jeffrey Horner
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 t0, 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 

Re: [R] Creating Movies with R

2006-09-22 Thread J.R. Lockwood
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,1000)
}


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 t0, 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

Re: [R] Creating Movies with R

2006-09-22 Thread Gabor Grothendieck
See the flag= argument on formatC:

n - 10
formatC(1:n, dig = nchar(n)-1, flag = 0)

# Here is another way:

n - 10
sprintf(paste(%0, nchar(n), .0f, sep = ), 1:n)


On 9/22/06, J.R. Lockwood [EMAIL PROTECTED] 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,1000)
 }


 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 t0, 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