Home > Blockchain >  Create animation or GIF with point plots in R
Create animation or GIF with point plots in R

Time:07-29

I am trying to create an animation or GIF that shows the evolution of an environmental condition over time. Basically, I have a dataset (example below) with year, value of the environmental condition, unit, and coordinates.

year condition unit Lat Long
1945 -0.120148 TSS 41.36531 41.67889
1948 0.274646 TSS 30.45368 -87.99042
1948 0.074794 TSS 30.45368 -87.99042
1975 -0.102050 TSS 38.10541 -122.06782
1979 -0.169886 NTU 29.77048 -84.91630

Complete dataset: enter image description here

CodePudding user response:

You can create a series of png files and assemble them into an animation with the gifski package:

library(ggplot2)
library(gifski)

for(i in 1:30){
  gg <- ggplot(......)
  ggsave(sprintf("myplotd.png", i), gg)
}

png_files <- Sys.glob("myplot*.png")

gifski(
  png_files,
  "myanimation.gif",
  width = 400, height = 400,
  delay = 1/5 # 5 images per second
)

file.remove(png_files)
  • Related