You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

R语言ggplot2与gganimate:动画异常突变问题求助

Fixing Frame Glitches in gganimate Treemap Transitions

Hey there! Let's tackle those annoying frame glitches in your treemap animation. I dug into your code and data, and the issue boils down to how the treemap layout handles zero-value categories during transitions—even with fixed = T, sudden changes in element sizes (like zeros turning to non-zeros or vice versa) make the layout algorithm recalculate positions in a jumpy way, causing those abrupt shifts in frames 22-24 and 42-44.

Here's how to fix it:

Step 1: Clean Up Zero-Value Rows

Treemapify struggles with zero-area elements during tweening. Let's remove rows where Number == 0 from both datasets to eliminate sudden tile appearances/disappearances:

# Filter out rows with zero values
PID50baseline_clean <- PID50baseline[PID50baseline$Number != 0, ]
PID50late_clean <- PID50late[PID50late$Number != 0, ]

# Ensure consistent factor levels across datasets (critical for layout consistency)
common_clusters <- union(levels(PID50baseline_clean$Cluster.Name), levels(PID50late_clean$Cluster.Name))
PID50baseline_clean$Cluster.Name <- factor(PID50baseline_clean$Cluster.Name, levels = common_clusters)
PID50late_clean$Cluster.Name <- factor(PID50late_clean$Cluster.Name, levels = common_clusters)

Step 2: Adjust Tween Parameters for Smoother Transitions

Tweak the tweening settings to spread transitions over more frames and reduce abrupt jumps:

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")

# Create tweened data with cleaned datasets and adjusted timing
tweened <- tween_states(
  list(PID50baseline_clean, PID50late_clean, PID50baseline_clean),
  tweenlength = 12,  # Longer transition phase for gradual size changes
  statelength = 6,   # Shorter hold on static states to keep animation flowing
  ease = 'cubic-in-out',
  nframes = 60       # More frames to smooth out transitions
)

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

# Adjust animation speed for smoother playback
animation::ani.options(interval = 1/12)

# Render the fixed animation
gganimate(animated_plot, "animated_treemap_PID50_fixed.gif", title_frame = TRUE, ani.width = 200, ani.height = 200)

Why This Works

  • Removing zero-value rows eliminates sudden, tiny tile changes that throw off the treemap layout.
  • Consistent factor levels ensure the layout algorithm uses the same category order across all frames, preventing unexpected shifts.
  • Longer tween lengths and more frames spread size changes over more steps, making transitions gradual instead of abrupt.

Give this modified code a try—those glitchy frames should be gone, and your animation will transition smoothly between baseline and late states!

内容的提问来源于stack exchange,提问作者Radwa Sharaf

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 07:27:41