gganimate时间序列和两线图 [英] gganimate time series and two line plot

查看:54
本文介绍了gganimate时间序列和两线图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

遵循源代码

following the source code Here. I am trying to replicate it with my data. I have a time-series of data, where I'm plotting active and non-active number on the y-axis. here is my sample data structure:

df <- tibble::tribble(
  ~Date, ~active, ~non_active,
      1,                 848,             335,
      2,                 998,             280,
      3,                1096,             308,
      4,                1127,             274,
      5,                1022,             313,
      6,                 973,             351,
      7,                1131,             302,
      8,                1165,             312,
      9,                1159,             293,
     10,                1192,             311,
     11,                1221,             332,
     12,                1075,             369,
     13,                1056,             416,
     14,                1219,             356,
     15,                1240,             363,
     16,                1270,             376,
     17,                1302,             325,
     18,                1292,             346,
     19,                1104,             374,
     20,                1084,             413,
     21,                1257,             350,
     22,                1306,             356,
     23,                1318,             368,
     24,                1380,             378,
     25,                1350,             388,
     26,                1163,             421,
     27,                1158,             468,
     28,                1368,             410,
     29,                1429,             423,
     30,                1514,             456,
     31,                1564,             434
  )

I am stumped on how to create the second line tracker for the lower line. What am I am missing here? your feedback/help would be appreciated!

My code:

library(gganimate)
library(dplyr)
library(tibbletime)
library(gifski)
library(ggplot2)
library(png)

p <- ggplot(df, aes(Date, active)) +
  geom_line(aes(y = active)) +
  geom_line(aes(y = non_active))+
  geom_segment(aes(xend = 15, yend = active), linetype = 2, colour = 'blue') +
  geom_segment(aes(xend = 15, yend = non_active), linetype = 2, colour = 'red') +
  geom_point(size = 3) + 
  geom_text(aes(x = 15.1, label = active ), hjust = 0) +
  transition_reveal(Date) + 
  # labs(title = "Date: {frame_time}") +
  view_follow(fixed_y = TRUE)+
  coord_cartesian(clip = 'off') + 
  labs(title = 'Active in Jan', y = 'Individual Active') +
  enter_drift(x_mod = -1) + exit_drift(x_mod = 1) +
  theme_bw() +
  theme(
    panel.border = element_blank(),
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    axis.line = element_line(colour = "black")
  )+
  # theme_minimal() +
  theme(plot.margin = margin(5.5, 40, 5.5, 5.5))

animate(p, fps=5)

解决方案

First, you need to stack the active and non_active variables of df and then to create a group variable grp (a factor with two categories):

df2 <- data.frame(Date=rep(df$Date, 2), 
                  act_noact=c(df$active, df$non_active), 
                  grp=rep(c("Active","Non active"), each=nrow(df)))

Then, you can plot the animated plot using the new df2 dataframe with the following code:

p <- ggplot(df2, aes(x=Date, y=act_noact, group=grp)) +
  geom_line() +
  geom_segment(aes(xend=max(Date), yend = act_noact), linetype=2, colour='blue') +
  geom_point(size = 3) + 
  geom_text(aes(x = max(Date)+.1, label = sprintf("%5.0f", act_noact)), hjust=0) +
  transition_reveal(Date) + 
  view_follow(fixed_y = TRUE)+
  coord_cartesian(clip = 'off') + 
  labs(title = 'Active in Jan', y = 'Individual Active') +
  enter_drift(x_mod = -1) + exit_drift(x_mod = 1) +
  theme_bw() +
  theme(panel.border = element_blank(),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        axis.line = element_line(colour = "black"),
        plot.margin = margin(5.5, 40, 5.5, 5.5))

    animate(p, fps=5)

这篇关于gganimate时间序列和两线图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