Graphics Reference
In-Depth Information
Animating a Three-Dimensional Plot
Problem
You want to animate a three-dimensional plot by moving the viewpoint around the plot.
Solution
Rotating a 3D plot can provide a more complete view of the data. To animate a 3D plot, use
play3d() with spin3d() :
library(rgl)
plot3d(mtcars$wt, mtcars$disp, mtcars$mpg, type = "s" , size = 0.75 , lit = FALSE
FALSE )
play3d(spin3d())
Discussion
By default, the graph will be rotated on the z (vertical) axis, until you send a break command to
R.
You can change the rotation axis, rotation speed, and duration:
# Spin on x-axis, at 4 rpm, for 20 seconds
play3d(spin3d(axis = c( 1 , 0 , 0 ), rpm = 4 ), duration = 20 )
To save the movie, use the movie3d() function in the same way as play3d() . It will generate a
series of .pngiles, one for each frame, and then attempt to combine them into a single animated
.gifile using the convert program from the ImageMagick image utility.
This will spin the plot once in 15 seconds, at 50 frames per second:
# Spin on z axis, at 4 rpm, for 15 seconds
movie3d(spin3d(axis = c( 0 , 0 , 1 ), rpm = 4 ), duration = 15 , fps = 50 )
The output file will be saved in a temporary directory, and the name will be printed on the R
console.
If you don't want to use ImageMagick to convert the output to a .gif, you can specify conver-
t=FALSE and then convert the series of .pngiles to a movie using some other utility.
Search WWH ::




Custom Search