【问题标题】:R ggplot2 & gganimate: animation changes at endR ggplot2 & gganimate:动画最后的变化
【发布时间】:2019-03-06 14:34:02
【问题描述】:

我正在尝试使用 gganimate 库创建这个随时间变化的动画,从“基线”时间点到“后期”时间点。然而由于某种原因,图像在第 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))

【问题讨论】:

    标签: r ggplot2 gganimate


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2018-03-30
      • 1970-01-01
      • 2021-02-22
      • 2020-11-03
      • 1970-01-01
      • 2021-02-23
      • 1970-01-01
      • 2020-07-23
      • 2019-09-07
      相关资源
      最近更新 更多