Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 将渐变和手绘效果添加到绘图动画中_R_Ggplot2_Textures_Gradient_Gganimate - Fatal编程技术网

R 将渐变和手绘效果添加到绘图动画中

R 将渐变和手绘效果添加到绘图动画中,r,ggplot2,textures,gradient,gganimate,R,Ggplot2,Textures,Gradient,Gganimate,我对将gganimate与渐变、手绘和绘画类型的填充效果一起使用很感兴趣(请参见此处的sketchy and painty and gradients:)。这可能吗?我发现ggrough()能够将ggplot2对象转换为具有这些效果。但是,是否可以使用ggrough或其他东西与gganimate结合使用 即使在baseggplot2(即,不使用gganimate?)中,还有其他方法可以做到这一点吗?请注意,我担心这两个问题的答案都是否定的,特别是对于渐变填充(参见@hadley-hadley-W

我对将
gganimate
与渐变、手绘和绘画类型的填充效果一起使用很感兴趣(请参见此处的sketchy and painty and gradients:)。这可能吗?我发现
ggrough
()能够将
ggplot2
对象转换为具有这些效果。但是,是否可以使用
ggrough
或其他东西与
gganimate
结合使用

即使在base
ggplot2
(即,不使用
gganimate
?)中,还有其他方法可以做到这一点吗?请注意,我担心这两个问题的答案都是否定的,特别是对于渐变填充(参见@hadley-hadley-Wickhams对这个问题的回答:)


或者还有其他解决方案仍然使用
ggplot2
,而不是
gganimate
?我想,如果有可能在base
ggplot2
中,我可以制作许多单独的文件,并将它们缝合在一起,形成一个.gif文件。虽然理论上我可以使用
ggrough

的输出来实现这一点,但另一个选项可能是使用
xkcd
库来获得曲线。它不做弯曲填充,但这是一个开始,并且在正确的美学邻里。我无法使用
gganimate
将其开箱即用,但可以使用
tweenr
(与
gganimate
所依赖的软件包相同)准备数据,并将其输入
gganimate::transition\u手册
,效果良好:

这是怎么做的。鉴于这些虚假数据:

library(tidyverse); library(gganimate)
df <- tibble(Group = rep(1:3, times = 3),
             value = c(1:6, 3:1),
             period = rep(1:3, each = 3))

我在放入
xkcd::xkcdrect
等效项时出错:

# Doesn't work
ggplot() +
  xkcd::xkcdrect(aes(xmin = Group - 0.4, xmax = Group + 0.4,
                     ymin = 0, ymax = value), fill = "gray80",
                 df) +
  transition_states(period)
if(nrow(from)==0&&nrow(to)==0)中出错{:缺少值 如果需要TRUE/FALSE,另外:警告消息:1:In rep(“raw”,length=nrow(from)):使用'length.out'的第一个元素 参数2:In rep(“raw”,length=nrow(to)):使用的第一个元素 “length.out”参数3:In rep(NA_integer,length=nrow(to)):
用于'length.out'参数的第一个元素

但是我们可以通过手动准备tweed数据到达相同的位置,然后将其输入到
transition\u manual

df_tween <- tweenr::tween_states(
  list(df[1:3,],
       df[4:6,],
       df[7:9,],
       df[1:3,]), 3, 1, 'cubic-in-out', 100)


  ggplot() +
  xkcd::xkcdrect(aes(xmin = Group - 0.4, xmax = Group + 0.4,
                     ymin = 0, ymax = value), fill = "gray80",
                 df_tween) +
  transition_manual(.frame)

df_tween这可能是一种rube goldberg解决方案,但我不知道如何优雅地完成。你可以获取数据,使用
tweenr
跨帧插值,然后编写一个函数来渲染每个帧,并使用类似的方法保存输出:然后你可以使用
gifski
组合帧变成gif。很有趣!谢谢你的建议,也谢谢你让我知道
tweenr
。我想这可能行得通。如果你愿意改用python,也许可以绕过ggplot的一些限制-plotnine(ggplot clone)同时支持动画()和对自定义主题的支持。我能找到的最接近的预构建主题是xkcd-(如漫画中的摇摆线等)。您应该能够在matplotlib中向plotnine添加任何参数。感谢您对python-plotnine的介绍!
df_tween <- tweenr::tween_states(
  list(df[1:3,],
       df[4:6,],
       df[7:9,],
       df[1:3,]), 3, 1, 'cubic-in-out', 100)


  ggplot() +
  xkcd::xkcdrect(aes(xmin = Group - 0.4, xmax = Group + 0.4,
                     ymin = 0, ymax = value), fill = "gray80",
                 df_tween) +
  transition_manual(.frame)