SimonDude
SimonDude

Reputation: 17

Fill transition in gganimate

I want to animate a fill value transition between states. I have a 100x100 grid of geom_points that take values between 0 and 1. Essentially, the points stay in the same position at every point in time, but their fill value changes, which I want to animate. Here an excerpt on the data

head(strg)

        Var1      Var2 const4       pred id
   1 -1.0000000   -1      1 3.337224e-37  1
   2 -0.9292929   -1      1 1.922538e-34  2
   3 -0.8585859   -1      1 2.561529e-32  3
   4 -0.7878788   -1      1 2.522569e-30  4
   5 -0.7171717   -1      1 1.433660e-28  5
   6 -0.6464646   -1      1 1.795601e-27  6

where const4 takes on the different states. And here the initial plot

ggplot(strg, aes(x = strg[,1], y = strg[,2])) + geom_point(aes(color = strg[,"pred"])) + scale_color_gradient("Predictions", low = "blue", high = "orange")

My questions are now

  1. How do I have to structure my data frame upon which the ggplot is build? I thought about having it repeat for all the states in const4.
  2. What function to use in gganimate to visualize the tranisition?

Upvotes: 0

Views: 305

Answers (1)

SimonDude
SimonDude

Reputation: 17

I figured it out by myself after some help from Possible to animate polygon fill using gganimate in R?.

strg <- strg[with(strg,order(Var1, Var2)),]
strg$id <- rep(1:length(steps), times = num.interpolating.points^2)
strg$id <- as.factor(as.character(strg$id))
p <- ggplot(strg, aes(x = Var1, y = Var2)) + geom_point(data =  strg, aes(color = pred)) + scale_color_gradient("Predictions", low = "blue", high = "orange") + theme(panel.border = element_blank(), panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.background = element_blank()) + coord_cartesian(xlim = c(-0.75,6), ylim = c(-0.8,6))


p <- p + transition_manual(frames = id) 
p
anim_save("Transition1.mp4")

,which resulted in the desired output. The variable id is used to determine the point in time for each of the 10,000 points.

Upvotes: 0

Related Questions