Search code examples
ranimationggplot2gganimate

ggplot2 and gganimate: How to make an animated line change colors depending on Y axis values


I want the animated line in my plot to change colors when it goes above 50%, to show the difference in outcomes.

My "data.thesis" frame looks like this:

image1

My hypothetical outcome plot is here. The code will animate the plot so that the black line moves to potential outcomes:

image2

library(ggplot2)
library(gganimate)

z <- qnorm(0.33)  
sd <- (50 - 55)/z
moe <- 1.96*sd

data.thesis <- data.frame(name = "Candidate", percent.vote = 55)
data.thesis <- data.thesis %>% 
  mutate(sd = sd, 
         ymin = percent.vote - 1.96*sd, 
         ymax = percent.vote + 1.96*sd)


set.seed(2016)
n.outcomes <- 50

df <- data.frame(simulation = 1:n.outcomes, 
                 team = "Yes", 
                 vote.share = round(rnorm(50, mean = 55, sd = sd), 3))

p <- df %>%
  ggplot(aes(x = team, y = vote.share)) +
  geom_errorbar(aes(
    ymin = vote.share, ymax = vote.share, 
    frame = simulation, cumulative = TRUE), 
    color = "grey80", alpha = 1/8) +
  geom_errorbar(aes(
    ymin = vote.share, ymax = vote.share, frame = simulation), 
    color = "black") +
  theme(panel.background = element_rect(fill = "#FFFFFF"),
        text=element_text(size=15, vjust=1, family="Verdana"),
        axis.title.x=element_blank(),
        axis.text.x=element_blank(),
        axis.ticks.x=element_blank()) +
  labs(title = "", y = "Vote share", x = "") +
  scale_y_continuous(limits=c(0, 100), 
                     breaks=c(0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100)) +
  xlab("") +
  ylab("Percent") +
  ggtitle("Treatment 2: Hypothetical Outcome Plot") +
  annotate("segment", x=1.6, xend=1.6, y=51, yend=70, colour="black", size=1,  arrow=arrow()) +
  annotate(geom="text", x=1.6, y=74, label="A Wins", color="black", size=5) + 
  annotate("segment", x=1.6, xend=1.6, y=49, yend=30, colour="black", size=1,  arrow=arrow()) +
  annotate(geom="text", x=1.6, y=26, label="A Loses", color="black", size=5) + 
  annotate("text", x = 1.8, y = 75, label = "")  +
  annotate("segment", x=.55, xend=1.45, y=55, yend=55, colour="red")

gganimate(p, title_frame = FALSE)

Solution

  • Instead of hard-coding the line color, use a colour aesthetic in the second call to geom_errorbar. Specifically, change the second geom_errorbar statement from this:

      geom_errorbar(aes(ymin = vote.share, ymax = vote.share, frame = simulation), 
        color = "black") +
    

    to this:

      geom_errorbar(aes(ymin = vote.share, ymax = vote.share, frame = simulation, 
        colour=ifelse(vote.share > 50, "Win","Lose")), show.legend=FALSE) +
    

    This isn't specific to gganimate, as this is exactly what you would do for any ggplot to make the line color dependent on exceeding some y-axis cut point.

    In the plot below, I've added + scale_colour_manual(values=c(Win="blue", Lose="red")), but you can set the colors to whatever values you wish. Also, I've left the red line annotation at y=55. I'm not sure if you wanted that in the final plot, but it can of course easily be removed.

    enter image description here