Animações usando gganimate
Caio VAOS
2024
library(ggplot2)
library(gganimate)
## # A tibble: 6 × 6
## country continent year lifeExp pop gdpPercap
## <fct> <fct> <int> <dbl> <int> <dbl>
## 1 Afghanistan Asia 1952 28.8 8425333 779.
## 2 Afghanistan Asia 1957 30.3 9240934 821.
## 3 Afghanistan Asia 1962 32.0 10267083 853.
## 4 Afghanistan Asia 1967 34.0 11537966 836.
## 5 Afghanistan Asia 1972 36.1 13079460 740.
## 6 Afghanistan Asia 1977 38.4 14880372 786.
## # A tibble: 6 × 4
## code_state geom ano preenchimento
## <chr> <MULTIPOLYGON [°]> <dbl> <int>
## 1 11 (((-63.32721 -7.97672, -62.86662 -7.975868, -6… 2000 96
## 2 11 (((-63.32721 -7.97672, -62.86662 -7.975868, -6… 2010 70
## 3 11 (((-63.32721 -7.97672, -62.86662 -7.975868, -6… 2022 54
## 4 12 (((-73.18253 -7.335496, -72.58477 -7.552004, -… 2000 88
## 5 12 (((-73.18253 -7.335496, -72.58477 -7.552004, -… 2010 79
## 6 12 (((-73.18253 -7.335496, -72.58477 -7.552004, -… 2022 38
## # A tibble: 6 × 6
## Ozone Solar.R Wind Temp Month Day
## <int> <int> <dbl> <int> <int> <int>
## 1 41 190 7.4 67 5 1
## 2 36 118 8 72 5 2
## 3 12 149 12.6 74 5 3
## 4 18 313 11.5 62 5 4
## 5 NA NA 14.3 56 5 5
## 6 28 NA 14.9 66 5 6
## # A tibble: 5 × 2
## Month Temp
## <int> <dbl>
## 1 5 65.5
## 2 6 79.1
## 3 7 83.9
## 4 8 84.0
## 5 9 76.9
## # A tibble: 6 × 5
## x y size time id
## <dbl> <dbl> <int> <dbl> <int>
## 1 0.831 0.498 2 1 1
## 2 0.912 0.182 2 4 1
## 3 0.242 0.941 1 6 1
## 4 0.306 0.994 1 7 1
## 5 0.396 0.0356 2 9 1
## 6 0.360 0.575 1 6 2
## # A tibble: 6 × 5
## Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## <dbl> <dbl> <dbl> <dbl> <fct>
## 1 5.1 3.5 1.4 0.2 setosa
## 2 4.9 3 1.4 0.2 setosa
## 3 4.7 3.2 1.3 0.2 setosa
## 4 4.6 3.1 1.5 0.2 setosa
## 5 5 3.6 1.4 0.2 setosa
## 6 5.4 3.9 1.7 0.4 setosa
## # A tibble: 6 × 11
## mpg cyl disp hp drat wt qsec vs am gear carb
## <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 21 6 160 110 3.9 2.62 16.5 0 1 4 4
## 2 21 6 160 110 3.9 2.88 17.0 0 1 4 4
## 3 22.8 4 108 93 3.85 2.32 18.6 1 1 4 1
## 4 21.4 6 258 110 3.08 3.22 19.4 1 0 3 1
## 5 18.7 8 360 175 3.15 3.44 17.0 0 0 3 2
## 6 18.1 6 225 105 2.76 3.46 20.2 1 0 3 1
## # A tibble: 6 × 6
## x y begin length enter exit
## <int> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 1 0.978 59.9 15.0 5.72 5.62
## 2 2 0.915 87.1 18.7 7.45 7.54
## 3 3 0.896 8.64 17.3 6.52 7.83
## 4 4 0.0122 48.6 17.4 9.91 5.38
## 5 5 0.483 35.4 18.4 5.02 7.87
## 6 6 0.542 45.2 19.6 7.64 9.06
Tipo de variável: Temporal contínua.
Transição: Suave e contínua.
Uso típico: Dados que mudam ao longo do tempo.
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")+
theme_bw()+
transition_time(year) +
labs(title = "Year: {frame_time}")
Fonte: gganimate: How to Create Plots with Beautiful Animation in R
Tipo de variável: Discreta.
Transição: Manual, sem interpolação.
Uso típico: Controle explícito dos estados da animação.
ggplot(DF_MAPA)+
geom_sf(aes(fill = preenchimento))+
scale_fill_gradientn(colours= c("#0d1b2a","#415a77","#e0e1dd","#588157","#344e41"))+
labs(title = "Animação transition_manual - Ano {current_frame}",
x="", y="", fill= "Taxa")+
theme_bw()+
transition_manual(ano)
Fonte: transition_manual: Create an animation by specifying the frame membership directly ; Create an animation by specifying the frame membership directly
Tipo de variável: Contínua (geralmente temporal).
Transição: Dados são revelados progressivamente ao longo de uma variável.
Uso típico: Efeito de traçado ou revelação ao longo do tempo ou outra variável contínua.
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") +
theme_bw()+
transition_reveal(Day)
Fonte: gganimate: How to Create Plots with Beautiful Animation in R
Tipo de variável: Categórica.
Transição: Suave entre estados categóricos.
Uso típico: Dados categóricos com transições suaves entre estados.
ggplot(mean.temp, aes(Month, Temp, fill = Temp)) +
geom_col() +
scale_fill_distiller(palette = "Reds", direction = 1) +
theme_minimal() +
theme(panel.grid = element_blank(),
panel.grid.major.y = element_line(color = "white"),
panel.ontop = TRUE) +
theme_bw() +
transition_states(Month, wrap = FALSE) +
shadow_mark()
Fonte: gganimate: How to Create Plots with Beautiful Animation in R
Tipo de variável: Múltiplas variáveis independentes.
Transição: Suave e contínua para cada componente de forma independente.
Uso típico: Animação de várias variáveis componentes de forma independente, mantendo a interpolação suave entre os estados de cada componente.
ggplot(DATA_1, aes(x, y, group = id, size = size)) +
geom_point() +
transition_components(time, enter_length = 2, exit_length = 2) +
theme_bw() +
enter_grow() +
exit_fade()
Fonte:Transition individual components through their own lifecycle
Tipo de variável: Condição lógica.
Transição: Aparição ou desaparecimento de elementos com base em uma condição.
Uso típico: Introdução e remoção de dados durante a animação com base em uma condição específica.
ggplot(iris, aes(Petal.Width, Petal.Length, colour = Species)) +
geom_point() +
transition_filter(
transition_length = 2,
filter_length = 1,
Setosa = Species == 'setosa',
Long = Petal.Length > 4,
Wide = Petal.Width > 2,
keep = TRUE
) +
ggtitle(
'Filter: {closest_filter}',
subtitle = '{closest_expression}'
) +
theme_bw() +
exit_recolour(colour = 'grey') +
exit_shrink(size = 0.5)
Fonte: Animated Plots using ggplot and gganimate ; Transition between different filters
Tipo de variável: Não definido explicitamente (baseado em camadas de gráfico).
Transição: Adição progressiva de camadas ao gráfico.
Uso típico: Construção de gráficos camada por camada, ajudando a entender a composição do gráfico.
ggplot(mtcars, aes(mpg, disp)) +
geom_point() +
geom_smooth(colour = 'grey', se = FALSE) +
geom_smooth(aes(colour = factor(gear))) +
transition_layers(layer_length = 1, transition_length = 2) +
theme_bw() +
enter_fade() + enter_grow()
Fonte: Build up a plot, layer by layer
Tipo de variável: Temporal com eventos discretos.
Transição: Eventos ocorrem com início e fim definidos.
Uso típico: Animação de eventos com duração definida.
ggplot(DATA_2, aes(x, y)) +
geom_col() +
transition_events(start = begin,
end = begin + length,
enter_length = enter,
exit_length = exit) +
theme_bw() +
enter_grow() +
exit_drift(x_mod = 11) +
exit_fade()
Fonte: Transition individual events in and out
Tipo de variável: Nenhuma.
Transição: Nenhuma transição.
Uso típico: Exibir um gráfico estático ou quando a transição não é necessária.
animate(): controla os frames do gif.
anim_save(): salva o gif.
anim <- animate(PLOT_ANIMADO, nframes = 50, fps = 10)
anim_save("animacao.gif", animation = anim)