gganimate: ggplot2拓展之讓你的數(shù)據(jù)high起來(lái)

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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市膘魄,隨后出現(xiàn)的幾起案子乌逐,更是在濱河造成了極大的恐慌,老刑警劉巖瓣距,帶你破解...
    沈念sama閱讀 216,919評(píng)論 6 502
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件黔帕,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡蹈丸,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,567評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門(mén)呐芥,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)逻杖,“玉大人,你說(shuō)我怎么就攤上這事思瘟≥┌伲” “怎么了?”我有些...
    開(kāi)封第一講書(shū)人閱讀 163,316評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵滨攻,是天一觀的道長(zhǎng)够话。 經(jīng)常有香客問(wèn)我,道長(zhǎng)光绕,這世上最難降的妖魔是什么女嘲? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,294評(píng)論 1 292
  • 正文 為了忘掉前任,我火速辦了婚禮诞帐,結(jié)果婚禮上欣尼,老公的妹妹穿的比我還像新娘。我一直安慰自己停蕉,他們只是感情好愕鼓,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,318評(píng)論 6 390
  • 文/花漫 我一把揭開(kāi)白布。 她就那樣靜靜地躺著慧起,像睡著了一般菇晃。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上蚓挤,一...
    開(kāi)封第一講書(shū)人閱讀 51,245評(píng)論 1 299
  • 那天磺送,我揣著相機(jī)與錄音驻子,去河邊找鬼。 笑死册着,一個(gè)胖子當(dāng)著我的面吹牛拴孤,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播甲捏,決...
    沈念sama閱讀 40,120評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼演熟,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了司顿?” 一聲冷哼從身側(cè)響起芒粹,我...
    開(kāi)封第一講書(shū)人閱讀 38,964評(píng)論 0 275
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎大溜,沒(méi)想到半個(gè)月后化漆,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體钦奋,經(jīng)...
    沈念sama閱讀 45,376評(píng)論 1 313
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡座云,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,592評(píng)論 2 333
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了付材。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片朦拖。...
    茶點(diǎn)故事閱讀 39,764評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖厌衔,靈堂內(nèi)的尸體忽然破棺而出璧帝,到底是詐尸還是另有隱情,我是刑警寧澤富寿,帶...
    沈念sama閱讀 35,460評(píng)論 5 344
  • 正文 年R本政府宣布睬隶,位于F島的核電站,受9級(jí)特大地震影響页徐,放射性物質(zhì)發(fā)生泄漏苏潜。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,070評(píng)論 3 327
  • 文/蒙蒙 一泞坦、第九天 我趴在偏房一處隱蔽的房頂上張望窖贤。 院中可真熱鬧,春花似錦贰锁、人聲如沸赃梧。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,697評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)授嘀。三九已至,卻和暖如春锣险,著一層夾襖步出監(jiān)牢的瞬間蹄皱,已是汗流浹背览闰。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 32,846評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留巷折,地道東北人压鉴。 一個(gè)月前我還...
    沈念sama閱讀 47,819評(píng)論 2 370
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像锻拘,于是被迫代替她去往敵國(guó)和親油吭。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,665評(píng)論 2 354