使用gganimate包做动态图 r 学习笔记

本文介绍了gganimate包,它是ggplot2的扩展,用于创建动态图表。主要内容包括:gganimate包的核心函数如transition_*(如transition_states、transition_filter等)、view_*(如view_follow、view_step)以及shadow_*(如shadow_wake、shadow_trail)的使用方法,以及如何通过这些函数控制数据的动态展示和视图变换。
摘要由CSDN通过智能技术生成

今天学习一个主要用于绘制动画的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
  • 12
    点赞
  • 50
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值