自制动图展示连续数据

2019-08-23 23:20:15 浏览数 (1)

gganimate|让你的图动起来!!!

这是ggplot中十分可爱的一个扩增包,目的只有一个,就是让你的图动起来!就是酱紫!!

gganimate扩展了ggplot2实现的图形语法,包括动画描述。它通过提供一系列新的语法类来实现这一点,这些类可以添加到绘图对象中,以便自定义它应该如何随时间变化。

下面是他的parameter:

transition_*()定义了数据应该如何展开以及它与时间的关系。 view_*()定义位置比例应如何沿动画更改。 shadow_*()定义如何在给定的时间点呈现来自其他时间点的数据。 enter_*()/ exit_*()定义新数据应如何显示以及旧数据在动画过程中应如何消失。 ease_aes()定义了在过渡期间应该如何进行过渡。

举个栗子!

代码语言:javascript复制
#安装辅助包,该包有两个版本,已经更新为最新版本,老版本在未来将不再支持。install.packages("gganimate")

# 安装开发版
# install.packages('devtools')
# devtools::install_github('thomasp85/gganimate')
library(ggplot2)
library(gganimate)

ggplot(mtcars, aes(factor(cyl), mpg))  
  geom_boxplot()   geom_point()  
  # Here comes the gganimate code
  transition_states(
    gear,
    transition_length = 2,
    state_length = 1
  )  
  enter_fade()  
  exit_shrink()  
  ease_aes('sine-in-out')

加载时间是比较长的,需要耐心等待哈!

Yet Another Example

首先查看一下数据格式吧,Gapminder是关于预期寿命,人均国内生产总值和国家人口的数据摘录。

代码语言:javascript复制
library(gapminder)
head(gapminder)#我们看一下数据格式
代码语言:javascript复制
ggplot(gapminder, aes(gdpPercap, lifeExp, size = pop, colour = country))  
#点的大小和颜色分别由pop和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)                       #按照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')#指数据变化的状态,线性发展比较缓慢

哈哈哈,现在我们以肿瘤数据为例进行演示一下:

我编了一组测试数据,其中将肿瘤分为I,II,III型,IV型为control,然后分别显示了再不同样本中不同肿瘤分型下的部分基因的表达情况。

代码语言:javascript复制
library(ggplot2)
library(gganimate)
#首先我们进行数据的读入

