````{CREATING ANIMATION}

load the iris data

data("iris")
attach(iris)

creaegating a statis ggplot of the data using Petal lenght and width

library(ggplot2)
a<- ggplot(iris, aes(x=Petal.Width, y=Petal.Length)) +
  geom_point()+
  theme()
plot(a)

## create animation object and using the gganimate packages, it does it itself.

library(gganimate)
anim <- a +
  transition_states(iris$Species, 
                    transition_length = 2,
                    state_length = 1)
anim

## Easing when transition_static() calculates intermediary data for the tweening, it needs to decide how th change from one value to another should progress. this concept known as Easing. the default easing is linear. Setting easing is done with the ease_aes () function.

anim +
  ease_aes('cubic-in-out') # slow start and end for the smoother look

# ease_eas() it defines the velocity with which aesthetics change and animation.

anim +
  ease_aes(y='bounce-out') # set special ease for y aesthetic

# Labelling: like all other charts, its hard to understand animated chart without a label. gganimate provides a way of adding labels by setting a et of varibles for each frame, which can be inserted into plot labels using glue syntax.

anim +
  ggtitle('Now Showing {closest_state}', 
          subtitle = 'Frame {frame} of {nframes}')

using the glue syntax to insert frame values in plot labels and titles. Different transition provide different variables, closest_state only makes sense for transition_state() and is thus only available when that transition is used. ## Object perfomance in the anims above, it appears as if data in a single measurement changes gradually as the flower being measured on somehow morphs between three different iris species. this is not exatly how Fishers conducted the experiment and got the numbers. In general, when you make an animation, graphic elements should only transition between instances of the same underlying phenomenon.to explain this phenomenom understandable, consider the line plot equivalent to the crated animation below.

ggplot(iris, aes(x=Petal.Width, y=Petal.Length)) +
  geom_line(aes(group = rep(1:50, 3)), colour ='grey')+
  geom_point()

We can also add aesthetic that distinguish the different species using unique colors for each.

ggplot(iris, aes(x=Petal.Width, y=Petal.Length)) +
  geom_point(aes(colour = Species))+
  transition_states(Species, 
                    transition_length = 2,
                    state_length = 1)

2. set the group directly

ggplot(iris, aes(x=Petal.Width, y=Petal.Length)) +
  geom_point(aes(colour = Species, group=seq_along(Species)))+
  transition_states(Species, 
                    transition_length = 2,
                    state_length = 1)

In general 2, is prefered as it makes the intend explicit. it also makes it possible to match data with different discrete aesthetics such as keeping transition while having color as shown below:

ggplot(iris, aes(x=Petal.Width, y=Petal.Length)) +
  geom_point(aes(colour = Species, group=1L))+
  transition_states(Species, 
                    transition_length = 2,
                    state_length = 1)

## Enter and Exit method: to make your animation attention grabber. you may need to change the appearance and disapperance of data. the enter and exit functions are used. these functins are responsible for modifying the state of appearing(entering) and disappearing(exiting) data. by this, the animation can tween from and to te new state. see the animation below

anim <- ggplot(iris, aes(x=Petal.Width, y=Petal.Length)) +
  geom_point(aes(colour=Species), size =2 )+
  transition_states(Species, 
                    transition_length = 2,
                    state_length = 1)


anim +
  enter_fade()+
  exit_shrink()

enter and exit funtions are used to modify the aesthetics of appearing and disappearing data so that their entrance and exit may be animated. However, gganimate comes with a range of different functions, using enter_manual() and exit_manual() fucntions can be used to create ones preference. enter and exit functions are composable though,

anim+
  enter_fade() + enter_drift(x_mod = -1)+
  exit_shrink() + exit_drift(x_mod = 5)

## Rendering To be able to print this animation and maintain their automatic transition, as gganimate`s model for an animation is dimensionless in the same way as ggplot2 describe plots independent of the final width and hieght of the plot. this means that the final number of frames and its frame-rate are only ever given hen you as gganimate to render the animation. when you print an animation object the animate() function is called on the animation with default arguments: -nframes** sets the number pf frames(d=100) -fpssets the number of frames(d=10) -dev sets the device tp render each frame(d=png) -renderer**sets the function used to combine each frame into an animate (d=gifski_renderer()), below are how to render a video clip

# video output
animate(anim +
          enter_fade()+exit_fly(y_loc = 1),
  renderer = av_renderer()
        )