简体   繁体   English

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

[英]gganimate: How to make stacked bar chart grow smoothly upwards from x-axis

I'm having trouble animating a stacked static plot.我在为堆叠的 static plot 设置动画时遇到问题。 (See animation below) (见下文 animation)

The animation moves in a volatile way from left to right, instead of each bar growing upwards from the x-axis. animation 从左到右以不稳定的方式移动,而不是每个条从 x 轴向上增长。 The stacked parts also don't grow very smoothly together, which I can't seem to solve.堆叠的部分也不会很顺利地一起生长,我似乎无法解决。

What do I need to change in my code, so that, as the animated plot transitions along the x-axis, the individual bars grow upwards?我需要在我的代码中进行哪些更改,以便随着动画 plot 沿 x 轴过渡,各个条形向上增长? I would also like to have the stacked bars grow more smoothly, so the stacked bar parts aren't "thrown" on top of each other.我还想让堆叠的条更平滑地增长,所以堆叠的条部分不会“扔”在彼此的顶部。

How is that possible?这怎么可能?

The top gif in this article is what I'm looking for, but I don't understand how and where his code differs from mine: ideal animation本文顶部的 gif 是我正在寻找的,但我不明白他的代码与我的代码有何不同以及在哪里:理想 animation

Here is my reprex:这是我的代表:

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

Right now, the bars transition by moving rightwards off of the previous bar.现在,条形图通过从上一个条形向右移动来过渡。 See the current animation here:在此处查看当前的 animation: 当前动画

Thank you in advance for any help!预先感谢您的任何帮助!

There is likely a more correct way to do this, but it can be accomplished by changing the way you construct your initial static plot and using transition_layers() and enter_drift() .可能有更正确的方法可以做到这一点,但可以通过更改构建初始 static plot 的方式并使用transition_layers()enter_drift()来完成。

Note that to simplify my example I am only including years 1996-2000.请注意,为了简化我的示例,我只包括 1996-2000 年。 To animate all years simply copy and paste the geom_bar() sections and change the year being filtered.要为所有年份设置动画,只需复制并粘贴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())

By placing each year-based bar on its own layer, we can use transition_layers() to make them appear one by one while keeping all old layers.通过将每个基于年份的条形图放置在其自己的图层上,我们可以使用transition_layers()使它们一一出现,同时保留所有旧图层。 We use enter_drift() to animate each bar as if it is drifting upwards from below the graph.我们使用enter_drift()为每个条设置动画,就好像它从图表下方向上漂移一样。

The animated output is here: link动画 output 在这里:链接

There is an easier solution.有一个更简单的解决方案。 Consider your "Year" variable as a factor instead of numeric.将您的“年份”变量视为一个因素而不是数字。 This will make the bars grow smoothly upwards just like you want it.这将使酒吧像你想要的那样平稳地向上增长。

Basically use the following chunk for your static plot code and it will work:基本上为您的 static plot 代码使用以下块,它将起作用:

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

I know I am a bit late, but I hope it helps.我知道我有点晚了,但我希望它有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM