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

浏览: 1534

taoyan:R语言中文社区特约作家,伪码农,R语言爱好者,爱开源。

个人博客: https://ytlogos.github.io/

公众号:生信大讲堂



简介

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)

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

定制化

将gganimate与ggplot2结合起来可以定制化很多复杂的动画

p2 <- ggplot(gapminder, aes(gdpPercap, lifeExp, size=pop))+

    geom_point()+

    geom_point(aes(frame=year), color="red")+

    scale_x_log10()

gganimate(p2)  

如果要绘制累积效果图,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)

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

p4 <- ggplot(gapminder, aes(gdpPercap, lifeExp, size=pop, frame=continent))+

    geom_point(color="blue")+

    scale_x_log10()

gganimate(p4)

需要注意的是如何我们绘制的图形涉及到统计汇总比如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)

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

 gganimate(p, interval = .2)

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
   




公众号后台回复关键字即可学习

回复 R                  R语言快速入门及数据挖掘 
回复 Kaggle案例  Kaggle十大案例精讲(连载中)
回复 文本挖掘      手把手教你做文本挖掘
回复 可视化          R语言可视化在商务场景中的应用 
回复 大数据         大数据系列免费视频教程 
回复 量化投资      张丹教你如何用R语言量化投资 
回复 用户画像      京东大数据,揭秘用户画像
回复 数据挖掘     常用数据挖掘算法原理解释与应用
回复 机器学习     人工智能系列之机器学习与实践
回复 爬虫            R语言爬虫实战案例分享


推荐 1
本文由 R语言中文社区 创作,采用 知识共享署名-相同方式共享 3.0 中国大陆许可协议 进行许可。
转载、引用前需联系作者,并署名作者且注明文章出处。
本站文章版权归原作者及原出处所有 。内容为作者个人观点, 并不代表本站赞同其观点和对其真实性负责。本站是一个个人学习交流的平台,并不用于任何商业目的,如果有任何问题,请及时联系我们,我们将根据著作权人的要求,立即更正或者删除有关内容。本站拥有对此声明的最终解释权。

0 个评论

要回复文章请先登录注册