PENDAHULUAN

Tujuan

Sebagai penyemangat dalam belajar R, kadang perlu membuat sesuatu yang keren dengan cara copy paste dari hasil script orang lain, kemudian kita modifikasi agar nantinya dapat memahami arti dari script-script yang di buat dan akhirnya kita dapat menyusun sendiri sekumpulan script yang kita perlukan untuk menyelesaikan pekerjaan kita.

Animasi kembang api dapat dipelajari dari website ini `

# Import Library
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))
radius <- round(n + sqrt(n))
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()

gfire <- 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(wake_length = 0.1, size = 3, alpha = TRUE, wrap = FALSE, 
              falloff = 'sine-in', exclude_phase = 'enter') + 
  exit_recolour(colour = 'black') +
  labs(title='BAHASA PEMROGRAMAN R MEMANG KEREN', subtitle='Bahasa R itu Mudah', caption = 'Ref.: https://www.data-imaginist.com') +
  theme(plot.title=element_text(color='yellow', hjust=0.5, size=10, face = 'bold'),
        plot.subtitle=element_text(color='white', hjust=0.5, size=9),
        plot.caption = element_text(color = "white", face = "italic", size = 6, hjust = 1))
gfire
library(gganimate)
anim_save('fireworks_bahasa_R.gif',
          plot=gfire)