gganimate:结合 transition_layers 和 geom_smooth

gganimate: Combining transition_layers and geom_smooth

我如何将 geom_smooth(method = "lm) 函数与 gganimate() 的 transition_layers 结合起来,以便随着单个柱 drift/grow 向上,线性线geom_smooth 的出现,如下所示:Example of desired appearance of geom_smooth line 唯一的区别是,在我的情况下,条形图会随着线的出现向上漂移,而不是点。

当前的条形效果很好,通过向上漂移出现,通过使用 gganimate 的 transition_layers 功能成为可能。

但是,我不知道如何添加 geom_smooth 行,所以它显示为条形向上增长。现在,该行出现在最后,如下所示。

查看下面的动画当前外观。

这是我的问题的一个简单代表:

#Df for reprex
library(ggplot2)
library(tidyverse)

year <- as.numeric(c(1996:2002,
                     1996:2002,
                     1996:2002))
c <- c(39, 40, 67, 80, 30, 140, 90, 23, 100, 123,
       140, 1, 2, 1, 13, 3, 3, 30, 1, 3, 3)
df <- data.frame(year, c) %>%
  select(year, c) %>%
  arrange(year)

#Static plot
(static_plot <- ggplot(data = df) +
    geom_bar(data = df %>% filter(year == 1996), stat="identity", position ="stack",
             aes(x = year, y = c)) +
    geom_bar(data = df %>% filter(year == 1997), stat="identity", position ="stack",
             aes(x = year, y = c)) +
    geom_bar(data = df %>% filter(year == 1998), stat="identity", position ="stack",
             aes(x = year, y = c)) +
    geom_bar(data = df %>% filter(year == 1999), stat="identity", position ="stack",
             aes(x = year, y = c)) +
    geom_bar(data = df %>% filter(year == 2000), stat="identity", position ="stack",
             aes(x = year, y = c)) +
    geom_bar(data = df %>% filter(year == 2001), stat="identity", position ="stack",
             aes(x = year, y = c)) +
    geom_bar(data = df %>% filter(year == 2002), stat="identity", position ="stack",
             aes(x = year, y = c)) +
  labs(y = "year",
       x = "c",
       title = "Reprex") +
  geom_smooth(df, mapping = aes(x = year, y = c), method = "lm",
              colour = "black", se = F)
  )

#Animation
library(gganimate)
anim <- static_plot +
  transition_layers(layer_length = 1, transition_length = 1) +
  enter_drift(x_mod = 0, y_mod = -max(df$c))

animate(anim, fps = 10, duration = 10,
        width = 600, height = 500, renderer = gifski_renderer())

geom-line是在最后计算的,因此它只出现在最后。每次计算 geom-bar 后,您还必须计算 geom-line,以便该线与 Bars 增长同时出现。

geom_bar(data = df %>% filter(year == 1997), stat="identity", position ="stack",
             aes(x = year, y = c)) +
geom_line(filter(df, year %in% c(1996, 1997)), mapping = aes(x = year, y = lm),
                colour = "black")

多年来一直这样做,您应该会得到预期的结果!

这是一种复制数据然后过滤数据的方法,因此每个版本逐渐显示更多的年份。

library(dplyr); library(tidyr)

animate(
  df %>%
    count(year, wt = c, name = "c") %>%   # Aggregate for each year's total
    uncount(7, .id = "year_disp") %>%     # Make 7 copies, one for each year
    arrange(year_disp, year) %>% 
    mutate(year_disp = year_disp + min(df$year) - 1) %>%
    filter(year <= year_disp) %>%         # Only keep years up to "year_disp"
    ggplot(aes(year, c)) +
    geom_col(aes(group = year)) +   # "group" here connects each year to itself between frames
    geom_smooth(method = "lm", se = F) +
    transition_states(year_disp) +
    enter_drift(y_mod = -max(df$c)),
  fps = 10, duration = 10,
  width = 600, height = 500, renderer = gifski_renderer())