πŸ”­ 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
  1. TRUE
  2. TRUE
  3. TRUE
  4. TRUE
  5. TRUE
  6. TRUE
  7. TRUE
  8. TRUE
  9. TRUE
  10. TRUE
  11. TRUE
  12. TRUE
  13. TRUE
  14. TRUE
  15. TRUE
  16. TRUE
  17. TRUE
  18. TRUE
  19. TRUE
  20. TRUE
  21. TRUE
  22. TRUE
  23. TRUE
  24. TRUE
  25. TRUE
  26. TRUE
  27. TRUE
  28. TRUE
  29. TRUE
  30. TRUE
  31. TRUE
  32. TRUE
  33. TRUE
  34. TRUE
  35. TRUE
  36. TRUE
  37. TRUE
  38. TRUE
  39. TRUE
  40. TRUE
  41. TRUE
  42. TRUE
  43. TRUE
  44. TRUE
  45. TRUE
  46. TRUE
  47. TRUE
  48. TRUE
  49. TRUE
  50. TRUE
## image_write(img_animated, path = "car.gif", format = "gif")   ## puedes guardar el gif