JRC
JRC

Reputation: 170

Plotting 3D GIF in R

I was working with 3 vectors x , y , z , each of length N (Assume N to be some large natural number, say 20000). In order to visualize this, I was able to plot this easily using the following R code :

library("plot3D")
lines3D(x, y, z, type = "l")

Now, I was thinking if we can make a little 3D animation (i.e. a 3D GIF) from the vectors x , y , z. Is it possible in R ?


NOTE : I've previously done 2D GIFs in R, with the help of packages like ggplot2 , gganimate , magick etc. However, I'm curious whether the same thing can be done for 3D data. Thanks in advance.

Upvotes: 0

Views: 420

Answers (1)

Elia
Elia

Reputation: 2584

It is very simple to create .gif with the animation package (note it requires ‘ImageMagick’ or ‘GraphicsMagick’ to run). After the installation, you could do something like this (I assume that you want to display your plot with different point of view):

library(plot3D)
library(animation)
x <- runif(1000)
y <- x*2+runif(1000)
z <- sample(1:10,length(x),replace = T)*x/y

ani.options(interval=0.5,nmax=35)
saveGIF(for(t in seq(0,360,10)){
lines3D(x, y, z, type = "l",theta=t)
}, movie.name = "animation.gif")

Upvotes: 2

Related Questions