gpt4 book ai didi

r - 有没有办法让 geoms 褪色但坚持 gganimate?

转载 作者:行者123 更新时间:2023-12-04 03:55:54 24 4
gpt4 key购买 nike

我有如下地理数据(但数据量大得多):

library(tidyverse)
library(gganimate)

n <- 500

longitude <- runif(n)
latitude <- runif(n)
time <- round(runif(n, 0, 100),1) %>% sort
data <- tibble(longitude,latitude) %>% arrange(longitude) %>% mutate(time = time)

我可以用 gganimate 制作动画如下:

anim1 <- ggplot(data, aes(x=longitude, y=latitude, group = time)) + 
geom_point(color = "red", size = 10) +
transition_components(time, exit_length = 30) +
exit_fade() +
shadow_mark(color = "red" ,alpha = 0.1, size = 10)

animate(anim1, nframes = 100)

这给出了以下输出:

enter image description here

在这里,我同时使用 exit_fade 来使点淡出,还使用 ​​shadow_mark 和设置的 alpha 来使数据持续存在。这基本上非常非常接近我想要的。

问题在于,这里真正发生的是 gganimate 在数据点首次出现在 geom_point 下方后立即放置一个 shadow_mark,然后在原始点消失时它慢慢变得可见。如果我只是想让点淡出但持续存在,这基本上是可以的(尽管淡入淡出过渡有点奇怪,因为原始的 alpha 和 shadow_mark 在淡出过程中在视觉上加在一起)。但它变成了一个更大的问题,因为有时我还需要调整点的大小。

如果我使用一个小于原始点的shadow_mark,你可以看到问题:

anim2 <- ggplot(data, aes(x=longitude, y=latitude, group = time)) + 
geom_point(color = "red", size = 10) +
transition_components(time, exit_length = 30) +
exit_fade() +
shadow_mark(color = "red" ,alpha = 0.1, size = 2)

animate(anim2, nframes = 100)

给出以下内容:

enter image description here

您可以看到较小的点出现而较大的点逐渐消失(这里为了简单起见我省略了同时调整大小,但在这种情况下问题仍然存在)。所以我想知道是否有一种方法可以在 gganimate 中“正确地”执行此操作,使得该点实际上只是消失但仍然存在,而 shadow_mark 只是在下面绘制并通过逐渐消失慢慢显露出来原点?

问题的原因是我最终想同时平滑exit_fadeexit_shrink,这样收缩后的最终大小与上例中的 shadow_mark 尺寸较小,然后该点在整个动画的其余部分一直保持较小的半淡化状态。

注意:我意识到我可以uncount 数据行并在数据中手动制作我自己的帧和转换,并允许点以这种方式持续存在,就像在其他讨论中answers .问题是我的真实数据集实际上非常大,像这样进行计数会导致数据集对于内存来说太大了。因此,如果存在的话,我更喜欢纯 gganimate 解决方案。

最佳答案

以下方法将数据集复制了两次,这确实会导致文件大小稍微膨胀,但可能不会像不计算那样出现问题。看看它是否适合您?

data <- data %>%
mutate(id = seq(1, n()), # add an ID column to original dataset,
type = "original",
size = 10, # specify initial size / alpha / any other aspect
alpha = 1) # to be changed during animation

rbind(data,

data %>% # create second version of the dataset,
mutate(type = "transiting", # with time lagged by the desired transition
time = time + 30, # amount (30 in the question's example),
size = size * 0.2, # & size / alpha / any other aspect defined
alpha = alpha * 0.1), # according to end state after transition;

data %>% # create third version of the dataset,
mutate(type = "persisting", # which should be identical to the second,
time = max(time) + 30, # except that the time is set to the max time
size = size * 0.2, # across all rows.
alpha = alpha * 0.1)) %>%

# pass this combined dataset to ggplot & animate as per normal with explicitly
# specified parameters for size / alpha / etc, & group aesthetic set to ID value.
# no need to specify exit_fade or shadow_mark now, as each point DOESN'T exit at all.
ggplot(aes(x = longitude, y = latitude, group = id,
size = size, alpha = alpha)) +
geom_point(colour = "red") +
transition_components(time) +
ggtitle("{frame_time}") + # optional; added to illustrate frame time explicitly
scale_size_identity() +
scale_alpha_identity()

animated

关于r - 有没有办法让 geoms 褪色但坚持 gganimate?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63913059/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com