Last active
February 10, 2022 14:40
-
-
Save AdamSpannbauer/712392e71c5c7ec209a50b24701593c4 to your computer and use it in GitHub Desktop.
animated heart using the R package gganimate
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
<3