R Language : Mixing gganimate and ggrepel

152 views Asked by At

I've been using both packages (gganimate and ggrepel) for a few days and I must say they are really helpful when it comes down to visualization. Though, I was wondering if there was a way of combining them.

Lastly, I've tried to plot, for a number of communities (in France), the amount of expenses in staff versus the amount of expenses in equipment for each community (18 of them) and the evolution of this arbitrage between 2011 and 2015.

So, an interesting thing to do would be to use ggrepel in order to relate a point to the right community as well as gganimate to see how these points evolve throughout time.

Here's a part of my code:

p <- ggplot(df) + 
          geom_point(aes(Equip, Staff, size=population, color=factor(Type), frame=Year)) + 
          scale_size(range = c(2,10)) +
          geom_label_repel(aes(Equip, Staff, fill = factor(Type),
                           label = Name),
                           size=2.5,
                           fontface = 'bold',
                           color = 'white',
                           box.padding = unit(2.5, "lines"),
                          point.padding = unit(0.1, "lines"),
                          segment.color = 'grey50',
                          segment.size = 1,
                          max.iter = 20000, alpha = .7) +
          facet_wrap(~other_variable, scales = "free")
gganimate(p,interval = 3,
          ani.width = 1064,
          ani.heigth = 768)

My problem is that gganimate updates only the points for each frame (i.e. year) and print all labels discarding which frame is actually being printed.

For instance, if in 2011 city A has coordinates: (100, 200) and in 2012 (150, 200), it will print the labels at each coordinate for both years instead of printing A at the first coordinates for 2011 and A at the second coordinates for 2012.

Does anyone have a solution to this issue?

Thanks a lot.

0

There are 0 answers