gganimate | create dynamic visual map, so you can speak charts

This article first appeared in "Health Letter Depot" public number, https://mp.weixin.qq.com/s/kKQ2670FBiDqVCMuLBL9NQ

More about the R language, ggplot2 mapping, analysis of the content of raw faith, stay tuned trumpet.

 

Introduce a major draw for animation ggplot2 expansion pack --- gganimate package.

 

Hans Rosling on "New Insights on Poverty" in TED speech is definitely one of the biggest influence for me a few TED, the original data can be visualized this show ,,, so ,,, Hyun story can say ...

 

Try using the following gganimate and gapminder packet data sets to achieve a similar visualization process.

 

Loading a pack R, data

#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.

Data set includes the world's major countries in 1952-- 2007, GDP per capita, life expectancy and population growth data.

 

Draw two ggplot2

Use ggplot2 draw

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() 核心函数,添加动态

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

img

2 按需设置

1)添加小尾巴

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

2)原数据做背景

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

◆ ◆ ◆ ◆ ◆

精心整理(含图版)|你要的全拿走!有备无患 (R统计,ggplot2绘图,生信图形可视化汇总)

 

【觉得不错,右下角点个“在看”,期待您的转发,谢谢!】

 

 

Guess you like

Origin www.cnblogs.com/Mao1518202/p/11993575.html