首页 > 解决方案 > gganimate::transition_time() 在不存在的观测值之间转换

问题描述

我正在尝试更熟悉这个gganimate包。在这样做的过程中,我注意到这transition_time(time)似乎不一定使用确切的值,time而是填充。在下面的变量中data.frame有 41 个日期。time据我了解,我希望transition_time(time)只在这 41 个日期之间进行转换。

在下面的代码中,我还使用geom_rect基于category. 查看输出的df.gif,当背景为白色时,transition_time(time)似乎使用了一个time没有真正观察到的值。此外,在某些情况下,观察似乎是一种“推算”,即真实观察之间存在过渡,其间有多个步骤,其中没有观察。

也许这不是问题,但由于我不了解它的实际工作原理。无论如何,非常感谢任何指针,谢谢!

df <- structure(list(time = structure(c(12174, 12179, 12180, 12181, 
12189, 12191, 12193, 12198, 12199, 12200, 12201, 12202, 12203, 
12205, 12206, 12207, 12208, 12209, 12210, 12211, 12212, 12213, 
12214, 12215, 12216, 12217, 12218, 12219, 12220, 12221, 12222, 
12223, 12224, 12225, 12226, 12227, 12228, 12229, 12230, 12231, 
12232), class = "Date"), lon = c(37.0615319203799, 37.0716477974754, 
37.0615319203799, 37.0923434723092, 37.0923434723092, 37.0507170659893, 
37.1042645986751, 37.1042645986751, 37.0716477974754, 37.0507170659893, 
37.0507170659893, 37.0923434723092, 37.0716477974754, 37.047738185691, 
37.0906383964751, 37.066505639265, 37.1069135660838, 37.0808814378127, 
37.066505639265, 37.0808814378127, 37.1069135660838, 37.066505639265, 
37.1069135660838, 37.0906383964751, 37.047738185691, 37.1069135660838, 
37.1069135660838, 37.0808814378127, 37.066505639265, 37.0808814378127, 
37.047738185691, 37.066505639265, 37.0906383964751, 37.047738185691, 
37.1069135660838, 37.0808814378127, 37.066505639265, 37.0906383964751, 
37.066505639265, 37.0906383964751, 37.0808814378127), lat = c(-2.7411848459376, 
-2.71292701762913, -2.7411848459376, -2.72165547518141, -2.72165547518141, 
-2.73329474303528, -2.71147633070969, -2.71147633070969, -2.71292701762913, 
-2.73329474303528, -2.73329474303528, -2.72165547518141, -2.71292701762913, 
-2.73075363771866, -2.71197727883059, -2.7343739141538, -2.71507451885925, 
-2.71061194970373, -2.7343739141538, -2.71061194970373, -2.71507451885925, 
-2.7343739141538, -2.71507451885925, -2.71197727883059, -2.73075363771866, 
-2.71507451885925, -2.71507451885925, -2.71061194970373, -2.7343739141538, 
-2.71061194970373, -2.73075363771866, -2.7343739141538, -2.71197727883059, 
-2.73075363771866, -2.71507451885925, -2.71061194970373, -2.7343739141538, 
-2.71197727883059, -2.7343739141538, -2.71197727883059, -2.71061194970373
), group = c("D", "A", "D", "E", "E", "C", "B", "B", "A", "C", 
"C", "E", "A", "C", "A", "D", "B", "E", "D", "E", "B", "D", "B", 
"A", "C", "B", "B", "E", "D", "E", "C", "D", "A", "C", "B", "E", 
"D", "A", "D", "A", "E"), category = c("High", "High", "High", 
"High", "High", "High", "High", "High", "High", "High", "High", 
"High", "High", "Low", "Low", "Low", "Low", "Low", "Low", "Low", 
"Low", "Low", "Low", "Low", "Low", "Low", "Low", "Low", "Low", 
"Low", "Low", "Low", "Low", "Low", "Low", "Low", "Low", "Low", 
"Low", "Low", "Low")), row.names = c(NA, -41L), class = c("tbl_df", 
"tbl", "data.frame"))
gif <- df %>% 
  mutate(group=factor(group), category=factor(category)) %>%
  ggplot() +
  geom_rect(aes(xmin=37.00, xmax=37.21, ymin=-Inf, ymax=Inf, fill=category), alpha=0.5, show.legend=T) + 
  geom_jitter(aes(x=lon, y=lat), size=5, width=0.02, height=0.02) +
  labs(x="Longitude", y="Latitude") +
  scale_x_continuous(limits=c(37.00, 37.21), expand=c(0,0)) +
  theme(panel.background=element_blank(), 
        axis.ticks.length=unit(0.25,"cm"),
        axis.ticks.x=element_line(size=0.2),
        axis.ticks.y=element_line(size=0.2),
        axis.line.x=element_line(size=0.2),
        axis.line.y=element_line(size=0.2),
        plot.margin=margin(t=5.5, r=5.5, b=5.5, l=5.5)) +
  transition_time(time) +
  view_follow(fixed_x=T, fixed_y=T) +
  labs(title = '{frame_time}') +
  ease_aes('linear')

animate(gif, height=500, width=500, duration=70, renderer=gifski_renderer()) # use duration to slow it down
anim_save("df.gif")

df

标签: ranimationggplot2gganimate

解决方案


推荐阅读