π Animaciones
Contents
π Animaciones#
Existen algunos cuentos paquetes para crear animaciones en R, vamos a usar magick
## En jupyer-lab los graficos por defecto se muestran en un cuadrado de 7 x 7
## para poder cambiar el largo y ancho del grafico uso la siguinete funcion
dim.plot <- function(x, y) options(repr.plot.width=x, repr.plot.height=y)
### Nerd Fonts https://www.nerdfonts.com/cheat-sheet tiene un conjunto amplio de "Iconos"
### que podemos usar para mejorar nuestros graficos
### En Windows y usando Jupyter es un poco diferente el uso de estos, checar: https://github.com/IRkernel/repr/issues
windowsFonts(serif = "Times New Roman",
sans = "Arial",
mono = "Courier New",
A = "UbuntuMono Nerd Font",
B = "Comic Sans MS")
library(magick) ## Cargamos el paquete
Linking to ImageMagick 6.9.12.3
Enabled features: cairo, freetype, fftw, ghostscript, heic, lcms, pango, raw, rsvg, webp
Disabled features: fontconfig, x11
Paisaje#
Vamos a contruir un paisaje sencillo donde se ve avanzar un camion
n <- 10^2
x <- seq(-2*pi, 2*pi, length.out = n )
y1 <- rep(-1.3, n)
y2 <- sin(x)
png('car%02d.png', width = 15, height = 4, units = "in", res = 72)
par(bg = "#B3E5FC", mar = c(0,0,0,0))
for (i in seq(1, n, by = 2)) {
plot(x, sin(x), ylim = c(-1.3, 1.7), axes = "F", ylab = "", xlab = "", type = "l", lwd = 1.4)
polygon(c(x, rev(x)), c(y1, rev(y2)),col = "#6BD7AF")
text(-pi/2, 1.3 , "\uf0c2", family = "A", cex = 5, col = "#2196F3")
text(x[i], sin(x[i]) + 0.2 , "\ufc9d", family = "A", cex = 3.5, col = "#FF6D00")
}
dev.off()
png: 2
Al final unimos las imagenes para formar un gif
imgs <- list.files(getwd(), full.names = TRUE)
imgs <- imgs[ grep("png", imgs) ]
img_list <- lapply(imgs, image_read)
img_joined <- image_join(img_list)
img_animated <- image_animate(img_joined, fps = 10)
img_animated
# A data frame: 50 x 7
format width height colorspace matte filesize density
<chr> <int> <int> <chr> <lgl> <int> <chr>
1 gif 1080 288 sRGB FALSE 0 28x28
2 gif 1080 288 sRGB TRUE 0 28x28
3 gif 1080 288 sRGB TRUE 0 28x28
4 gif 1080 288 sRGB TRUE 0 28x28
5 gif 1080 288 sRGB TRUE 0 28x28
6 gif 1080 288 sRGB TRUE 0 28x28
7 gif 1080 288 sRGB TRUE 0 28x28
8 gif 1080 288 sRGB TRUE 0 28x28
9 gif 1080 288 sRGB TRUE 0 28x28
10 gif 1080 288 sRGB TRUE 0 28x28
# ... with 40 more rows
file.remove(list.files(pattern=".png")) ## borramos los archivos png
- TRUE
- TRUE
- TRUE
- TRUE
- TRUE
- TRUE
- TRUE
- TRUE
- TRUE
- TRUE
- TRUE
- TRUE
- TRUE
- TRUE
- TRUE
- TRUE
- TRUE
- TRUE
- TRUE
- TRUE
- TRUE
- TRUE
- TRUE
- TRUE
- TRUE
- TRUE
- TRUE
- TRUE
- TRUE
- TRUE
- TRUE
- TRUE
- TRUE
- TRUE
- TRUE
- TRUE
- TRUE
- TRUE
- TRUE
- TRUE
- TRUE
- TRUE
- TRUE
- TRUE
- TRUE
- TRUE
- TRUE
- TRUE
- TRUE
- TRUE
## image_write(img_animated, path = "car.gif", format = "gif") ## puedes guardar el gif