0

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?

GiacomoDB
  • 369
  • 1
  • 10
  • 1
    You can create a series of png files from different angles using `snapshot3d` then convert the png files into a gif using `gifski`. There are many fine examples on Stephane Laurent's blog, for example [this page](https://laustep.github.io/stlahblog/posts/constantSpeedAnimation.html) – Allan Cameron Jul 06 '23 at 15:36
  • 1
    There's also the `movie3d` function in `rgl`. – user2554330 Jul 08 '23 at 11:30
  • I tried with the example of Stephane but I was not able to understand it 100%. Easier was movie3D, which gave me the solution. I have only to learn how to define the angles I am looking for. Thank you all for your help! – GiacomoDB Jul 08 '23 at 17:22
  • 1
    One way to do that is to manually rotate the display, then save the direction using `M1 <- par3d("userMatrix")`, rotate it again, save `M2`, etc. Then `list(M1, M2, M3)` can be used as the `userMatrix` argument in `par3dinterp`. – user2554330 Jul 08 '23 at 18:43

1 Answers1

1

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 )

enter image description here

GiacomoDB
  • 369
  • 1
  • 10