【问题标题】:Equivalent of gganimate::transition_events on plotly等效于情节上的 gganimate::transition_events
【发布时间】:2020-09-02 18:35:22
【问题描述】:

在 R 中,使用 gganimate,可以制作一个动画情节,其中事件随时间出现和消失。例如:

library(lubridate)
library(gganimate)

df=data.frame(
x=c(1,2,3,4),
y=c(1,2,3,4),
start=c(1,2,3,4),
end=c(5,6,7,8),
en=as_date(1),
ex=as_date(1))

ggplot(data=df, aes(x=x,y=y))+
  geom_point()+
  gganimate::transition_events(
     start=start, 
     end=end, 
     enter_length = as.numeric(en), 
     exit_length = as.numeric(ex))

这会生成一个图,其中点根据“开始”列出现,根据“结束”列消失。

我想知道是否有一种简单的方法可以在 plotly 中实现同样的效果(最好使用ggplotly()),让滑块随着时间移动。

【问题讨论】:

  • 你的例子对我不起作用——你可能想看看this
  • 谢谢@ismirsehregal,我错过了一对线。我还添加了对库的调用。查看了您的链接,但我找不到让点出现在“开始”并在“结束”消失的方法
  • 在删除 data.frame 定义后的多余括号后,这给了我:Error: enter_length data must be numeric。你不会在情节中找到startend 的相同方法。当相应动画帧中没有可用数据(或值为NA)时,点将消失。
  • 再次抱歉。我仔细检查了它现在必须工作。无论如何,谢谢你提供的情节信息。我想我应该转换 data.frame 以获得每个日期和点 1 行。
  • 我只是试图为您创建一个示例并回忆起几个月前我filed an issue关于这个话题。请支持它(竖起大拇指)以引起关注。

标签: r ggplot2 plotly r-plotly


【解决方案1】:

这是一个使用ggplotly 的示例。然而结果并不完全相同:

library(plotly)
library(lubridate)

df = data.frame(
  x = c(1, 2, 3, 4),
  y = c(1, 2, 3, 4),
  start = c(1, 2, 3, 4),
  end = c(5, 6, 7, 8),
  en = as_date(1),
  ex = as_date(1)
)

frame_list <- Map(seq, from = df$start, to = df$end)
DF <- data.frame(x = rep(df$x, times = lengths(frame_list)),
                 y = rep(df$y, times = lengths(frame_list)),
                 frame = unlist(frame_list))

p <- ggplot(DF, aes(x, y)) +
  geom_point(aes(size = y, frame = frame))

fig <- ggplotly(p)

fig %>%
  animation_opts(
    frame = 0,
    easing = "linear",
    redraw = FALSE,
    mode = "immediate"
  )
fig

【讨论】:

  • 感谢您的回答,但我仍然必须将我的 df 转换为您的 DF,所以我不会将其标记为答案(输入与问题不同)
  • 是的,这就是我想要展示的。 Plotly 需要不同的输入格式。
  • 是的,所以部分解决方案是将 df 转换为 DF。您为我指明了正确的方向,我很感激谢谢!
  • 将您的df 转换为所需的格式并不难。请检查我的更新。
  • 谢谢,太好了
猜你喜欢
  • 2020-02-04
  • 1970-01-01
  • 2020-01-17
  • 2011-04-30
  • 1970-01-01
  • 2011-02-26
  • 2022-01-22
  • 2019-03-22
相关资源
最近更新 更多