【问题标题】:gganimate barchart: smooth transition when bar is replacedgganimate barchart:替换bar时平滑过渡
【发布时间】:2019-07-06 11:19:57
【问题描述】:

我想用 gganimate 包创建一个动画条形图。条形图应包含 4 个条形图,但只能同时显示三个条形图。当一个条退出并进入一个新条时,动画应该是平滑的(就像两个条在图中切换位置时一样)。

考虑以下示例:

# Set seed
set.seed(642)

# Create example data
df <- data.frame(ordering = c(rep(1:3, 2), 3:1, rep(1:3, 2)),
                 year = factor(sort(rep(2001:2005, 3))),
                 value = round(runif(15, 0, 100)),
                 group = c(letters[sample(1:4, 3)],
                           letters[sample(1:4, 3)],
                           letters[sample(1:4, 3)],
                           letters[sample(1:4, 3)],
                           letters[sample(1:4, 3)]))

# Load packages
library("gganimate")
library("ggplot2")

# Create animated ggplot
ggp <- ggplot(df, aes(x = ordering, y = value)) +
  geom_bar(stat = "identity", aes(fill = group)) +
  transition_states(year, transition_length = 2, state_length = 0)
ggp

如果一个条被交换,条的颜色只是改变,没有任何平滑的动画(即新条应该从侧面飞入,而被替换的条应该飞出)。

问题:如何才能顺利更换钢筋?

【问题讨论】:

    标签: r animation ggplot2 bar-chart gganimate


    【解决方案1】:

    我在 2003 年遇到了一些小故障(b 和 c 似乎在过渡时交换了),但希望这可以帮助您更接近。我认为enter_driftexit_drift 是你要找的。​​p>

    library("gganimate")
    library("ggplot2")
    ggp <- ggplot(df, aes(x = ordering, y = value, group = group)) +
      geom_bar(stat = "identity", aes(fill = group)) +
      transition_states(year, transition_length = 2, state_length = 0) + 
      ease_aes('quadratic-in-out') +   # Optional, I used to see settled states clearer
      enter_drift(x_mod = -1) + exit_drift(x_mod = 1) +
      labs(title = "Year {closest_state}")
    animate(ggp, width = 600, height = 300, fps = 20)
    

    【讨论】:

    • 非常感谢,正是我想要的!
    • 哦,祝福你。我需要enter_grow,但我不知道如何要求。这么多的互联网搜索,你的回答非常有帮助。
    猜你喜欢
    • 2019-04-06
    • 2021-06-30
    • 2015-02-14
    • 1970-01-01
    • 1970-01-01
    • 2020-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多