I have simple plot:
sample_data <-
data.frame(
x = 1:100
, y = 1:100
)
temp_plot <-
ggplot(sample_data
, aes(x = x
, y = y)) +
geom_line(
size = 3
, arrow = arrow()
, lineend = "round"
, linejoin = "round"
) +
theme_minimal()
that looks like this:
And I want to animate it with gganimate
like so:
temp_animate <-
temp_plot +
transition_reveal(x)
anim_save("temp_animate.gif"
, temp_animate
, "~/Downloads/"
, end_pause = 10)
However, when I do, the arrow is pointing the wrong direction right up until the very last frame (paused to make it clear that it is correct at that point).
I've tried playing with the values in arrow
(including various angles, including negative) but nothing that I do seems to correct the orientation of the arrow (which should point along the current vector in each frame).
How can I get the arrow to point in the correct direction throughout? (I am cross-posting this as an issue in the github directory).
Explanation
This phenomenon arises because
transition_reveal
tweens values to get the transition position (where the arrowhead is located) in each frame. Whenever the calculated transition position coincides with an actual point on the dataset, there would be two sets of coordinates for the same location. This results in the reversed arrow.(In your example, the arrow is reversed all the way because the default number of frames is the same as the number of rows in your data, so each calculated transition position is a duplicate of an existing data point. If the frame number is some other number, e.g. 137, the arrow would reverse in some frames & point straight in others.)
We can demonstrate this phenomenon with a smaller dataset:
Workaround
The key function here is
expand_data
from the ggproto objectTransitionReveal
. I wrote a modified version that adds a check for duplicated positions before returning the expanded dataset:We can define an alternate version of
transition_reveal
that uses the above instead:Demonstrate with the original data: