I am facing an issue: I would like to print a .gif file animated of a 3D graph showing the rotation of the graph with a custom defined rotation angle and speed. So as to make it clear, I place the code of the 3D graph which I created:
library(ggplot2)
library(readxl)
Dati <- data.frame(
"% Ampiezza" = c(100, 100, 100, 100, 50, 25, 25),
"% frequenza" = c(25, 50, 75, 100, 25, 25, 50),
"stima quantità applicata" = c(18.12631579, 27.83157895, 42.37894737, 51.57894737, 13.27157895, 8.147368421, 12.58947368)
)
attach(Dati)
library("plot3D")
x <- Ampiezza <- Dati$`X..Ampiezza`
y <- frequenza <- Dati$`X..frequenza`
z <- quantita <- Dati$`stima.quantità.applicata`
### INTERPOLATION DEFINITION
fit <- lm(z ~ x + y)
grid.lines = 26
x.pred <- seq(min(x), max(x), length.out = grid.lines)
y.pred <- seq(min(y), max(y), length.out = grid.lines)
xy <- expand.grid( x = x.pred, y = y.pred)
z.pred <- matrix(predict(fit, newdata = xy),
nrow = grid.lines, ncol = grid.lines)
fitpoints <- predict(fit)
### GRAPHIC PLOTTING
scatter3D(x, y, z, pch = 18, cex = 2,
theta = 20, phi = 20, ticktype = "detailed",
xlab = "% Ampiezza", ylab = "% Frequenza", zlab = "Quantità reale",
surf = list(x = x.pred, y = y.pred, z = z.pred,
facets = NA, fit = fitpoints), main = "Titolo")
At this point, I know how to create a rgl dynamic graph, so as to rotate by clicking with the mouse on it.
library("plot3Drgl")
plotrgl()
The problem is that i would like to create an aniomation and export it as a .gif file showing the rotation continuosly. Is it possible to make this with R?
I solved the problem adding the following code:
library(magick)
library(rgl)
library(webshot2)
M <- par3d("userMatrix")
if (!rgl.useNULL() && interactive())
play3d( par3dinterp(times = (0:2)*0.5, userMatrix = list(M,
rotate3d(M, pi/2, 1, 0, 0),
rotate3d(M, pi/2, 0, 1, 0) ) ),
duration = 2 )
movie3d( spin3d(), duration = 5 )