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

问题描述

我有一个简单的情节:

gganimate

看起来像这样:

enter image description here

我想用temp_animate <- temp_plot + transition_reveal(x) anim_save("temp_animate.gif",temp_animate,"~/Downloads/",end_pause = 10) 对其进行动画处理,如下所示:

arrow

但是,当我这样做时,箭头一直指向错误的方向,直到最后一帧为止(已暂停以清楚地表明该点正确)。

enter image description here

我尝试使用 @Mapping(target = "employee.mainAddress.address",source = "employee.registeredAddresses[0].privateAddresses[0].address") abstract EmployeeDto map(Employee employee); 中的值(包括各种角度,包括负值),但是我似乎没有做任何事情来校正箭头的方向(箭头应指向每帧中的当前矢量)

如何始终使箭头指向正确的方向? (我将其交叉发布为github目录中的an issue)。

解决方法

说明

之所以出现此现象,是因为transition_reveal补间值以获取每一帧中的过渡位置(箭头所在的位置)。每当计算的过渡位置与数据集中的实际点重合时,同一位置就会有两组坐标。这将导致箭头反转。

(在您的示例中,箭头一直反向,因为默认的帧数与数据中的行数相同,因此,每个计算的过渡位置都是现有数据点的副本。数字是其他一些数字,例如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

Series.view

4 frames

解决方法

这里的关键功能是来自ggproto对象expand_data的{​​{1}}。我写了一个修改后的版本,在返回扩展的数据集之前添加了对重复位置的检查:

TransitionReveal

我们可以定义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,params$nframes,!!time,group,c(1,params$nframes),FALSE,params$keep_last),path = tweenr:::tween_along(data,TRUE,polygon = tweenr:::tween_along(data,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_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))
}

10 frames

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...