gganimate:如何使堆叠的条形图从x轴向上平滑增长

时间:2019-09-20 08:30:44

标签: r animation ggplot2 data-visualization gganimate

我无法为堆叠的静态图制作动画。 (请参见下面的动画)

动画从左到右以不稳定的方式移动,而不是每个条从x轴向上增长。堆叠的部分也不能一起非常顺利地生长,这似乎无法解决。

我需要在代码中进行哪些更改,以便在动画图沿x轴过渡时,各个条形向上增长?我还希望堆叠的条形更加平稳地生长,以使堆叠的条形部分不会彼此“抛出”。

那怎么可能?

本文所要寻找的是最喜欢的gif,但我不明白他的代码与我的代码有何不同之处:ideal animation

这是我的代表:

#Static df
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
year <- as.numeric(c(1996:2015,
                       1996:2015,
                       1996:2015))
c <- c(39, 40, 67, 80, 30, 140, 90, 23, 100, 123,
       140, 160, 100, 89, 173, 200, 32, 90, 100, 211,
       1, 2, 1, 13, 3, 3, 30, 1, 3, 3,
       1, 1, 20, 2, 1, 10, 1, 2, 1, 1,
       1, 3, 1, 2, 1, 3, 1, 3, 6, 1,
       1, 30, 1, 1, 8, 9, 1, 32, 1, 1)
cat <- as.character(c("out", "out", "out", "out", "out", "out", "out", "out", "out", "out",
                   "out", "out", "out", "out", "out", "out", "out", "out", "out", "out",
                   "in", "in", "in", "in", "in", "out", "in", "in", "in", "in",
                   "in", "in", "in", "in", "in", "in", "in", "in", "in", "in",
                   "other", "other", "other", "other", "other", "other", "other", "other", "other", "other",
                   "other", "other", "other", "other", "other", "other", "other", "other", "other", "other"))
cat <- as.factor(cat)
static_df_test <- data.frame(year, c, cat) %>%
  select(year, cat, c) %>%
  arrange(year, cat)


#Static plot
library(ggplot2)
(static_plot <- ggplot(static_df_test) +
    geom_bar(data = static_df_test, stat="identity", position ="stack",
             aes(x = year, y = c, fill = cat)) +
    scale_x_continuous(breaks = seq(1996, 2015, 1),
                       expand = c(0.003, 0.003),
                       labels = c(1996, 97, 98, 99, 2000,
                                  "01", "02", "03", "04", "05", "06",
                                  "07", "08", "09", 10, 11, 12, 13, 14, 15)) +
    scale_y_continuous(breaks = seq(0, 250, 25),
                       expand = c(0,0),
                       limits = c(0,250)) +
    labs(x = "year", y = "c")
)

#Animated plot
library(gganimate)
library(ggplot2)
ani <- (static_plot) +
  transition_time(as.integer(year)) +
  enter_grow() + shadow_mark(past = TRUE)

animate(ani, fps = 8, 50, duration = 15,
        width = 1500, height = 750, renderer = gifski_renderer())

现在,这些条形通过向右移出上一个条形而过渡。在此处查看当前动画:Current animation

在此先感谢您的帮助!

2 个答案:

答案 0 :(得分:1)

执行此操作的方法可能更正确,但是可以通过更改构建初始静态图的方式并使用transition_layers()enter_drift()来实现。

请注意,为简化我的示例,我仅包括1996-2000年。要为所有年份设置动画,只需复制并粘贴geom_bar()部分并更改要过滤的年份即可。

#Static plot
library(ggplot2)
(static_plot <- ggplot(static_df_test) +
    geom_bar(data = static_df_test %>% filter(year == 1996), stat="identity", position ="stack",
             aes(x = year, y = c, fill = cat)) +
    geom_bar(data = static_df_test %>% filter(year == 1997), stat="identity", position ="stack",
             aes(x = year, y = c, fill = cat)) +
    geom_bar(data = static_df_test %>% filter(year == 1998), stat="identity", position ="stack",
             aes(x = year, y = c, fill = cat)) +
    geom_bar(data = static_df_test %>% filter(year == 1999), stat="identity", position ="stack",
             aes(x = year, y = c, fill = cat)) +
    geom_bar(data = static_df_test %>% filter(year == 2000), stat="identity", position ="stack",
             aes(x = year, y = c, fill = cat)) +
    scale_x_continuous(breaks = seq(1996, 2015, 1),
                       expand = c(0.003, 0.003),
                       labels = c(1996, 97, 98, 99, 2000,
                                  "01", "02", "03", "04", "05", "06",
                                  "07", "08", "09", 10, 11, 12, 13, 14, 15)) +
    scale_y_continuous(breaks = seq(0, 250, 25),
                       expand = c(0,0),
                       limits = c(0,250)) +
    labs(x = "year", y = "c")
)

#Animated plot
library(gganimate)
library(ggplot2)

ani <- (static_plot) +
  transition_layers(layer_length = 1, transition_length = 2) +
  enter_drift(x_mod = 0, y_mod = -max(static_df_test$c))

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

通过将每个基于年的条形图放置在其自己的层上,我们可以使用transition_layers()使其在保留所有旧层的同时一个接一个地显示。我们使用enter_drift()为每个条设置动画,好像它从图形下方向上漂移一样。

动画输出在这里: link

答案 1 :(得分:0)

有一个更简单的解决方案。将您的“年份”变量视为一个因素而不是数字。这将使条形像您想要的那样平滑向上生长。

基本上将以下块用于您的静态绘图代码,它会起作用:

static_plot <- ggplot(static_df_test) +
    geom_bar(data = static_df_test, stat="identity", position ="stack",
             aes(x = as.factor(year), y = c, fill = cat)) +
    scale_y_continuous(breaks = seq(0, 250, 25),
                       expand = c(0,0),
                       limits = c(0,250)) +
    labs(x = "year", y = "c")

我知道我有点晚了,但我希望它有所帮助。