## Animations generated in R version 4.3.1 (2023-06-16
##   ucrt) using the package animation
library(plotly)
library(tidyr)
library(ggplot2)
library(animation)
library(circular)
library(htmlwidgets)
oopt = ani.options(interval = 0.2)
set.seed(1234)
x <- rvonmises(n = 1000, mu = circular(pi/5), kappa = 3)
y <- rvonmises(n = 1000, mu = circular(-pi/3), kappa = 5)
z <- rvonmises(n = 1000, mu = circular(0), kappa = 10)
r <- runif(n = 1000, min = 0, max = 2)
vectorX = as.vector(x)
vectorY = as.vector(y)
vectorZ = as.vector(z)
plotX = c(vectorX[1])
plotY = c(vectorY[1])
plotZ = c(vectorZ[1])
for (i in 2:1000) {
    plotX = c(plotX, vectorX[i])
    plotY = c(plotY, vectorY[i])
    plotZ = c(plotZ, vectorZ[i])
    tempX = circular(plotX)
    tempY = circular(plotY)
    tempZ = circular(plotZ)
    resx <- density(tempX, bw = 25, xaxt = "n", yaxt = "n")
    resy <- density(tempY, bw = 25, xaxt = "n", yaxt = "n")
    resz <- density(tempZ, bw = 25, xaxt = "n", yaxt = "n")
    res <- plot(resx, points.plot = TRUE, xlim = c(-1.1, 
        1.5), ylim = c(-1.5, 1.5), main = "Trivariate random sampling of\n kime-magnitudes (times) and kime-directions (phases)", 
        offset = 0.9, shrink = 1, ticks = T, lwd = 3, axes = F, 
        stack = TRUE, bins = 150)
    lines(resy, points.plot = TRUE, col = 2, points.col = 2, 
        plot.info = res, offset = 1, shrink = 1.45, lwd = 3, 
        stack = T)
    lines(resz, points.plot = TRUE, col = 3, points.col = 3, 
        plot.info = res, offset = 1.1, shrink = 1.2, lwd = 3, 
        stack = T)
    segments(0, 0, r[i] * cos(vectorX[i]), r[i] * sin(vectorX[i]), 
        lwd = 2, col = "black")
    segments(0, 0, r[i] * cos(vectorY[i]), r[i] * sin(vectorY[i]), 
        lwd = 2, col = "red")
    segments(0, 0, r[i] * cos(vectorZ[i]), r[i] * sin(vectorZ[i]), 
        lwd = 2, col = "green")
    ani.pause()
}
saveVideo({
    oopt = ani.options(interval = 0.2)
    set.seed(1234)
    x <- rvonmises(n = 1000, mu = circular(pi/5), kappa = 3)
    y <- rvonmises(n = 1000, mu = circular(-pi/3), kappa = 5)
    z <- rvonmises(n = 1000, mu = circular(0), kappa = 10)
    r <- runif(n = 1000, min = 0, max = 2)
    vectorX = as.vector(x)
    vectorY = as.vector(y)
    vectorZ = as.vector(z)
    plotX = c(vectorX[1])
    plotY = c(vectorY[1])
    plotZ = c(vectorZ[1])
    for (i in 2:1000) {
        plotX = c(plotX, vectorX[i])
        plotY = c(plotY, vectorY[i])
        plotZ = c(plotZ, vectorZ[i])
        tempX = circular(plotX)
        tempY = circular(plotY)
        tempZ = circular(plotZ)
        resx <- density(tempX, bw = 25, xaxt = "n", yaxt = "n")
        resy <- density(tempY, bw = 25, xaxt = "n", yaxt = "n")
        resz <- density(tempZ, bw = 25, xaxt = "n", yaxt = "n")
        res <- plot(resx, points.plot = TRUE, xlim = c(-1.1, 
            1.5), ylim = c(-1.5, 1.5), main = "Trivariate random sampling of\n kime-magnitudes (times) and kime-directions (phases)", 
            offset = 0.9, shrink = 1, ticks = T, lwd = 3, 
            axes = F, stack = TRUE, bins = 150)
        lines(resy, points.plot = TRUE, col = 2, points.col = 2, 
            plot.info = res, offset = 1, shrink = 1.45, lwd = 3, 
            stack = T)
        lines(resz, points.plot = TRUE, col = 3, points.col = 3, 
            plot.info = res, offset = 1.1, shrink = 1.2, 
            lwd = 3, stack = T)
        segments(0, 0, r[i] * cos(vectorX[i]), r[i] * sin(vectorX[i]), 
            lwd = 2, col = "black")
        segments(0, 0, r[i] * cos(vectorY[i]), r[i] * sin(vectorY[i]), 
            lwd = 2, col = "red")
        segments(0, 0, r[i] * cos(vectorZ[i]), r[i] * sin(vectorZ[i]), 
            lwd = 2, col = "green")
        ani.pause()
    }
}, video.name = "C:/Users/Dinov/Desktop/KimeRandomSamplingAnimation.mp4", 
    other.opts = "-pix_fmt yuv420p -b:v 500K", ffmpeg = "E:/Ivo.dir/Ivo_Tools/ffmpeg-20200218-ebee808-win64-static/bin/ffmpeg.exe", 
    ani.width = 600, ani.height = 600)
## R version 4.3.1 (2023-06-16 ucrt)
## Platform: x86_64-w64-mingw32/x64 (64-bit)
## Other packages: plotly 4.10.3, tidyr 1.3.0, ggplot2 3.4.4, animation
## 2.7, circular 0.5-0, htmlwidgets 1.6.3