gganimate:讓ggplot2圖 隨時(shí)而動(dòng)运沦。
常用參數(shù)
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.
實(shí)踐的真知
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)
一般來(lái)說(shuō)我們都是將時(shí)間映射給frame掌桩,這也符合我們的直覺(jué)萎津,但是這并不意味著我們只能將時(shí)間映射給frame,我們可以將任何想要的變量映射給frame。
ggplot(gapminder, aes(gdpPercap, lifeExp, size=pop, frame=continent))+
geom_point(color="blue")+
scale_x_log10() +
transition_states(continent)
有一篇介紹本R包的比較好的一篇文章,大家可以自行比對(duì)學(xué)習(xí)腳本: 使用gganimate制作數(shù)據(jù)可視化gif動(dòng)圖
生物的坑
以下內(nèi)容copy自如何讓你的基因放煙花霜运。
注:此部分內(nèi)容僅覺(jué)得好玩脾歇,我還沒(méi)有想到這些處理怎么展示會(huì)比較好,主要是學(xué)習(xí)以下代碼淘捡。
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()
分面藕各,表示不同基因的表達(dá)量
p <- ggplot(test,aes(x=subgroup,y=expression,fill=subgroup))+
geom_boxplot()+
geom_jitter()+
theme_bw()
p +facet_grid(.~gene)
transition_*()
選擇分面并動(dòng)態(tài)顯示,transition_time()
專(zhuān)指時(shí)間焦除。
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)")
基因在不同分組中的動(dòng)態(tài)數(shù)據(jù)展示
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制作數(shù)據(jù)可視化gif動(dòng)圖
如何讓你的基因放煙花
生物的坑
數(shù)據(jù) 提取碼: a55g