本文介绍了gganimate:结合transition_layers和geom_smooth的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将geom_smooth(method = lm)函数与gganimate()的transition_layers结合使用,以便随着各个条形的向上偏移/增长,出现geom_smooth的线性线,如下所示:

解决方案

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

  library(dplyr); library(tidyr)

animate(
df%&%;%
count(year,wt = c,name = c)%&%;%#每年的总计总计
uncount(7,.id = year_disp)%&%;%#制作7份,每年一本
range(year_disp,year)%>%
mutate(year_disp = year_disp + min(df $ year)-1)%&%;%
过滤器(year< = year_disp)%&%;%#仅将年份保持为 year_disp
ggplot(aes(year ,c))+
geom_col(aes(group = year))+# group在这里每年将自身与框架
之间连接起来geom_smooth(method = lm,se = F)+
transition_states(year_disp)+
enter_drift(y_mod = -max(df $ c)),
fps = 10,持续时间= 10,
宽度= 600,高度= 500,渲染器= gifski_renderer())

How do I combine geom_smooth(method = "lm) function with gganimate()'s transition_layers, so that, as the individual bars drift/grow upwards, the linear line of geom_smooth appears, like so: Example of desired appearance of geom_smooth line The only difference is that in my case, instead of the points, the bars would drift upwards as the line appears.

The bars current work well, appearing by drifting upwards, made possible by using the transition_layers function of gganimate.

However, I can't figure out how to add the geom_smooth line, so it appears as the bars grow upwards. Right now, the line appears just at the end, as seen below.

See below for the current look of the animation.

Here is a simple reprex of my problem:

#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())
解决方案

Here's an approach where the data is replicated and then filtered so each version shows progressively more years.

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

这篇关于gganimate:结合transition_layers和geom_smooth的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 16:32