R 中的动画热图

Animated heatmap in R

我正在尝试使用 ggplot 和(可能)plot_ly 制作动画热图。到目前为止,我已经完成了两个部分目标:

1) 我可以使用 plot_ly:

制作动画散点图
dd <- data.frame(f = rep(1:5, c(rep(50, 5))), 
             x = round(runif(250, 10,100),0),
             y = round(runif(250, 10,100),0),
             id = rep(1:50,5)) 

p <- dd %>%
  plot_ly(
    x = ~x,
    y = ~y,
    frame = ~f,
    type = 'scatter',
    mode = 'markers',
    showlegend = F
  )
p

但是,对于 scatter,绘图不会传达有关帧内或帧间重复元素的信息。 (到目前为止,我无法让带有选项 heatmapplot_ly 工作。)

2) 我可以使用 ggplotstat_bin2d.

为每个步骤生成热图
dd.wide <- reshape(dd, direction="wide", timevar = "f")

for (i in seq(2,length(names(dd.wide)),2)){
  p <- ggplot(dd.wide, aes_(x=as.name(names(dd.wide)[i]),y=as.name(names(dd.wide)[i+1]))) +
    stat_bin2d() +
    xlim(10,100) +
    ylim(10,100) +
    guides(fill = guide_colorbar(barwidth = 1, barheight = 5)) +
    scale_fill_gradientn(colours=rev(heat.colors(10)))
  print(p)
}

第二种方法的机器人我没有找到将这些静态图变成漂亮动画的简单方法(我可以制作 snapshot-movie 但我希望图像之间的过渡更平滑。)

我也尝试过使用 ggplotly(),但无法正常工作。

如果有任何关于如何使热图像 plot_ly 中的散点图一样变化的建议,我将不胜感激(我可以使用不同的动画包)。

更新我无法在网站上使用动画散点图,所以我用该图的快照替换了动画。

您是否正在寻找这样的东西:

p <- ggplot(data= dd, aes(x = x, y = y)) +
  stat_bin2d(aes(frame = f)) +
  xlim(10,100) +
  ylim(10,100) +
  guides(fill = guide_colorbar(barwidth = 1, barheight = 5)) +
  scale_fill_gradientn(colours=rev(heat.colors(10)))
p <- ggplotly(p)