# R: 花火を描く！, gganimate

gganimate has transitioned to a state of release

Rのライブラリgganimateで作成された花火のgif動画です。

gif動画を見れない方のために静止画像を貼っておきます。

スクリプト

```# the fantastic sample on https://www.data-imaginist.com/2019/gganimate-has-transitioned-to-a-state-of-release/
library(ggplot2)
library(gganimate)
# Firework colours
colours <- c(
'lawngreen',
'gold',
'white',
'orchid',
'royalblue',
'yellow',
'orange'
)
# Produce data for a single blast
blast <- function(n, radius, x0, y0, time) {
u <- runif(n, -1, 1)
rho <- runif(n, 0, 2*pi)
x <- radius * sqrt(1 - u^2) * cos(rho) + x0
y <- radius * sqrt(1 - u^2) * sin(rho) + y0
id <- sample(.Machine\$integer.max, n + 1)
data.frame(
x = c(x0, rep(x0, n), x0, x),
y = c(0, rep(y0, n), y0, y),
id = rep(id, 2),
time = c((time - y0) * runif(1), rep(time, n), time, time + radius + rnorm(n)),
colour = c('white', rep(sample(colours, 1), n), 'white', rep(sample(colours, 1), n)),
stringsAsFactors = FALSE
)
}
# Make 20 blasts
n <- round(rnorm(20, 30, 4))
x0 <- runif(20, -30, 30)
y0 <- runif(20, 40, 80)
time <- runif(20, max = 100)
fireworks <- Map(blast, n = n, radius = radius, x0 = x0, y0 = y0, time = time)
fireworks <- dplyr::bind_rows(fireworks)

ggplot(fireworks) +
geom_path(aes(x = x, y = y, group = id, colour = colour)) +
scale_colour_identity()

ggplot(fireworks) +
geom_point(aes(x, y, colour = colour, group = id), size = 0.5, shape = 20) +
scale_colour_identity() +
coord_fixed(xlim = c(-65, 65), expand = FALSE, clip = 'off') +
theme_void() +
theme(plot.background = element_rect(fill = 'black', colour = NA),
panel.border = element_blank()) +
# Here comes the gganimate code
transition_components(time, exit_length = 20) +
ease_aes(x = 'sine-out', y = 'sine-out') +
shadow_wake(0.05, size = 3, alpha = TRUE, wrap = FALSE,
falloff = 'sine-in', exclude_phase = 'enter') +
exit_recolour(colour = 'black')
# save the animation
anim_save("firework.gif")```