gganimate: ggplot2拓展之让你的数据high起来
gganimate:让ggplot2图 随时而动。
常用参数
transition_*()
defines how the data should be spread out and how it relates to itself across time.
view_*()
defines how the positional scales should change along the animation.
shadow_*()
defines how data from other points in time should be presented in the given point in time.
enter_*()/exit_*()
defines how new data should appear and how old data should disappear during the course of the animation.
ease_aes()
defines how different aesthetics should be eased during transitions.
实践的真知
R包安装
install.package("gganimate")
install.packages("gifski")
Preliminaries: Load gganimate & datas
rm(list = ls()) # Clear the environment
options(warn=-1) # Turn off warning message globally
library(ggplot2)
library(gganimate)
先赏为敬
ggplot(gapminder, aes(gdpPercap, lifeExp, size = pop, colour = country)) +
geom_point(alpha = 0.7, show.legend = FALSE) +
scale_colour_manual(values = country_colors) +
scale_size(range = c(2, 12)) +
scale_x_log10() +
facet_wrap(~continent) +
# Here comes the gganimate specific bits
labs(title = 'Year: {frame_time}', x = 'GDP per capita', y = 'life expectancy') +
transition_time(year) +
ease_aes('linear')
庖丁解牛
library(gapminder)
theme_set(theme_bw())
head(gapminder)
country | continent | year | lifeExp | pop | gdpPercap |
---|---|---|---|---|---|
<fct> | <fct> | <int> | <dbl> | <int> | <dbl> |
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. |
ggplot(gapminder, aes(gdpPercap, lifeExp, size=pop, color=continent,frame=year)) +
geom_point() +
scale_x_log10() +
transition_time(year)+
shadow_wake(wake_length = 0.05)
图按照年份变化
ggplot(gapminder, aes(gdpPercap, lifeExp, size=pop))+
geom_point()+
geom_point(aes(colour = factor(year)))+
scale_x_log10() +
labs(title = 'Year: {frame_time}', x = 'GDP per capita', y = 'life expectancy') +
transition_time(year)
一般来说我们都是将时间映射给frame,这也符合我们的直觉,但是这并不意味着我们只能将时间映射给frame,我们可以将任何想要的变量映射给frame。
ggplot(gapminder, aes(gdpPercap, lifeExp, size=pop, frame=continent))+
geom_point(color="blue")+
scale_x_log10() +
transition_states(continent)
有一篇介绍本R包的比较好的一篇文章,大家可以自行比对学习脚本: 使用gganimate制作数据可视化gif动图
生物的坑
以下内容copy自如何让你的基因放烟花。
注:此部分内容仅觉得好玩,我还没有想到这些处理怎么展示会比较好,主要是学习以下代码。
load("fireworks.Rdata")
class(test)
>class(test)
[1] "data.frame"
ggplot(test,aes(x=subgroup,y=expression,fill=subgroup))+
geom_boxplot()+
geom_jitter()+
theme_bw()
分面,表示不同基因的表达量
p <- ggplot(test,aes(x=subgroup,y=expression,fill=subgroup))+
geom_boxplot()+
geom_jitter()+
theme_bw()
p +facet_grid(.~gene)
transition_*()
选择分面并动态显示,transition_time()
专指时间。
p <- ggplot(test,aes(x=subgroup,y=expression,fill=subgroup))+
geom_boxplot()+
geom_jitter()+
theme_bw()
p +transition_states(gene,
state_length = 0)+
labs(title = "{closest_state} expression(TPM)")
基因在不同分组中的动态数据展示
p <- ggplot(test,aes(x=gene,y=expression))+
geom_boxplot(aes(color=gene),size=2)+
geom_jitter()+
theme_bw()
p +transition_states(subgroup,
transition_length = 0.1,
state_length = 0)+
labs(title = "{closest_state}")
去除边界,放飞变量
p <- ggplot(test,aes(x=gene,y=expression,color=gene))+
#geom_boxplot()+
geom_jitter()+
theme_bw()
p +transition_states(subgroup,
transition_length = 0.1,
state_length = 0)+
labs(title = "{closest_state}")
巨人的肩膀
gganimate: A Grammar of Animated Graphics
Reference manual
使用gganimate制作数据可视化gif动图
如何让你的基因放烟花
生物的坑
数据 提取码: a55g