Skip to content

Instantly share code, notes, and snippets.

@AdamSpannbauer
Last active February 10, 2022 14:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AdamSpannbauer/712392e71c5c7ec209a50b24701593c4 to your computer and use it in GitHub Desktop.
Save AdamSpannbauer/712392e71c5c7ec209a50b24701593c4 to your computer and use it in GitHub Desktop.
animated heart using the R package gganimate
library(ggplot2)
library(gganimate)
library(data.table)
gen_heart_y = function(x, a) {
# source: https://i.imgur.com/avE8cxJ.gifv
(x^2)^(1 / 3) + 0.9 * (3.3 - x^2)^(1 / 2) * sin(a * pi * x)
}
heart_dt_list = lapply(seq(1, 15, by = 0.1), function(a) {
heart_dt = data.table(x = seq(-1.8, 1.8, length.out = 500), a = a)
heart_dt[, y := gen_heart_y(x, a)]
return(heart_dt)
})
full_heart_dt = rbindlist(heart_dt_list)
animated_heart = ggplot(full_heart_dt, aes(x, y)) +
geom_line(color='firebrick3') +
theme_void() +
transition_manual(a)
animation = animate(animated_heart, width = 400, height = 400)
gganimate::save_animation(animation, '~/Desktop/heart_gganimate.gif')
@seymakalay
Copy link

<3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment