【问题标题】:Moving image on click in R with gganimate使用 gganimate 在 R 中单击时移动图像
【发布时间】:2020-06-02 10:10:40
【问题描述】:

我刚开始使用很棒的gganimate 包,并设法创建了一个pretty cool animated gif。 但是,我希望图像在单击时从一种状态移动到另一种状态,而不是自动移动。有没有办法用 gganimate 包或其他包做到这一点?

谢谢,

莎拉

【问题讨论】:

    标签: r click gganimate


    【解决方案1】:

    我在评论方面的声誉不足 2,因此直接回答,否则会要求提供数据和代码,但我会尝试根据链接 gif 的理解来回答。

    由于 gganimate 的主要目的是创建动画,这些动画只是从 ggplot 输出中拼接的帧,所以据我所知,没有直接具有点击功能的选项。

    这需要一个具有交互式图形功能的包,因此plotly 是最佳选择。不想贬低gganimate(我自己就是它的忠实粉丝)。

    library(plotly)
    library(ggplot2)
    
    df <- data.frame(Dim1 = runif(100,-200,200), Dim2 = rpois(100,5), 
                     Timepoint = rep(c("PRE","4WP"),50), pop = runif(100,1,10))
    
    gg <- ggplot(df, aes(Dim1, Dim2)) +
      geom_point(aes(size = pop, frame = Timepoint))
    
    ggplotly(gg) %>% 
      animation_opts(
        frame = 1000,
        transition = 1000,
        easing = "linear",
        redraw = TRUE,
        mode = "immediate"
      )
    

    您可以从plotly/animation_attributes.js 尝试更多easing 选项。 这将直接创建一个交互式绘图:
    另一种选择是使用来自gganimate 的相同动画,方法是首先将其生成的每一帧保存在文件系统中:(因为我不知道你是如何创建动画的,所以我将创建一个基本动画)

    p <- gg + transition_states(Timepoint, transition_length = 100, state_length = 5)
    animate(p, renderer = file_renderer(prefix = "gganim_plot", overwrite = TRUE))
    

    然后可以将其输入到animation R 包的saveHTML 命令中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-25
      • 2021-02-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-15
      相关资源
      最近更新 更多