gganimate:结合transition_layers和geom_smooth

时间:2019-09-25 21:58:56

标签: r animation ggplot2 plot gganimate

如何将geom_smooth(method =“ lm)函数与gganimate()的transition_layers结合使用,以便随着各个条形的向上偏移/增长,出现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())

Current animation

2 个答案:

答案 0 :(得分:6)

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

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

enter image description here

答案 1 :(得分:0)

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

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