温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

R语言怎么使用gganimate创建可视化动图

发布时间:2022-05-12 11:23:07 来源:亿速云 阅读:200 作者:iii 栏目:开发技术

这篇“R语言怎么使用gganimate创建可视化动图”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“R语言怎么使用gganimate创建可视化动图”文章吧。

加载R包,数据

#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绘制

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

R语言怎么使用gganimate创建可视化动图

gganimate 动态

1. transition_time() 核心函数,添加动态

p + transition_time(year) +
  labs(title = "Year: {frame_time}")

R语言怎么使用gganimate创建可视化动图

2 按需设置

1)添加小尾巴

p + transition_time(year) +
  labs(title = "Year: {frame_time}") +
  shadow_wake(wake_length = 0.1, alpha = FALSE)

R语言怎么使用gganimate创建可视化动图

2)原数据做背景

p + transition_time(year) +
  labs(title = "Year: {frame_time}") +
  shadow_mark(alpha = 0.3, size = 0.5)

R语言怎么使用gganimate创建可视化动图

以上就是关于“R语言怎么使用gganimate创建可视化动图”这篇文章的内容,相信大家都有了一定的了解,希望小编分享的内容对大家有帮助,若想了解更多相关的知识内容,请关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI