Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

【系统】动画视频 #15

Open
XiangyunHuang opened this issue Sep 10, 2019 · 3 comments
Open

【系统】动画视频 #15

XiangyunHuang opened this issue Sep 10, 2019 · 3 comments

Comments

@XiangyunHuang
Copy link
Owner

这一节升级为视频动画

除了介绍 animation 包依赖 FFmpeg/ SWF Tools/ ImageMagick/ GraphicsMagick 做动画视频外,补充 avgganimate 借助 cargo 和系统库 libavfilter-dev,就是要去掉安装和配置软件的麻烦,话说 gganimate 现在很火

@XiangyunHuang
Copy link
Owner Author

这一节一定要找一个很棒的例子才动笔

很有意思的真实数据,很酷炫的效果

mapmate 不再维护了,有点可惜,可能是为了支持 gganimate,作者主页上的 demo 很炫 https://leonawicz.github.io/

@XiangyunHuang XiangyunHuang changed the title 【系统】动画 【系统】动画视频 Sep 11, 2019
@XiangyunHuang
Copy link
Owner Author

XiangyunHuang commented Oct 17, 2019

这个例子源于余光创的博客https://guangchuangyu.github.io/cn/2017/09/3d-breast/ 这种类型的例子不适合放在书里面

$$f(x;\theta,\phi) = \theta x\log(x)-\frac{1}{\phi}\mathit{e}^{-\phi^4(x-\frac{1}{\mathit{e}})^4}$$

其中 $\theta \in (2,3), \phi \in (30,50), x \in (0,1)$

# 准备数据
breast_curve <- function(y, theta, phi) {
  theta * y * log(y, base = 10) - 1 / phi * exp(-(phi * y - phi / exp(1))^4)
}

tmp <- sapply(0:100 / 101, breast_curve,
  theta = c(3, 2.8, 2.5, 2.2, 2),
  phi = c(30, 33, 36, 40, 50)
)

dat <- data.frame(
  x = c(t(tmp)), y = rep(0:100 / 101, 5),
  cols = rep(hcl.colors(5), each = 101)
)

dat1 <- split(dat, dat$cols)
# 中间过程
library(tweenr)
dat2 <- tween_states(dat1,
  tweenlength = 2, statelength = 1,
  ease = rep("cubic-in-out", 5), nframes = 101
)

cubic-in-out 的插值

tweenr::display_ease('cubic-in-out')
library(ggplot2)
ggplot(data = dat2, aes(x, y, color = I(cols))) +
  geom_path(na.rm = TRUE) +
  coord_flip() +
  theme_minimal() +
  labs(x = NULL, y = NULL)

GIF 动画

# 动画效果
library(gganimate)
ggplot(data = dat2, aes(x, y, color = I(cols))) +
  geom_path(na.rm = TRUE) +
  coord_flip() +
  theme_minimal() +
  labs(x = NULL, y = NULL, title = 'Frame {frame} of {nframes}') +
  # 转场特效
  transition_time(.frame) +
  ease_aes('cubic-in-out')

[gganimate 动画制作,以 mtcars 为例介绍 tweenr 通过插值生成中间过渡帧]{.todo}

生成静态图片

ggplot(mtcars, aes(disp, mpg)) +
  geom_point(colour = "purple", size = 3) +
  facet_wrap(~cyl)

用来分面的变量 cyl 作为转场的帧

library(gganimate)
ggplot(mtcars, aes(disp, mpg)) +
  geom_point(colour = "purple", size = 3) +
  facet_wrap(~cyl) +
  transition_manual(cyl) +
  labs(title = "{current_frame}")
# p <- ggplot(mtcars, aes(disp, mpg)) +
#   geom_point(colour = "purple", size = 3) +
#   facet_wrap(~cyl) +
#   transition_manual(cyl) +
#   labs(title = "{current_frame}")
# animate(p, nframes = 100, fps = 50, renderer = gifski_renderer())

@XiangyunHuang
Copy link
Owner Author

影响动画在线生成的因素

目前 gitbook 版 dev: "svglite" 即默认情况下,代码生成的图形保存设备为 svglite::svglite()

每章开头都会加载

knitr::opts_chunk$set(
  fig.align = "center",
  cache = TRUE,
  small.mar = TRUE,
  fig.showtext = TRUE,
  dpi = 300
)

意味着启用 fig.showtext=TRUE 处理中文字体,会自己打开一套图形保存设备

@XiangyunHuang XiangyunHuang transferred this issue from XiangyunHuang/msg Dec 23, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant