+ - 0:00:00
Notes for current slide
Notes for next slide

R语言中制作图形动画的多种方法

俞丽佳

2021/11/20

1 / 26

一个好的视觉效果能抓住观众的兴趣并给人留下深刻印象

2 / 26

动画原理

3 / 26

视觉残留

走马灯

晶状体成像 => 感光细胞感光,并且将光信号转换为神经电流 => 传回大脑引起人体视觉

4 / 26

视觉残留

逐帧动画

5 / 26

视觉残留

逐帧动画

关键帧动画

6 / 26

常用概念

  • 帧 (Frame): 画幅
  • 每秒帧数 (Frames per second,FPS):10帧/秒及以上为流畅的动画

7 / 26

R语言制作动画

8 / 26

概述

9 / 26

概述

编码器

ImageMagick (GraphicsMagick), Gifski

9 / 26

概述

编码器

ImageMagick (GraphicsMagick), Gifski

封装

magick, gifski

9 / 26

概述

编码器

ImageMagick (GraphicsMagick), Gifski

封装

magick, gifski

制作动画

animation, gganimate

9 / 26

概述

编码器

ImageMagick (GraphicsMagick), Gifski

封装

magick, gifski

制作动画

animation, gganimate

制作交互式动画

plotly

9 / 26

magick

读取图片

library(magick)
bigdata <- image_read('./images/bigdata.jpg')
frink <- image_read("./images/frink.png")
logo <- image_read("./images/Rlogo.png")
img <- c(bigdata, logo, frink)

10 / 26

magick

读取图片

制作动画

image_animate(image_scale(img, "200x200"),
fps = 1,
dispose = "previous")

11 / 26

magick

读取图片

制作动画

变形

newlogo <- image_scale(image_read("./images/Rlogo.png"))
oldlogo <- image_scale(image_read("./images/Rlogo-old.png"))
image_resize(c(oldlogo, newlogo), '200x150!') %>%
image_background('white') %>%
image_morph() %>%
image_animate(optimize = TRUE)

12 / 26

magick

读取图片

制作动画

变形

保存图片

newlogo <- image_scale(image_read("./images/Rlogo.png"))
oldlogo <- image_scale(image_read("./images/Rlogo-old.png"))
img=image_resize(c(oldlogo, newlogo), '200x150!') %>%
image_background('white') %>%
image_morph() %>%
image_animate(optimize = TRUE)
image_write(img, "Rlogo.gif")
13 / 26

gifski

一心一意做好将图像帧转换为高质量GIF动画的工作。

gifski(
png_files,
gif_file = "animation.gif",
width = 800,
height = 600,
delay = 1,
loop = TRUE,
progress = TRUE
)
save_gif(
expr,
gif_file = "animation.gif",
width = 800,
height = 600,
delay = 1,
loop = TRUE,
progress = TRUE,
...
)
14 / 26

animation

数学和统计学方法的动态展示

15 / 26

animation

制作动画的函数

library(animation)
## generate some images
owd = setwd(tempdir())
ani.options(interval = 0.05, nmax = 20)
png("bm%03d.png")
brownian.motion(pch = 21, cex = 5, col = "red", bg = "yellow",
main = "Demonstration of Brownian Motion")
dev.off()
## filenames with a wildcard *
im.convert("bm*.png", output = "bm-animation1.gif")
## use GraphicsMagick
gm.convert("bm*.png", output = "bm-animation2.gif")
## or a filename vector
bm.files = sprintf("bm%03d.png", 1:20)
im.convert(files = bm.files, output = "bm-animation3.gif")
library(animation)
saveGIF({
brownian.motion(pch = 21, cex = 5, col = "red", bg = "yellow")
}, movie.name = "brownian_motion.gif", interval = 0.1, nmax = 30,
ani.width = 600)
  • saveHTML()
  • saveVideo()
  • saveLatex()
  • saveSWF()
16 / 26

gganimate

动画语法

  • Transitions:定义数据如何伸展变化以及与各种变量(时间、迭代)间的关联

  • Views:定义坐标位置比例如何随动画变化

  • Shadows:定义如何在给定帧的基础上呈现来自其它帧的数据

  • Tweening:定义新数据如何显示,旧数据如何消失

  • Rendering:动画渲染方式

17 / 26

gganimate

ggplot(gapminder, aes(gdpPercap, lifeExp, size = pop, color = country)) +
geom_point(alpha = 0.7, show.legend = FALSE) +
scale_color_manual(values = country_colors) +
scale_x_log10() +
scale_size(range = c(2, 12)) +
facet_wrap(~continent) +
labs(title = "Year:{frame_time}", x = "GDP per capita", y = "life expectancy") +
transition_time(year) +
ease_aes("linear")

18 / 26

Plotly

suppressPackageStartupMessages({
library(plotly)
library(gapminder)
})
df <- gapminder
fig <- df %>%
plot_ly(
x = ~gdpPercap,
y = ~lifeExp,
size = ~pop,
color = ~continent,
frame = ~year,
text = ~country,
hoverinfo = "text",
type = 'scatter',
mode = 'markers'
) %>% layout(
xaxis = list(
type = "log"
)
)
fig
2510002510k25100k304050607080
AfricaAmericasAsiaEuropeOceaniayear: 1952195219621972198219922002gdpPercaplifeExpPlay
19 / 26

Plotly

fig <- fig %>%
animation_opts(
frame = 1000,
easing = "elastic",
redraw = FALSE
)
fig
2510002510k25100k304050607080
AfricaAmericasAsiaEuropeOceaniayear: 1952195219621972198219922002gdpPercaplifeExpPlay
20 / 26

一些例子

作者:Guy Abel 工具:circlize+tweenr+magick

链接:Animated directional chord diagrams

作者:David Kretch 工具:ggplot2+animation

链接:Show Your Valentine How Much You Care: Create a Statistical Graph

作者:Giulia Ruggeri 工具:gganimate

链接:From static to animated time series: the tidyverse way

作者:Bryan 工具:animation

链接:Animations in R – How to Make Animated Graphs

作者:Garrick Aden‑Buie 工具:gganimate

链接:🤹 tidyexplain – Tidy Animated Verbs

作者:Mickaël Canouil 工具:gganimate

链接:A ggplot2 and gganimate Version of Pac-Man

作者:Jakob Schwalb-Willmann 工具:ggplot2+gifski

链接:moveVis: tools to visualize movement data

21 / 26

如何选择用哪个工具制作动画?

熟练程度(学习曲线)

gifski, magick, animation, plotly, gganimate

22 / 26

如何选择用哪个工具制作动画?

特点

  • 交互性:plotly

  • 基础绘图:gifski, magick, animation

  • ggplot2绘图:gganimate

23 / 26

总结

包外有包

  • anim.plots: animation的封装

  • animint2, echarts4r: javascript

  • tweenr: 插值

3D 动画

  • rgl
24 / 26

谢谢

本幻灯片由 R 包 xaringan 生成

26 / 26

一个好的视觉效果能抓住观众的兴趣并给人留下深刻印象

2 / 26
Paused

Help

Keyboard shortcuts

, , Pg Up, k Go to previous slide
, , Pg Dn, Space, j Go to next slide
Home Go to first slide
End Go to last slide
Number + Return Go to specific slide
b / m / f Toggle blackout / mirrored / fullscreen mode
c Clone slideshow
p Toggle presenter mode
t Restart the presentation timer
?, h Toggle this help
Esc Back to slideshow