一、gganimate简介
包的名称起的就非常直接,gg-ggplot2,animate-让...动起来。因此gganimate是一款基于ggplot2的动态可视化扩展包,简单来说就是将ggplot2绘图对象转为gif动图的形式,可以让抽象的数理理论更加形象化。
gganimate的本质思想非常简单,就是将ggplot2对象进行动态化, gganimate()特殊之处在于在除了为绘图提供x,y以及color,size这样的美学映射之外,还必须提供一个所谓的frame映射,所谓的frame映射,其实就是动画的时间轴。比如说我们想绘制我国人口随时间变化的一个ggplot2动态图,那么这个时间就是 frame映射。
gganimate的主要函数:
transition _ *() :定义了数据应该如何展开动画关系。
view _ *() :定义坐标比例如何随着动画更改而变化
shadow _ *() :定义如何在给定的时间点呈现来自其他时间点的数据
enter_*()/ exit _ *() :定义新数据应如何显示以及旧数据在动画过程中应如何消失。
ease_aes() :定义过渡期间应该如何切换
二、需要的包和数据
install.packages('ggplot2')
install.packages('gganimate')
install.packages('gapminder')
library(ggplot2)
library(gganimate)
library(gapminder)
我们主要使用gapminder包中的自带数据,包括不同国家,不同时间的GDP、人口、人均寿命。来看看数据结构
head(gapminder)
country | continent | year | lifeExp | pop | gdpPercap |
---|---|---|---|---|---|
Afghanistan | Asia | 1952 | 28.8 | 8425333 | 779. |
Afghanistan | Asia | 1957 | 30.3 | 9240934 | 821. |
Afghanistan | Asia | 1962 | 32.0 | 10267083 | 853. |
Afghanistan | Asia | 1967 | 34.0 | 11537966 | 836. |
Afghanistan | Asia | 1972 | 36.1 | 13079460 | 740. |
Afghanistan | Asia | 1977 | 38.4 | 14880372 | 786. |
三、绘图实例
首先绘制一张静态图,以GDP为X轴,人均寿命为Y轴,人口数量作为气泡的大小,颜色呈现大洲
ggplot(data = gapminder,
aes(x=gdpPercap, y=lifeExp, size = pop, colour = continent))+
geom_point(show.legend = FALSE, alpha =0.9)+
scale_x_log10()+
labs(x ="每个国家的GPD", y ="每个国家的寿命")
按年度进行动画展示,只需要增加transition_time(year)即可,并设置标签变量:frame_time,即可得到下面的图形
ggplot(data = gapminder,
aes(x=gdpPercap, y=lifeExp, size = pop, colour = continent))+
geom_point(show.legend = FALSE, alpha =0.9)+
scale_x_log10()+
transition_time(year)+
labs(title ="年份: {frame_time}",x ="每个国家的GPD", y ="每个国家的寿命")
要横纵随着时间而变化,只要加入view_follow(fixed_y = TRUE)即可。
ggplot(data = gapminder,
aes(x=gdpPercap, y=lifeExp, size = pop, colour = continent))+
geom_point(show.legend = FALSE, alpha =0.9)+
scale_x_log10()+
transition_time(year)+
labs(title ="年份: {frame_time}",x ="每个国家的GPD", y ="每个国家的寿命")+
view_follow(fixed_y = TRUE)
按不同的大洲来画分面图,要加个分面函数facet_wrap()
给图片加个尾巴
ggplot(data = gapminder,
aes(x=gdpPercap, y=lifeExp, size = pop, colour = continent))+
geom_point(show.legend = FALSE, alpha =0.9)+
scale_x_log10()+
transition_time(year)+
labs(title ="年份: {frame_time}",x ="每个国家的GPD", y ="每个国家的寿命")+
shadow_wake(wake_length =0.1, alpha =0.95)
这样是不是看起来看更酷炫
我们来画个箱线图看看,图中可以看到,每个大洲的寿命分布,并且随着时间的推移,都在逐步提高,非常明显的表达实际情况的变化
ggplot(data = gapminder, aes(x=continent, y=lifeExp,fill=continent,alpha=0.95))+
geom_boxplot(show.legend = FALSE)+
transition_time(year)+
labs(x ="大洲", y ="寿命分布",title ="年份: {frame_time}")
ggplot(airquality, aes(Day, Temp, group = Month,colour=as.factor(Month))) +
geom_line(show.legend = FALSE) +
transition_reveal(Day)
再加上一个开头的点点,更有感觉
ggplot(airquality, aes(Day, Temp, group = Month,colour=as.factor(Month))) +
geom_line(show.legend = FALSE) +
geom_point(colour = 'red', size = 3) +
transition_reveal(Day
如果要保持小点点不消失,加上一个分组就可以了
ggplot(airquality, aes(Day,Temp,group=Month))+
geom_line()+
geom_point(aes(group= seq_along(Day)))+
scale_color_viridis_d()+
geom_point(colour ='red', size =3)+
transition_reveal(Day)
以上就是主要的一些用法,当然动画的方式还有很多种,transition_components、transition_events、transition_filter、transition_manual等,以后给大家分享,创作不易,欢迎大家转发分享。
推荐原创干货阅读:
聊聊近状, 唠十块钱的
【Deep Learning】详细解读LSTM与GRU单元的各个公式和区别
【手把手AI项目】一、安装win10+linux-Ubuntu16.04的双系统(全网最详细)
【Deep Learning】为什么卷积神经网络中的“卷积”不是卷积运算?
【TOOLS】Pandas如何进行内存优化和数据加速读取(附代码详解)
【TOOLS】python3利用SMTP进行邮件Email自主发送
【手把手AI项目】七、MobileNetSSD通过Ncnn前向推理框架在PC端的使用
公众号:AI蜗牛车
保持谦逊、保持自律、保持进步