Animation of abline

I want to make an abline into animation.
line could be anything, like:
"
ggplot() + geom_abline(slope=1, intercept=10) + scale_x_continuous(limit=c(-10,10)) + scale_y_continuous(limit=c(-10,10))
"
How can I make it into animation, so that it starts from the left bottom, and extends to right top?
plotly could be acceptable, though i still haven't figure out how to do it with plotly.
Thanks.

I don't know of way to make geom_abline show only part of a line, but you could accomplish this with geom_segment and the gganimate package.

library(tidyverse); library(gganimate)


line_coords <- tribble(
  ~time,   ~x,  ~y, ~xend, ~yend,
      0,  -10, -10,   -10,   -10,
      1,  -10, -10,    10,    10
  )


a <- ggplot(line_coords) +
  geom_segment(aes(x = x, y = y, xend = xend, yend = yend)) +
  scale_x_continuous(limits = c(-10,10)) + 
  scale_y_continuous(limits = c(-10,10)) +
  transition_time(time) +
  ease_aes("cubic-in-out")

animate(a, nframes = 60, fps = 20)
3 Likes