今天学习一个主要用于绘制动画的ggplot2的扩展包—gganimate包
文章目录
gganimate包简要介绍
几个主要函数
transition_()定义了数据应如何散布以及如何与时间相关。
shadow_() 定义应如何在给定的时间点显示其他时间点的数据。
view_() 定义位置比例应如何随着动画变化。
enter_()/exit_() 定义在动画过程中新数据应如何显示以及旧数据应如何消失。
ease_aes() 系列函数定义在过渡期间应如何缓和不同的美学。
gganimate的关键语法有三个:ggplot图形、transition_*()过渡函数、animate()生成动画。
先决条件:
gganimate 稳定版本在 CRAN 上可用,可与 一起安装。最新的开发版本可以安装如下:
install.packages('gganimate')devtools::install_github('thomasp85/gganimate')
加载所需的包,将默认 ggplot2 主题设置为 :theme_bw()
library(ggplot2)
library(gganimate)
library(gifski)
library(av)
theme_set(theme_bw())
演示数据集
library(gapminder)
head(gapminder)
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.
静态绘图
p <- ggplot(
gapminder,
aes(x = gdpPercap, y=lifeExp