gganimate:数据跳动
简介
前面已经粗略学了一下gganimate
的部分语法,下面继续。
Example
使用数据集如下:
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.
Static plot
p <- ggplot(gapminder, aes(gdpPercap, lifeExp, size = pop, color = country)) +
geom_point(show.legend = FALSE, alpha = 0.7) +
scale_color_manual(values = country_colors) +
scale_x_log10() +
scale_size(range = c(5, 15)) +
labs(x = "GDP Per Captita", y = "Life Expectancy")
p
这个
country_color
调色版不错
随时间变化
p + transition_time(year) +
labs(title = "Year:{frame_time}")
固定坐标轴
由view_follow
实现
p + transition_time(year) +
labs(title = "Year: {frame_time}") +
view_follow(fixed_y = TRUE)
这里指固定y轴,可以看到x轴还是在变化,这个实际上用处不大
shadows_*
shadows: you want the animation to have memory.
这个很有意思,使你的动画具有记忆,看看是如何实现的。
shadow_wake()
This shadow is meant to draw a small wake after data by showing the latest frames up to the current. You can choose to gradually diminish the size and/or opacity of the shadow. The length of the wake is not given in absolute frames as that would make the animation susceptible to changes in the framerate. Instead it is given as a proportion of the total length of the animation.
可以看出,shadow_wake()
在transition_time()
的基础上进行了运动轨迹的记录。
p + transition_time(year) +
labs(title = "Year: {frame_time}") +
shadow_wake(wake_length = 0.1, alpha = FALSE)
默认情况下,动画会从前一帧中留下轨迹阴影,
gganimate
默认的是100帧动画,所以我们这里设置wake_length=0.1
,这样每个点后面会留下此动画前10个动画点阴影,并且大小、透明度都会下降。从上图我们可以看到这10个点之间有着明显的间隙,要修改这种间隙的话,最简单的方法就是通过animate()
中的detail
参数设置,另外我们可以看到这种动画不是很清晰漂亮,主要是我是在Windows
上实现的,Windows
默认使用Windows GDI
作为图形设备,以及png()
图形设备进行渲染。可以更改为cairo
进行渲染。如果你是Mac
用户的话,默认的渲染效果就非常棒了。
p_wake <- p + transition_time(year) +
labs(title = "Year: {frame_time}") +
shadow_wake(wake_length = 0.1, alpha = FALSE)
p_wake%>%animate(detail=5, type="cairo")
可以看到渲染效果非常棒!
shadow_
类函数十分好玩,通过修改各种参数,美感十足
修改长度wake_length
p_wake <- p + transition_time(year) +
labs(title = "Year: {frame_time}") +
shadow_wake(wake_length = 0.2, alpha = FALSE)
p_wake%>%animate(detail=5, type="cairo")
修改大小size
shadow_wake()
默认留下尺寸减小并透明的尾迹,这里我们生成尾迹透明但不收缩的。
p_wake <- p + transition_time(year) +
labs(title = "Year: {frame_time}") +
shadow_wake(wake_length = 0.1, size = NULL)
p_wake%>%animate(detail=5, type="cairo")
多参数控制
如果同时控制多参数,会如何呢?
p_wake <- p + transition_time(year) +
labs(title = "Year: {frame_time}") +
shadow_wake(wake_length = 0.2,
size=15,
alpha=NULL,
#尾迹颜色为白色
colour="white",
#切换模式
falloff = "quintic-in"
)
p_wake%>%animate(detail=5, type="cairo")
有点诡异。。。。。
shadow_mark()
This shadow lets you show the raw data behind the current frame. Both past and/or future raw data can be shown and styled as you want.
很简单,就是将每次出现的轨迹都留下来不消失
p_mark <- p + transition_time(year) +
labs(title = "Year: {frame_time}") +
shadow_mark(alpha = 0.3, size = 0.5)
p_mark%>%animate(detail=5, type="cairo")
transition_reveal()
transition_reveal() allows you to let data gradually appear, based on a given time dimension.
这对于时间序列非常有用,顺着年-月-日进行动画,这也是为什么对于时间序列
transition_time()
不好使。
head(airquality)
Ozone Solar.R Wind Temp Month Day
1 41 190 7.4 67 5 1
2 36 118 8.0 72 5 2
3 12 149 12.6 74 5 3
4 18 313 11.5 62 5 4
5 NA NA 14.3 56 5 5
6 28 NA 14.9 66 5 6
airquality$month_name <- rep(c("May", "June", "July", "August", "September"), c(31, 30, 31, 31, 30))
p_reveal <- ggplot(airquality, aes(Day, Temp, group = Month, color = month_name)) +
geom_line() +
geom_point(size=5) +
geom_text(aes(label = month_name)) +
transition_reveal(along = Day, range = , keep_last = F)
animate(p_reveal, nframes = 50, type="cairo")
SessionInfo
devtools::session_info()
- Session info ------------------------------------------------------------------
setting value
version R version 3.6.1 (2019-07-05)
os Windows 10 x64
system x86_64, mingw32
ui RStudio
language (EN)
collate Chinese (Simplified)_China.936
ctype Chinese (Simplified)_China.936
tz Asia/Taipei
date 2019-08-15
- Packages ----------------------------------------------------------------------
package * version date lib source
assertthat 0.2.1 2019-03-21 [1] CRAN (R 3.6.0)
backports 1.1.4 2019-04-10 [1] CRAN (R 3.6.0)
broom 0.5.2 2019-04-07 [1] CRAN (R 3.6.0)
callr 3.3.1 2019-07-18 [1] CRAN (R 3.6.1)
cellranger 1.1.0 2016-07-27 [1] CRAN (R 3.6.0)
cli 1.1.0 2019-03-19 [1] CRAN (R 3.6.0)
colorspace 1.4-1 2019-03-18 [1] CRAN (R 3.6.0)
crayon 1.3.4 2017-09-16 [1] CRAN (R 3.6.0)
desc 1.2.0 2018-05-01 [1] CRAN (R 3.6.0)
devtools 2.1.0 2019-07-06 [1] CRAN (R 3.6.1)
digest 0.6.20 2019-07-04 [1] CRAN (R 3.6.1)
dplyr * 0.8.3 2019-07-04 [1] CRAN (R 3.6.1)
fansi 0.4.0 2018-10-05 [1] CRAN (R 3.6.0)
farver 1.1.0 2018-11-20 [2] CRAN (R 3.6.0)
forcats * 0.4.0 2019-02-17 [1] CRAN (R 3.6.0)
fs 1.3.1 2019-05-06 [1] CRAN (R 3.6.0)
gapminder * 0.3.0 2017-10-31 [2] CRAN (R 3.6.0)
generics 0.0.2 2018-11-29 [2] CRAN (R 3.6.0)
gganimate * 1.0.3 2019-04-02 [2] CRAN (R 3.6.0)
ggplot2 * 3.2.1 2019-08-10 [1] CRAN (R 3.6.1)
gifski 0.8.6 2018-09-28 [1] CRAN (R 3.6.0)
glue 1.3.1 2019-03-12 [1] CRAN (R 3.6.0)
grkstyle 0.0.1 2019-08-13 [1] Github (gadenbuie/grkstyle@a141d39)
gtable 0.3.0 2019-03-25 [1] CRAN (R 3.6.0)
haven 2.1.1 2019-07-04 [1] CRAN (R 3.6.1)
hms 0.5.0 2019-07-09 [1] CRAN (R 3.6.1)
httr 1.4.1 2019-08-05 [1] CRAN (R 3.6.1)
jsonlite 1.6 2018-12-07 [1] CRAN (R 3.6.0)
labeling 0.3 2014-08-23 [1] CRAN (R 3.6.0)
lattice 0.20-38 2018-11-04 [2] CRAN (R 3.6.1)
lazyeval 0.2.2 2019-03-15 [1] CRAN (R 3.6.0)
lubridate 1.7.4 2018-04-11 [1] CRAN (R 3.6.0)
magrittr 1.5 2014-11-22 [1] CRAN (R 3.6.0)
memoise 1.1.0 2017-04-21 [1] CRAN (R 3.6.0)
modelr 0.1.5 2019-08-08 [1] CRAN (R 3.6.1)
munsell 0.5.0 2018-06-12 [1] CRAN (R 3.6.0)
nlme 3.1-141 2019-08-01 [2] CRAN (R 3.6.1)
pillar 1.4.2 2019-06-29 [1] CRAN (R 3.6.0)
pkgbuild 1.0.4 2019-08-05 [1] CRAN (R 3.6.1)
pkgconfig 2.0.2 2018-08-16 [1] CRAN (R 3.6.0)
pkgload 1.0.2 2018-10-29 [2] CRAN (R 3.6.0)
plyr 1.8.4 2016-06-08 [1] CRAN (R 3.6.0)
png 0.1-7 2013-12-03 [1] CRAN (R 3.6.0)
prettyunits 1.0.2 2015-07-13 [1] CRAN (R 3.6.0)
processx 3.4.1 2019-07-18 [1] CRAN (R 3.6.1)
progress 1.2.2 2019-05-16 [1] CRAN (R 3.6.0)
ps 1.3.0 2018-12-21 [2] CRAN (R 3.6.0)
purrr * 0.3.2 2019-03-15 [1] CRAN (R 3.6.0)
R6 2.4.0 2019-02-14 [1] CRAN (R 3.6.0)
Rcpp 1.0.2 2019-07-25 [1] CRAN (R 3.6.1)
readr * 1.3.1 2018-12-21 [1] CRAN (R 3.6.0)
readxl 1.3.1 2019-03-13 [1] CRAN (R 3.6.0)
remotes 2.1.0 2019-06-24 [2] CRAN (R 3.6.0)
rlang 0.4.0 2019-06-25 [1] CRAN (R 3.6.0)
rprojroot 1.3-2 2018-01-03 [1] CRAN (R 3.6.0)
rstudioapi 0.10 2019-03-19 [1] CRAN (R 3.6.0)
rvest 0.3.4 2019-05-15 [1] CRAN (R 3.6.0)
scales 1.0.0 2018-08-09 [1] CRAN (R 3.6.0)
sessioninfo 1.1.1 2018-11-05 [2] CRAN (R 3.6.0)
stringi 1.4.3 2019-03-12 [1] CRAN (R 3.6.0)
stringr * 1.4.0 2019-02-10 [1] CRAN (R 3.6.0)
styler 1.1.1 2019-05-06 [1] CRAN (R 3.6.0)
testthat 2.2.1 2019-07-25 [1] CRAN (R 3.6.1)
tibble * 2.1.3 2019-06-06 [1] CRAN (R 3.6.0)
tidyr * 0.8.3 2019-03-01 [1] CRAN (R 3.6.0)
tidyselect 0.2.5 2018-10-11 [1] CRAN (R 3.6.0)
tidyverse * 1.2.1 2017-11-14 [1] CRAN (R 3.6.0)
tweenr 1.0.1 2018-12-14 [2] CRAN (R 3.6.0)
usethis 1.5.1 2019-07-04 [1] CRAN (R 3.6.1)
utf8 1.1.4 2018-05-24 [1] CRAN (R 3.6.0)
vctrs 0.2.0 2019-07-05 [1] CRAN (R 3.6.1)
withr 2.1.2 2018-03-15 [1] CRAN (R 3.6.0)
xml2 1.2.2 2019-08-09 [1] CRAN (R 3.6.1)
zeallot 0.1.0 2018-01-28 [1] CRAN (R 3.6.0)
[1] C:/Tools/R/R_Library
[2] C:/Tools/R-3.6.1/library