# Load required libraries
library(ggplot2)
library(gganimate)

# Create a static plot
p <- 
  ggplot(
    iris, 
    aes(
      x = Sepal.Length,
      y = Sepal.Width, 
      color = Species)
    ) +
  geom_point() +
  labs(
    title = 'Iris Sepal Measurements'
    )

# Animate the plot by transitioning through Species
anim <- 
  p + transition_states(
    Species, 
    transition_length = 2, 
    state_length = 1
    ) +
  enter_fade() + 
  exit_fade()

# Render the animation
animate(
  anim,
  nframes = 100,
  fps = 10,
  renderer = gifski_renderer()
) ->
anim_out

View the animation

anim_out

Save the animation as a gif file

anim_save(
  filename = "test1.gif",
  anim_out
)