R ggplot2 & gganimate:最后动画变化

R ggplot2 & gganimate: animation changes at end

我正在尝试使用 gganimate 库创建这个随时间变化的动画,从 'baseline' 时间点到 'late' 时间点'。但是由于某种原因,图像在第 22-24 帧之间发生变化,并在第 42-44 帧之间再次发生变化。它抛弃了可视化。但我不确定如何修复它。非常感谢!

library(ggplot2)
library(tweenr)
library(gganimate)
library(treemapify)

set.seed(1)

colors <- c("turquoise", "gold", "yellowgreen", "dodgerblue", "firebrick", "orchid4",
            "grey74", "forestgreen", "deeppink2", "grey0", "slateblue", "sienna2", 
            "khaki2", "steelblue", "darksalmon", "darksalmon")

tweened <- tween_states(list(PID50baseline, PID50late, PID50baseline), 
                        tweenlength = 8, statelength = 8, 
                        ease = 'cubic-in-out', nframes = 50)

animated_plot <- ggplot(tweened, 
                        aes(area = Number, fill = Cluster.Name,
                            subgroup=Type, frame = .frame)) + 
  geom_treemap(fixed = T) + 
  geom_treemap_subgroup_border(fixed = T) + 
  geom_treemap_subgroup_text(place = "centre", grow = T, alpha = 0.5,
                             colour = "black", fontface = "italic", 
                             min.size = 0,fixed = T) + 
  scale_fill_manual(values = colors) +
  theme(legend.position = "bottom")

animation::ani.options(interval = 1/10)

gganimate(animated_plot, "animated_treemap_PID50.gif", title_frame = T,
          ani.width = 200, ani.height = 200)

我为此使用的数据:

dput(PID50baseline)
structure(list(Cluster.Name = structure(c(13L, 14L, 1L, 2L, 3L, 
4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 15L, 15L), .Label = c("Cluster 
13", "Cluster 14", "Cluster 17", "Cluster 18", "Cluster 19", "Cluster 20", 
"Cluster 27", "Cluster 35", "Cluster 36", "Cluster 40", "Cluster 41", 
"Cluster 42", "Cluster 5", "Cluster 6", "Non-clonal"), class = "factor"), 
Number = c(5L, 9L, 0L, 0L, 1L, 2L, 0L, 2L, 3L, 2L, 1L, 0L, 
0L, 0L, 1L, 28L), Type = structure(c(1L, 1L, 1L, 1L, 1L, 
1L, 1L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 1L), .Label = c("Defective", 
"Intact"), class = "factor")), .Names = c("Cluster.Name", 
"Number", "Type"), class = "data.frame", row.names = c(NA, -16L))

dput(PID50late)
structure(list(Cluster.Name = structure(c(13L, 14L, 1L, 2L, 3L, 
4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 15L, 15L), .Label = c("Cluster 13", 
"Cluster 14", "Cluster 17", "Cluster 18", "Cluster 19", "Cluster 20", 
"Cluster 27", "Cluster 35", "Cluster 36", "Cluster 40", "Cluster 41", 
"Cluster 42", "Cluster 5", "Cluster 6", "Non-clonal"), class = "factor"), 
Number = c(2L, 10L, 2L, 2L, 1L, 0L, 5L, 0L, 5L, 0L, 3L, 3L, 
2L, 2L, 18L, 59L), Type = structure(c(1L, 1L, 1L, 1L, 1L, 
1L, 1L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 1L), .Label = c("Defective", 
"Intact"), class = "factor")), .Names = c("Cluster.Name", 
"Number", "Type"), class = "data.frame", row.names = c(NA, -16L))

我相信treemapifyomits areas with a size of 0。这可能是您遇到问题的原因。换句话说,将 0 替换为大于 0 的小正值(并使用 16 种不同的颜色)会得到如下结果:

tweened$Number[tweened$Number==0] <- 1e-10
colors <- unname(randomcoloR::distinctColorPalette(nlevels(tweened$Cluster.Name)))