Radwa Sharaf
Radwa Sharaf

Reputation: 179

R ggplot2 & gganimate: animation changes at end

I am trying to create this figure that animates over time using the gganimate library, going from the 'baseline' timepoint to the 'late' timepoint'. However for some reason, the image changes between frames 22-24 and again between 42-44. It throws off the visualization. But I am not sure how to fix it. Many thanks!

enter image description here

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)

The data I used for this:

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

Upvotes: 2

Views: 432

Answers (1)

lukeA
lukeA

Reputation: 54247

I believe treemapify omits areas with a size of 0. This could be the reason for your problem. In other words, replacing 0 with a small positive value greater than 0 (and using 16 distinct colors) gives you something like this:

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

enter image description here

Upvotes: 2

Related Questions