library(ggplot2)
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(gganimate)
theme_set(theme_bw())
library(gapminder)
library(devtools)
## Loading required package: usethis
library(ggsankey)


p <- ggplot(
  gapminder, 
  aes(x = gdpPercap, y=lifeExp, size = pop, colour = country)
) +
  geom_point(show.legend = FALSE, alpha = 0.7) +
  scale_color_viridis_d() +
  scale_size(range = c(2, 12)) +
  scale_x_log10() +
  labs(x = "GDP per capita", y = "Life expectancy")
p

p + transition_time(year) +
  labs(title = "Year: {frame_time}")

p + transition_time(year) +
  labs(title = "Year: {frame_time}") +
  view_follow(fixed_y = TRUE)

year=c(1,2,3,4,5,1,2,3,4,5)
cum_prop=c(0.10,0.12,0.15,0.16,0.22,0.05,0.07,0.10,0.12,0.12)
cohort=as.factor(c("73-78","73-78","73-78","73-78","73-78","89-95","89-95","89-95","89-95","89-95"))
d=data.frame(year,cum_prop,cohort)

r <- ggplot(d, aes(year, cum_prop, fill = cum_prop)) +
  geom_col() +
  scale_fill_distiller(palette = "Reds", direction = 1) +
  theme_minimal() +
  facet_wrap(~cohort,nrow=2)+
  theme(
    panel.grid = element_blank(),
    panel.grid.major.y = element_line(color = "white"),
    panel.ontop = TRUE
  )
r

r1=r + transition_states(year, wrap = TRUE)+shadow_mark()
animate(r1,height=600,width=900)

anim_save("r1.gif")

r1+ease_aes(y='bounce-out')

r2=r+facet_wrap(~cohort)+transition_states(year,wrap= TRUE,transition_length=2,state_length = 1)+ 
  shadow_mark()
animate(r2,height=600,width=900)

anim_save("r2.gif")


r2+ease_aes(y='bounce-out')

r3=r + transition_reveal(year)
animate(r3,height=600,width=900)

anim_save("r3.gif")
p2 <- ggplot(
  airquality,
  aes(Day, Temp, group = Month, color = factor(Month))
) +
  geom_line() +
  scale_color_viridis_d() +
  labs(x = "Day of Month", y = "Temperature") +
  theme(legend.position = "top")
p2

p2 + transition_reveal(Day)

p2 + 
  geom_point(aes(group = seq_along(Day))) +
  transition_reveal(Day)

anim2<- ggplot(iris,aes(x=Petal.Width, y=Petal.Length))+
  geom_point(aes(colour=Species))+
  transition_states(Species,transition_length = 2, state_length = 1)
anim2+enter_fade()+
  exit_shrink()+
  ease_aes('sine-in-out')