简体   繁体   中英

Create animation or GIF with point plots in R

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

I am using ggplot2 to create the plots comprising year gaps. Here is the code I am using to plot the variation from 1945 to 1980:

`ggplot() +
  geom_map(data = world, map = world,aes(long, lat, map_id = region),color = "seashell2", fill = "seashell", size = 0.3, alpha=0.9)+
  geom_point(data = mapa_variacao_anual_45_80,aes(Long, Lat, color = med_turb),size=2, shape=16, position = position_jitter(width = 8)) + 
  labs(title = "1945 to 1980")+
  theme(plot.title = element_text(hjust = 0.5))+
  scale_colour_gradient( low = "darkgreen",    high = "red")+
  xlab("Longitude") + ylab("Latitude")+
  theme(legend.title= element_blank())+
  theme(panel.background = element_rect(fill = 'aliceblue', colour = 'gray'))`

My plan is to have several plots with determined year ranges and in the end combine all of them in sequence to show temporal variation. Is there an easy way to combine the plots? I have been looking for solutions online but they seem not to suit my goal or are just too complicated.

Thanks in advance for any help.

You could get gganimate to handle the animation for you:

library(ggplot2)
library(gganimate)

world <- map_data("world")

p <- ggplot() +
  geom_map(data = world, map = world,
           aes(long, lat, map_id = region),
           color = "seashell2", fill = "seashell", size = 0.3, alpha = 0.9)+
  geom_point(data = mapa_variacao_anual_45_80, 
             aes(Long, Lat, color = med_turb),
             size = 2, shape = 16, position = position_jitter(width = 8)) + 
  labs(title = "1945 to 1980")+
  theme(plot.title = element_text(hjust = 0.5))+
  scale_colour_gradient( low = "darkgreen", high = "red") +
  xlab("Longitude") + 
  ylab("Latitude")+
  theme(legend.title= element_blank())+
  theme(panel.background = element_rect(fill = 'aliceblue', colour = 'gray')) +
  transition_events(seq(nrow(mapa_variacao_anual_45_80)) ,
                    enter = 1L, exit = 1L) +
  enter_fade() +
  exit_fade()

anim_save("map.gif", p, device = "ragg_png", duration = 3, fps = 30,
          width = 900, height = 450)

在此处输入图像描述

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("myplot%03d.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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM