r - R ggplot2 & gganimate:动画最后的变化
问题描述
我正在尝试使用 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))
解决方案
我相信treemapify
忽略大小为 0 的区域。这可能是您的问题的原因。换句话说,将 0 替换为大于 0 的小正值(并使用 16 种不同的颜色)会得到如下结果:
tweened$Number[tweened$Number==0] <- 1e-10
colors <- unname(randomcoloR::distinctColorPalette(nlevels(tweened$Cluster.Name)))
推荐阅读
- javascript - Javascript:正则表达式中的异常“如果前面没有”
- python - Pandas:快速随机负采样
- sql - SSAS 表格中的不同流程模式如何工作?
- python - 仅在 x 和 y 之间的列表中打印数字
- c# - 多上下文 - 使用身份框架上下文和实体框架 DbContext 的正确方法
- java - JDK11/JavaFX:如何在没有构建/依赖管理的情况下制作一个胖 jar?
- javascript - 表格中的响应式 c3 图表
- asp.net-core - 如何更改 Razor 组件中 HTML 文档头部的标题和其他 html 元素?
- javascript - 如何将变异变量传递给渲染函数?
- tfs - Azure Devops/TFS 版本“支持文件”(Powershell、配置等)的适当位置是什么?