Animated 3D scatterplot and save it as gif in R

Viewed 399

I want to plot a animated 3D scatterplot and save it as gif. I followed the code provided by the R Graph Gallery example: https://www.r-graph-gallery.com/3-r-animated-cube.html.

library(rgl)
library(magick)
options(rgl.printRglwidget = TRUE)

# Let's use the iris dataset
# iris

# This is ugly
colors <- c("royalblue1", "darkcyan", "oldlace")
iris$color <- colors[ as.numeric( as.factor(iris$Species) ) ]

# Static chart
plot3d( iris[,1], iris[,2], iris[,3], col = iris$color, type = "s", radius = .2 )

# We can indicate the axis and the rotation velocity
play3d( spin3d( axis = c(0, 0, 1), rpm = 20,dev = cur3d()),startTime = 0, duration = 10 )
# Save like gif
movie3d(
  movie="3dAnimatedScatterplot", 
  spin3d( axis = c(0, 0, 1), rpm = 20,dev = cur3d()),
  startTime = 0,
  duration = 10, 
  dir = ".",
  type = "gif", 
  clean = T,
  fps=10,
  convert=T
)

plot3d was successed output a 3d scatter plot. Static 3d scatter plot

But the final output: 3dAnimatedScatterplot.gif,just a black image 3dAnimatedScatterplot.gif

when I set clean=F, all frame images are black. So, I guess the play3d() was not working.

Can anyone provide any help to me ? Thanks a lot !

1 Answers

Most likely snapshot3d isn't working for you. Try it with the option webshot = FALSE instead of the default webshot = TRUE. That uses a different mechanism for saving the image.

Related