R语言可视化学习笔记之gganimate包

mark

简介

gganimate包是ggplot2的扩展包,主要用于绘制动画。它在ggplot2的基础上了补充了一个美学映射frame,就像x,y,size,color,fill一样进行映射。

安装

if(!require(devtools)) install.packages("devtools")
devtools::install_github("dgrtwo/gganimate")

需要注意的是这个包依赖于ImageMagick来产生动画,如果自行安装的话在调用ImageMagick很容易出错,所以推荐在RStudio里面安装。

install.packages("installr")
installr::install.ImageMagick("http://www.imagemagick.org/script/download.php")

安装好之后以管理员身份运行RStudio

可视化

这里我们使用gapminder包里的数据集gapminder进行可视化

library(gapminder)
library(ggplot2)
library(gganimate)
theme_set(theme_bw())
head(gapminder)
# A tibble: 6 x 6
  country     continent  year lifeExp      pop gdpPercap
  <fct>       <fct>     <int>   <dbl>    <int>     <dbl>
1 Afghanistan Asia       1952    28.8  8425333       779
2 Afghanistan Asia       1957    30.3  9240934       821
3 Afghanistan Asia       1962    32.0 10267083       853
4 Afghanistan Asia       1967    34.0 11537966       836
5 Afghanistan Asia       1972    36.1 13079460       740
6 Afghanistan Asia       1977    38.4 14880372       786
p <- ggplot(gapminder, aes(gdpPercap, lifeExp, size=pop, color=continent,frame=year))+
  geom_point()+
  scale_x_log10()
 gganimate(p)

mark

不管动画中的图形如何移动,坐标轴、图例等都是固定的。

定制化

gganimateggplot2结合起来可以定制化很多复杂的动画

p2 <- ggplot(gapminder, aes(gdpPercap, lifeExp, size=pop))+
  geom_point()+
  geom_point(aes(frame=year), color="red")+
  scale_x_log10()
gganimate(p2)

mark

如果要绘制累积效果图,gganimate提供了cumalative参数,这对于路径图来说十分是有效的

p3 <- ggplot(gapminder, aes(gdpPercap, lifeExp, frame=year, color=continent))+
  geom_path(aes(cumulative=TRUE, group=country))+
  scale_x_log10()+
  facet_wrap(~continent)
gganimate(p3)

mark

一般来说我们都是将时间映射给frame,这也符合我们的直觉,但是这并不意味着我们只能将时间映射给frame,我们可以将任何想要的变量映射给frame

p4 <- ggplot(gapminder, aes(gdpPercap, lifeExp, size=pop, frame=continent))+
  geom_point(color="blue")+
  scale_x_log10()
gganimate(p4)

mark

需要注意的是如何我们绘制的图形涉及到统计汇总比如geom_smooth(),那么在geom_smooth()图层中需要添加group映射。

p5 <- ggplot(gapminder,aes(gdpPercap, lifeExp, size=pop, frame=year))+
  geom_point()+
  geom_smooth(aes(group=year),method = "lm", show.legend = FALSE)+
  facet_wrap(~continent, scales = "free")+
  scale_x_log10()
gganimate(p5)

mark

最后如果需要控制动画播放速度,使用interval参数控制

gganimate(p, interval = .2)

mark

SessionInfo

sessionInfo()
R version 3.4.3 (2017-11-30)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)

Matrix products: default

locale:
[1] LC_COLLATE=Chinese (Simplified)_China.936 
[2] LC_CTYPE=Chinese (Simplified)_China.936   
[3] LC_MONETARY=Chinese (Simplified)_China.936
[4] LC_NUMERIC=C                              
[5] LC_TIME=Chinese (Simplified)_China.936    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods  
[7] base     

other attached packages:
[1] gganimate_0.1.0.9000 ggplot2_2.2.1.9000  
[3] gapminder_0.3.0     

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.15      rstudioapi_0.7    magrittr_1.5     
 [4] munsell_0.4.3     colorspace_1.3-2  rlang_0.1.6      
 [7] stringr_1.2.0     plyr_1.8.4        tools_3.4.3      
[10] grid_3.4.3        gtable_0.2.0      utf8_1.1.3       
[13] cli_1.0.0         withr_2.1.1.9000  htmltools_0.3.6  
[16] yaml_2.1.16       lazyeval_0.2.1    assertthat_0.2.0 
[19] digest_0.6.15     tibble_1.4.2      crayon_1.3.4     
[22] base64enc_0.1-3   animation_2.5     labeling_0.3     
[25] stringi_1.1.6     compiler_3.4.3    pillar_1.1.0     
[28] installr_0.19.0   scales_0.5.0.9000
Researcher

I am a PhD student of Crop Genetics and Breeding at the Zhejiang University Crop Science Lab. My research interests covers a range of issues:Population Genetics Evolution and Ecotype Divergence Analysis of Oilseed Rape, Genome-wide Association Study (GWAS) of Agronomic Traits.

comments powered by Disqus