【问题标题】:Is there a way to make geoms fade but persist in gganimate?有没有办法让geoms褪色但坚持gganimate?
【发布时间】:2020-10-06 15:15:16
【问题描述】:

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

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)

这给出了以下输出:

在这里,我同时使用 exit_fade 来让点逐渐消失,但也使用 shadow_mark 设置 alpha 以使数据持续存在。这基本上非常接近我想要的。

问题在于,这里真正发生的事情是,gganimate 在数据点首次出现在 geom_point 下方后立即将shadow_mark 置于打开状态,然后在原始点消失时它变得缓慢可见。如果我只是想让点淡出但坚持下去,这基本上没问题(尽管淡入淡出过渡有点奇怪,因为原始的阿尔法和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)

这给出了以下内容:

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

问题的原因是我最终希望同时平滑exit_fadeexit_shrink,使得收缩后的最终尺寸与示例中shadow_mark的较小尺寸相同然后这个点在动画的其余部分以较小的、半褪色的状态持续存在。

注意:我意识到我可以uncount 数据行并在数据中手动制作我自己的框架和转换,并允许这些点以这种方式持续存在,正如在其他 @ 987654323@。问题是我的真实数据集实际上非常大,并且像这样进行计数会导致数据集太大而无法存储。因此,如果存在的话,我更喜欢纯 gganimate 解决方案。

【问题讨论】:

    标签: r ggplot2 gganimate


    【解决方案1】:

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

    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()
    

    【讨论】:

      猜你喜欢
      • 2011-05-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多