R 使用gganimate指向错误方向的箭头

R 使用gganimate指向错误方向的箭头,r,gganimate,R,Gganimate,我有一个简单的情节: sample_data <- data.frame( x = 1:100 , y = 1:100 ) temp_plot <- ggplot(sample_data , aes(x = x , y = y)) + geom_line( size = 3 , arrow = arrow() , lineend = "round" ,

我有一个简单的情节:

sample_data <-
  data.frame(
    x = 1:100
    , y = 1:100
  )

temp_plot <-
  ggplot(sample_data
         , aes(x = x
               , y = y)) +
  geom_line(
    size = 3
    , arrow = arrow()
    , lineend = "round"
    , linejoin = "round"
  ) +
  theme_minimal()
但是,当我这样做时,箭头指向错误的方向,直到最后一帧为止(暂停以明确该点是正确的)

我尝试过使用
箭头中的值(包括各种角度,包括负数),但我所做的任何操作似乎都无法纠正箭头的方向(箭头应指向每个帧中的当前向量)

如何使箭头始终指向正确的方向?(我在github目录中交叉发布此信息)。

说明 出现这种现象的原因是
transition\u显示两个值之间的值,以获得每个帧中的转换位置(箭头所在的位置)。每当计算的过渡位置与数据集上的实际点重合时,同一位置将有两组坐标。这将导致反向箭头

(在您的示例中,箭头一直反向,因为默认帧数与数据中的行数相同,因此每个计算的转换位置都是现有数据点的副本。如果帧数是其他数字,例如137,则箭头将在某些帧中反向并在其他帧中直接指向。)

我们可以用较小的数据集演示这种现象:

p <- ggplot(data.frame(x = 1:4, y = 1:4),
            aes(x, y)) +
  geom_line(size = 3, arrow = arrow(), lineend = "round", linejoin = "round") +
  theme_minimal() +
  transition_reveal(x)

animate(p + ggtitle("4 frames"), nframes = 4, fps = 1) # arrow remains reversed till the end
animate(p + ggtitle("10 frames"), nframes = 10, fps = 1) # arrow flips back & forth throughout
我们可以定义一个替代版本的
transition\u reveal
,它使用上述内容:

transition_reveal2 <- function (along, range = NULL, keep_last = TRUE) {
  along_quo <- enquo(along)
  gganimate:::require_quo(along_quo, "along")
  ggproto(NULL, TransitionReveal2, # instead of TransitionReveal
          params = list(along_quo = along_quo, range = range, keep_last = keep_last))
}

我认为您的示例可能缺少一两行代码,因为我不知道
temp\u plot
如何变成
temp\u animate
任何地方…谢谢,@Z.Lin。我在复制粘贴的时候错过了。它只是添加了
transition\u reveal(x)
,现在是一个问题。我仍然不清楚,当存在重复坐标时,为什么箭头会反转,但知道它是这样就足够了。我将链接到您对我打开的Github问题的回答。也许他们可以在下一次更新中加入您的
transition\u reveal
。谢谢箭头反转的原因我也不清楚,但在这一点上,这是一个
grid
问题,而不是
gganimate
问题,我真的没有做足够的工作来理解前者=P
TransitionReveal2 <- ggproto(
  "TransitionReveal2", TransitionReveal,
  expand_panel = function (self, data, type, id, match, ease, enter, exit, params, 
                           layer_index) {    
    row_vars <- self$get_row_vars(data)
    if (is.null(row_vars)) 
      return(data)
    data$group <- paste0(row_vars$before, row_vars$after)
    time <- as.numeric(row_vars$along)
    all_frames <- switch(type,
                         point = tweenr:::tween_along(data, ease, params$nframes, 
                                                      !!time, group, c(1, params$nframes),
                                                      FALSE, params$keep_last),
                         path = tweenr:::tween_along(data, ease, params$nframes, 
                                                     !!time, group, c(1, params$nframes),
                                                     TRUE, params$keep_last),
                         polygon = tweenr:::tween_along(data, ease, params$nframes, 
                                                        !!time, group, c(1, params$nframes),
                                                        TRUE, params$keep_last),
                         stop(type, " layers not currently supported by transition_reveal", 
                              call. = FALSE))
    all_frames$group <- paste0(all_frames$group, "<", all_frames$.frame, ">")
    all_frames$.frame <- NULL
    
    # added step to filter out transition rows with duplicated positions
    all_frames <- all_frames %>%
      filter(!(.phase == "transition" &
                 abs(x - lag(x)) <= sqrt(.Machine$double.eps) &
                 abs(y - lag(y)) <= sqrt(.Machine$double.eps)))
    
    all_frames
  }
)
transition_reveal2 <- function (along, range = NULL, keep_last = TRUE) {
  along_quo <- enquo(along)
  gganimate:::require_quo(along_quo, "along")
  ggproto(NULL, TransitionReveal2, # instead of TransitionReveal
          params = list(along_quo = along_quo, range = range, keep_last = keep_last))
}
temp_plot + transition_reveal2(x)