data <- "subgroup,sample,gene,expression
I,Tumor,p53,12.725952
II,Tumor,p53,11.914176
III,Tumor,p53,12.315768
IV,Normal,p53,12.978894
I,Tumor,p53,11.93924
II,Tumor,p53,12.262185
III,Tumor,p53,11.538924
IV,Normal,p53,12.016589
I,Tumor,p53,12.302574
II,Tumor,p53,11.939233
III,Tumor,p53,12.803992
IV,Normal,p53,10.674506
I,Tumor,p53,12.569142
II,Tumor,p53,12.088496
III,Tumor,p53,9.971951
IV,Normal,p53,13.008554
I,Tumor,p53,12.804154
II,Tumor,p53,11.847107
III,Tumor,p53,12.081261
IV,Normal,p53,12.158431
I,Tumor,p53,11.096693
II,Tumor,p53,12.655811
III,Tumor,p53,11.509067
IV,Normal,p53,12.523573
I,Tumor,p53,11.3554
II,Tumor,p53,11.560566
III,Tumor,p53,10.969046
IV,Normal,p53,11.169892
I,Tumor,p53,12.884054
II,Tumor,p53,12.284268
III,Tumor,her2,9.575523
IV,Normal,her2,12.409381
I,Tumor,her2,12.114364
II,Tumor,her2,11.493997
III,Tumor,her2,10.987106
IV,Normal,her2,11.943991
I,Tumor,her2,11.171378
II,Tumor,her2,13.120248
III,Tumor,her2,12.628872
IV,Normal,her2,11.91914
I,Tumor,her2,12.36504
II,Tumor,her2,12.707354
III,Tumor,her2,12.54517
IV,Normal,her2,12.199749
I,Tumor,her2,13.184496
II,Tumor,her2,12.640412
III,Tumor,her2,12.716897
IV,Normal,her2,13.359091
I,Tumor,her2,11.760945
II,Tumor,her2,11.406367
III,Tumor,her2,11.984382
IV,Normal,her2,12.254977
I,Tumor,her2,11.579763
II,Tumor,her2,11.983042
III,Tumor,her2,12.566317
IV,Normal,her2,10.869331
I,Tumor,her2,10.910963
II,Tumor,her2,11.948207
III,Tumor,myc,12.363072
IV,Normal,myc,12.755182
I,Tumor,myc,11.922223
II,Tumor,myc,9.618839
III,Tumor,myc,12.693868
IV,Normal,myc,13.40685
I,Tumor,myc,11.871609
II,Tumor,myc,11.783704
III,Tumor,myc,12.485053
IV,Normal,myc,12.669123
I,Tumor,myc,11.653691
II,Tumor,myc,11.675768
III,Tumor,myc,12.744605
IV,Normal,myc,12.911619
I,Tumor,myc,12.008307
II,Tumor,myc,11.838161
III,Tumor,myc,12.590989
IV,Normal,myc,11.680278
I,Tumor,myc,11.719241
II,Tumor,myc,10.156746
III,Tumor,myc,11.84406
IV,Normal,myc,12.975393
I,Tumor,myc,10.963332
II,Tumor,myc,12.338216
III,Tumor,myc,12.030859
IV,Normal,myc,11.119114
I,Tumor,myc,12.661349
II,Tumor,myc,13.168166
III,Tumor,myc,11.707595
IV,Normal,myc,12.06719
I,Tumor,myc,12.463962
II,Tumor,myc,12.288819
III,Tumor,myc,12.036757
IV,Normal,myc,12.98055
I,Tumor,myc,11.343075
II,Tumor,myc,12.565481
III,Tumor,myc,12.279996
IV,Normal,myc,12.965189
I,Tumor,myc,12.946155
II,Tumor,myc,11.688462
III,Tumor,sox4,11.944477
IV,Normal,sox4,12.128177
I,Tumor,sox4,11.116105
II,Tumor,sox4,11.148871
III,Tumor,sox4,13.139244
IV,Normal,sox4,10.043207
I,Tumor,sox4,12.043914
II,Tumor,sox4,9.990576
III,Tumor,sox4,11.624263
IV,Normal,sox4,11.647402
I,Tumor,sox4,12.502176
II,Tumor,sox4,12.291812
III,Tumor,sox4,11.421913
IV,Normal,sox4,12.282511
I,Tumor,sox4,12.511991
II,Tumor,sox4,12.285322
III,Tumor,sox4,11.7884
IV,Normal,sox4,13.747552
I,Tumor,sox4,11.212993
II,Tumor,sox4,12.936845
III,Tumor,sox4,12.442484
IV,Normal,sox4,10.324288
I,Tumor,sox4,12.436421
II,Tumor,sox4,11.923122
III,Tumor,sox4,12.831474
IV,Normal,sox4,12.271537
I,Tumor,sox4,12.208086
II,Tumor,sox4,11.830799
III,Tumor,sox4,12.410238
IV,Normal,sox4,12.13912
I,Tumor,sox4,12.47"

test <- read.csv(text=data,header=T)
head(test)
代码语言:javascript复制
library(ggplot2)
ggplot(test,aes(x=subgroup,y=expression,fill=subgroup)) 
  geom_boxplot() 
  geom_jitter() 
  theme_bw()                     #按照subgroup进行分型,并画出箱式图

同样对不同gene在各组中的分布情况进行描述:

代码语言:javascript复制
library(ggplot2)
p <- ggplot(test,aes(x=subgroup,y=expression,fill=subgroup)) 
  geom_boxplot() 
  geom_jitter() 
  theme_bw()
p  facet_grid(.~gene)#按照gene对各个小组进行分类
代码语言:javascript复制
library(ggplot2)
library(gganimate)
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")

0 人点赞