我刚刚开始使用很棒的gganimate程序包,然后设法创建了一个pretty cool animated gif。但是,我希望图像在单击时从一种状态移动到另一种状态,而不是自动地。有没有办法使用gganimate软件包或另一个软件包来做到这一点?
谢谢,
莎拉
我在评论方面声誉欠佳,因此直接回答,信誉欠佳,否则我会要求提供数据和代码,尽管如此,我仍会根据对链接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"
)
您可以从easing
中尝试更多的plotly/animation_attributes.js选项。这将直接创建一个交互式图:另一种选择是使用gganimate
中的相同动画,方法是首先使用以下命令将由它生成的每个帧保存在文件系统中:(因为我不知道您是如何创建动画的,所以我将创建一个基本的动画)]
p <- gg + transition_states(Timepoint, transition_length = 100, state_length = 5)
animate(p, renderer = file_renderer(prefix = "gganim_plot", overwrite = TRUE))
然后可以将其输入到saveHTML
R程序包的animation
命令中。