Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stop ggrepel labels moving between gganimate frames in R/ggplot2?

I would like to add labels to the end of lines in ggplot, avoid them overlapping, and avoid them moving around during animation.

So far I can put the labels in the right place and hold them static using geom_text, but the labels overlap, or I can prevent them overlapping using geom_text_repel but the labels do not appear where I want them to and then dance about once the plot is animated (this latter version is in the code below).

I thought a solution might involve effectively creating a static layer in ggplot (p1 below) then adding an animated layer (p2 below), but it seems not.

How do I hold some elements of a plot constant (i.e. static) in an animated ggplot? (In this case, the labels at the end of lines.)

Additionally, with geom_text the labels appear as I want them - at the end of each line, outside of the plot - but with geom_text_repel, the labels all move inside the plotting area. Why is this?

Here is some example data:

library(dplyr)
library(ggplot2)
library(gganimate)
library(ggrepel)

set.seed(99)

# data
static_data <- data.frame(
  hline_label = c("fixed_label_1", "fixed_label_2", "fixed_label_3", "fixed_label_4", 
                  "fixed_label_5", "fixed_label_6", "fixed_label_7", "fixed_label_8", 
                  "fixed_label_9", "fixed_label_10"), 
  fixed_score = c(2.63, 2.45, 2.13, 2.29, 2.26, 2.34, 2.34, 2.11, 2.26, 2.37))

animated_data <- data.frame(condition = c("a", "b")) %>% 
  slice(rep(1:n(), each = 10)) %>% 
  group_by(condition) %>% 
  mutate(time_point = row_number()) %>% 
  ungroup() %>% 
  mutate(score = runif(20, 2, 3))

and this is the code I am using for my animated plot:

# colours for use in plot
condition_colours <- c("red", "blue")

# plot static background layer 
p1 <- ggplot(static_data, aes(x = time_point)) +
  scale_x_continuous(breaks = seq(0, 10, by = 2), expand = c(0, 0)) + 
  scale_y_continuous(breaks = seq(2, 3, by = 0.10), limits = c(2, 3), expand = c(0, 0)) + 
  # add horizontal line to show existing scores
  geom_hline(aes(yintercept = fixed_score), alpha = 0.75) + 
  # add fixed labels to the end of lines (off plot)
  geom_text_repel(aes(x = 11, y = fixed_score, label = hline_label), 
                  hjust = 0, size = 4, direction = "y", box.padding = 1.0) +
  coord_cartesian(clip = 'off') +
  guides(col = F) +
  labs(title = "[Title Here]", x = "Time", y = "Mean score") + 
  theme_minimal() + 
  theme(panel.grid.minor = element_blank(),
        plot.margin = margin(5.5, 120, 5.5, 5.5))

# animated layer
p2 <- p1 + 
  geom_point(data = animated_data, 
             aes(x = time_point, y = score, colour = condition, group = condition)) +
  geom_line(data = animated_data, 
            aes(x = time_point, y = score, colour = condition, group = condition), 
            show.legend = FALSE) +
  scale_color_manual(values = condition_colours) + 
  geom_segment(data = animated_data, 
               aes(xend = time_point, yend = score, y = score, colour = condition),
               linetype = 2) + 
  geom_text(data = animated_data, 
            aes(x = max(time_point) + 1, y = score, label = condition, colour = condition), 
            hjust = 0, size = 4) + 
  transition_reveal(time_point) +
  ease_aes('linear') 

# render animation 
animate(p2, nframes = 50, end_pause = 5, height = 1000, width = 1250, res = 120)

like image 340
monkeytennis Avatar asked Apr 17 '19 08:04

monkeytennis


Video Answer


1 Answers

Suggestions for consideration:

  1. The specific repelling direction / amount / etc. in geom_text_repel is determined by a random seed. You can set seed to a constant value in order to get the same repelled positions in each frame of animation.

  2. I don't think it's possible for repelled text to go beyond the plot area, even if you turn off clipping & specify some repel range outside plot limits. The whole point of that package is to keep text labels away from one another while remaining within the plot area. However, you can extend the plot area & use geom_segment instead of geom_hline to plot the horizontal lines, such that these lines stop before they reach the repelled text labels.

  3. Since there are more geom layers using animated_data as their data source, it would be cleaner to put animated_data & associated common aesthetic mappings in the top level ggplot() call, rather than static_data.

Here's a possible implementation. Explanation in annotations:

p3 <- ggplot(animated_data,
       aes(x = time_point, y = score, colour = condition, group = condition)) +

  # static layers (assuming 11 is the desired ending point)
  geom_segment(data = static_data,
               aes(x = 0, xend = 11, y = fixed_score, yend = fixed_score), 
               inherit.aes = FALSE, colour = "grey25") +
  geom_text_repel(data = static_data,
                  aes(x = 11, y = fixed_score, label = hline_label), 
                  hjust = 0, size = 4, direction = "y", box.padding = 1.0, inherit.aes = FALSE, 
                  seed = 123,           # set a constant random seed
                  xlim = c(11, NA)) +   # specify repel range to be from 11 onwards

  # animated layers (only specify additional aesthetic mappings not mentioned above)
  geom_point() +
  geom_line() +
  geom_segment(aes(xend = time_point, yend = score), linetype = 2) +
  geom_text(aes(x = max(time_point) + 1, label = condition),
            hjust = 0, size = 4) +

  # static aesthetic settings (limits / expand arguments are specified in coordinates
  # rather than scales, margin is no longer specified in theme since it's no longer
  # necessary)
  scale_x_continuous(breaks = seq(0, 10, by = 2)) +
  scale_y_continuous(breaks = seq(2, 3, by = 0.10)) + 
  scale_color_manual(values = condition_colours)  +
  coord_cartesian(xlim = c(0, 13), ylim = c(2, 3), expand = FALSE) +
  guides(col = F) +
  labs(title = "[Title Here]", x = "Time", y = "Mean score") + 
  theme_minimal() + 
  theme(panel.grid.minor = element_blank())  + 

  # animation settings (unchanged)
  transition_reveal(time_point) +
  ease_aes('linear') 

animate(p3, nframes = 50, end_pause = 5, height = 1000, width = 1250, res = 120)

result

like image 186
Z.Lin Avatar answered Oct 19 '22 17:10

Z.Lin