【问题标题】:Cumulative plots in gganimate 0.9.9.9gganimate 0.9.9.9 中的累积图
【发布时间】:2019-03-22 16:44:37
【问题描述】:

我无法找到之前发布的足以回答此问题的问题。在之前的帖子中,已接受的答案使用 shadow_mark 来保持先前渲染的图层的持久性。

How to keep previous layers of data while doing animation in R gganimate?

Keep points in gganimate

在散点图中显示输出时,这是一个不错的解决方法,但它不是累积测量值,并且在尝试执行(例如,堆叠条形图)时会失败。

考虑以下数据。我想建立一个累积堆积条形图,使用我的 df 中的转换状态。

df <- data.frame(t = c(2000, 2000, 2001, 2001, 2002, 2002), 
                 f = c("y", "n", "y", "n", "y", "n"),
                 x = c("a", "a", "b", "c", "a", "c"),
                 y = c(2,3,5,1,4,8))
> df
     t f x y
1 2000 y a 2
2 2000 n a 3
3 2001 y b 5
4 2001 n c 1
5 2002 y a 4
6 2002 n c 8

我想显示 2000 年的数据,在下一层我想将 2001 年的数据添加为上一层的累积数据。同样,对于下一层,我想将 2002 年的数据添加为 2000 年和 2001 年的累积数据。

这说明了为什么 shadow_mark 不是累积数据的解决方案:

ggplot(df, aes(x=x, y=y, fill=f)) +
geom_col() + labs(x=NULL, y=NULL, fill=NULL, title="{closest_state}") +
transition_states(t, transition_length = 2, state_length = 1) +
shadow_mark() + enter_fade() + exit_shrink() + ease_aes('sine-in-out') + theme_bw() 

添加对 shadow_mark 的调用将无法实现累积图的预期结果。 "a" 的总和应该是 9。

可以将数据子集为 c(2000)、c(2000,2001) 和 c(2000,2001,2002) 的 3 个不同的 df,然后在创建新的 states 列后进行 rbind,但这似乎是一种非常老套的方法。

有没有更简洁的方法来使用 gganimate 内置的工具显示累积数据?

【问题讨论】:

    标签: r animation ggplot2 gganimate


    【解决方案1】:

    您可以在数据中创建一个新列,其中包含每年的相加结果并直接绘制它。在下面的代码中,我们使用 cumsum 函数执行此操作。我们还使用complete 来确保f 和x 的每个组合都有一个t 行(在这些添加的行中设置y=0)。如果我们不这样做,当f 和x 的某些组合缺少某些年份(t 值)时,累积总和将不正确。所有数据转换都是通过dplyr 管道即时完成的:

    library(tidyverse)
    library(gganimate)
    
    ggplot(df %>% 
             complete(t, nesting(f, x), fill=list(y=0)) %>% 
             arrange(t) %>%  
             group_by(x,f) %>%
             mutate(y_cum = cumsum(y)), 
           aes(x=x, y=y_cum, fill=f)) +
      geom_col() + 
      labs(x=NULL, y=NULL, fill=NULL, title="{closest_state}") +
      transition_states(t, transition_length = 2, state_length = 1) +
      enter_fade() + ease_aes('sine-in-out') + 
      theme_bw() +
      scale_y_continuous(breaks=0:10)
    

    【讨论】:

      【解决方案2】:

      我发现直方图的诀窍是复制早期的转换值以确保累积构建。

      # packages my mac needs for gganimate to work
      if (!require("pacman")) install.packages("pacman")
      pacman::p_load(dplyr, gganimate, gifski, png)
      
      # vector of values to plot in histogram
      sampling_dist_v1 <- rnorm(1e3)
      
      # create a transition sequence variable
      init_seq <- c(1, rep(2,10), rep(3,10))
      
      observed_rates <- 
        tibble(
          observed_rate = sampling_dist_v1,
          transition_sequence = c(init_seq, rep(4, length(sampling_dist_v1) - length(init_seq)))
        )
      
      # duplicate earlier entries to ensure animation is cumulative
      t_sub_4 <- 
        observed_rates %>% 
        filter(transition_sequence < 4) %>% 
        mutate(transition_sequence = 4)
      
      t_sub_3 <- 
        observed_rates %>% 
        filter(transition_sequence < 3) %>% 
        mutate(transition_sequence = 3)
      
      t_sub_2 <- 
        observed_rates %>% 
        filter(transition_sequence < 2) %>% 
        mutate(transition_sequence = 2)
      
      observed_rates <- 
        bind_rows(
          observed_rates,
          t_sub_4, 
          t_sub_3, 
          t_sub_2
        ) 
      
      # animate
      anim <- observed_rates %>% 
        ggplot(aes(x = observed_rate)) + 
        geom_histogram(binwidth = .25, fill = 'blue') + 
        transition_states(
          transition_sequence,
          state_length = 4,
          wrap = FALSE
          )
      
      [![enter image description here][1]][1]
      
      
        [1]: https://i.stack.imgur.com/vyJ8m.gif
      

      【讨论】:

        猜你喜欢
        • 2020-02-14
        • 2014-06-21
        • 1970-01-01
        • 2022-12-16
        • 2019-03-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多