为 gganimate plot 添加渐变和手绘效果
Add gradients and hand-drawn effects to gganimate plot
我有兴趣使用 gganimate
渐变和手绘和绘画类型的填充效果(请在此处查看粗略和绘画和渐变:https://semiotic.nteract.io/guides/sketchy-painty-patterns). Is this possible? I have found that ggrough
(https://xvrdm.github.io/ggrough/)能够转换 ggplot2
对象具有这些类型的效果。但是,是否可以使用 ggrough
或其他一些东西与 gganimate
结合使用?
有没有另一种方法可以做到这一点,即使是在基础 ggplot2
中(即不使用 gganimate
?)请注意,我担心这两个问题的答案是否定的,尤其是渐变填充(参见@hadley Hadley Wickhams 对这个问题的回答:How to add texture to fill colors in ggplot2)。
或者是否有另一种解决方案仍然使用 ggplot2
,而不是 gganimate
?我想如果有可能在 base ggplot2
中,我可以制作许多单独的文件并将它们拼接在一起以制作一个 .gif。虽然理论上我可以使用 ggrough
.
的输出来做到这一点
另一种选择可能是使用 xkcd
库来获得波浪线。它不会做波浪形填充,但它是一个开始,并且处于正确的审美环境中。我无法使用 gganimate
开箱即用,但可以使用 tweenr
(gganimate
依赖的相同包)准备您的数据并将其输入 gganimate::transition_manual
效果不错:
方法如下。鉴于此虚假数据:
library(tidyverse); library(gganimate)
df <- tibble(Group = rep(1:3, times = 3),
value = c(1:6, 3:1),
period = rep(1:3, each = 3))
我们可以用 geom_rect
和 gganimate::transition_states
制作动画。 geom_col
在这里会更容易,但我想稍后显示与 xkcd::xkcdrect
的相似性。
ggplot() +
geom_rect(data = df,
aes(xmin = Group - 0.4, xmax = Group + 0.4,
ymin = 0, ymax = value),
fill = "gray80") +
transition_states(period)
当我放入 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)
Error in if (nrow(from) == 0 && nrow(to) == 0) { : missing value
where TRUE/FALSE needed In addition: Warning messages: 1: In
rep("raw", length = nrow(from)) : first element used of 'length.out'
argument 2: In rep("raw", length = nrow(to)) : first element used of
'length.out' argument 3: In rep(NA_integer_, length = nrow(to)) :
first element used of 'length.out' argument
但是我们可以通过手动准备补间数据来到达同一个地方,然后将其输入 transition_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)
我有兴趣使用 gganimate
渐变和手绘和绘画类型的填充效果(请在此处查看粗略和绘画和渐变:https://semiotic.nteract.io/guides/sketchy-painty-patterns). Is this possible? I have found that ggrough
(https://xvrdm.github.io/ggrough/)能够转换 ggplot2
对象具有这些类型的效果。但是,是否可以使用 ggrough
或其他一些东西与 gganimate
结合使用?
有没有另一种方法可以做到这一点,即使是在基础 ggplot2
中(即不使用 gganimate
?)请注意,我担心这两个问题的答案是否定的,尤其是渐变填充(参见@hadley Hadley Wickhams 对这个问题的回答:How to add texture to fill colors in ggplot2)。
或者是否有另一种解决方案仍然使用 ggplot2
,而不是 gganimate
?我想如果有可能在 base ggplot2
中,我可以制作许多单独的文件并将它们拼接在一起以制作一个 .gif。虽然理论上我可以使用 ggrough
.
另一种选择可能是使用 xkcd
库来获得波浪线。它不会做波浪形填充,但它是一个开始,并且处于正确的审美环境中。我无法使用 gganimate
开箱即用,但可以使用 tweenr
(gganimate
依赖的相同包)准备您的数据并将其输入 gganimate::transition_manual
效果不错:
方法如下。鉴于此虚假数据:
library(tidyverse); library(gganimate)
df <- tibble(Group = rep(1:3, times = 3),
value = c(1:6, 3:1),
period = rep(1:3, each = 3))
我们可以用 geom_rect
和 gganimate::transition_states
制作动画。 geom_col
在这里会更容易,但我想稍后显示与 xkcd::xkcdrect
的相似性。
ggplot() +
geom_rect(data = df,
aes(xmin = Group - 0.4, xmax = Group + 0.4,
ymin = 0, ymax = value),
fill = "gray80") +
transition_states(period)
当我放入 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)
Error in if (nrow(from) == 0 && nrow(to) == 0) { : missing value where TRUE/FALSE needed In addition: Warning messages: 1: In rep("raw", length = nrow(from)) : first element used of 'length.out' argument 2: In rep("raw", length = nrow(to)) : first element used of 'length.out' argument 3: In rep(NA_integer_, length = nrow(to)) :
first element used of 'length.out' argument
但是我们可以通过手动准备补间数据来到达同一个地方,然后将其输入 transition_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)