前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >gganimate|创建可视化动图,让你的表会说话

gganimate|创建可视化动图,让你的表会说话

作者头像
生信补给站
发布2020-08-06 11:23:04
6240
发布2020-08-06 11:23:04
举报
文章被收录于专栏:生信补给站生信补给站

介绍一个主要用于绘制动画的ggplot2的扩展包---gganimate包。

Hans Rosling的关于“New Insights on Poverty”的TED演讲绝对是对我影响最大的几个TED之一,原来数据可以这样展示,,,可视化可以这样炫,,,故事可以这样讲...

下面尝试使用 gganimate 包和 gapminder 数据集,实现类似可视化过程。

加载R包,数据

代码语言:javascript
复制
#R包安装
install.packages("devtools")
library(devtools)    
install_github("thomasp85/gganimate")
install.packages("gapminder")
#加载
library(gganimate)
library(gapminder)
#查看数据
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.

数据集包括全球主要国家在1952-2007年的人均GDP增长、预期寿命以及人口增长的数据 。

二 ggplot2绘制

使用ggplot2绘制

代码语言:javascript
复制
theme_set(theme_bw())
p <- ggplot(gapminder,
  aes(x = gdpPercap, y=lifeExp, size = pop, colour = country)) +
  geom_point(show.legend = FALSE, alpha = 0.7) +
  scale_color_viridis_d() +
  scale_size(range = c(2, 12)) +
  scale_x_log10() +
  labs(x = "GDP per capita", y = "Life expectancy")
p

◆ ◆ ◆ ◆ ◆

三 gganimate 动态

1. transition_time() 核心函数,添加动态
代码语言:javascript
复制
p + transition_time(year) +
  labs(title = "Year: {frame_time}")
2 按需设置

1)添加小尾巴

代码语言:javascript
复制
p + transition_time(year) +
  labs(title = "Year: {frame_time}") +
  shadow_wake(wake_length = 0.1, alpha = FALSE)

2)原数据做背景

代码语言:javascript
复制
p + transition_time(year) +
  labs(title = "Year: {frame_time}") +
  shadow_mark(alpha = 0.3, size = 0.5)

参考资料

https://www.datanovia.com/en/blog/gganimate-how-to-create-plots-with-beautiful-animation-in-r/

TED:https://www.ted.com/talks/hans_rosling_the_best_stats_you_ve_ever_seen?language=zh-TW

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2019-12-05,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 生信补给站 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 使用ggplot2绘制
    • 1. transition_time() 核心函数,添加动态
      • 2 按需设置
        • 参考资料
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档