Skip to content

Instantly share code, notes, and snippets.

@ikashnitsky
Last active December 12, 2019 20:07
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 ikashnitsky/64ce22c6de6fdd5e1aac87fafe073453 to your computer and use it in GitHub Desktop.
Save ikashnitsky/64ce22c6de6fdd5e1aac87fafe073453 to your computer and use it in GitHub Desktop.
Generate snowfall -- every snowflake scaled to a country population size ^1/3
#===============================================================================
# 2019-12-12 -- twitter
# Generate snowfall -- every snowflake scaled to a country population size ^1/3
# Ilya Kashnitsky, ilya.kashnitsky@gmail.com
#===============================================================================
library(tidyverse)
library(magrittr)
library(gganimate)
# helper function to ensure the snowflakes don't disappear ----------------
# function for one snowflake
loop_single <- function(value){
if(value %>% is_less_than(0)){value <- 1}
if(value %>% is_greater_than(1)){value <- 0}
return(value)
}
# vectorize
loop_multiple <- function(values){
values %>% map_dbl(loop_single)
}
# create a larger dataframe with frames for snowflake movement ------------
# function to create smooth frames for snowflake falling
fun_framing <- function(df, n_frames = 100, wind = 0){
# frames vector
frames <- seq(1, n_frames)
# empty list
out <- list()
# place the init df in the first element
out[[1]] <- df
# length of the df
len <- nrow(df)
for (i in seq_along(frames)) {
# calculate x shift, unique for each snowflake
x_shift <- runif(len, -.5, .5) %>% add(wind)
dfi <- out[[i]] %>%
mutate(
x = x %>%
add(x_shift %>% multiply_by(size/1e3)) %>%
loop_multiple(),
y = y %>%
subtract(size/1e3) %>%
loop_multiple()
)
out[[i+1]] <- dfi
}
# bind list
out <- out %>%
bind_rows(.id = "frame") %>%
mutate(frame = frame %>% as.numeric)
return(out)
}
# calculate dataset -------------------------------------------------------
# get countries' population size
library(wpp2019)
data(pop)
set.seed(911)
df <- pop %>%
filter(
country_code %>% paste %>% map_dbl(nchar) %>% equals(3),
country_code %>% paste %>% str_sub(1,1) %>% equals(9) %>% not
) %>%
transmute(id = country_code, name, pop = `2020`) %>%
# calculate size as scaled log(pop)
mutate(
size = pop %>% raise_to_power(1/3) %>% scales::rescale(to = c(1, 12))
) %>%
# add random starting coordinates
mutate(
x = runif(178),
y = runif(178),
angle = runif(178) %>% scales::rescale(to = c(0, 60))
)
# generate movement frames
set.seed(911)
# generate df for animation
df_anim <- df %>% fun_framing(n_frames = 3e2, wind = -1)
save(df_anim, file = "1912-animated-snow/df_anim.rda")
load("1912-animated-snow/df_anim.rda")
# plot/animate ------------------------------------------------------------
library(ggimage)
svg_path <- "https://gist.githubusercontent.com/ikashnitsky/64ce22c6de6fdd5e1aac87fafe073453/raw/46c9908277330228515b57b7dafd532298ac5e18/snow-white.svg"
p <- df_anim %>%
ggplot(aes(x, y, size = I(size / 150))) +
geom_image(image = svg_path)+
coord_cartesian(c(0, 1), c(0, 1), expand = FALSE)+
theme_void()+
theme(
panel.background = element_rect(fill = "black"),
plot.background = element_rect(fill = "black")
)
# add transition
ani <- p +
transition_time(frame)+
ease_aes('linear')+
enter_fade()+
exit_fade()
# define the number of data points
nfr <- df_anim %>% pull(frame) %>% unique() %>% length()
# animate
animate(
ani,
nframes = nfr, fps = 30,
# device = "svg", renderer = magick_renderer(loop = TRUE),
width = 500, height = 500
)
# save the output
anim_save("animated-snowflakes.gif")
Display the source blob
Display the rendered blob
Raw
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:cc="http://creativecommons.org/ns#"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:svg="http://www.w3.org/2000/svg"
xmlns="http://www.w3.org/2000/svg"
height="1198.6073"
width="1068.8712"
id="svg835"
version="1.1"
viewBox="0 0 1068.8712 1198.6073">
<metadata
id="metadata841">
<rdf:RDF>
<cc:Work
rdf:about="">
<dc:format>image/svg+xml</dc:format>
<dc:type
rdf:resource="http://purl.org/dc/dcmitype/StillImage" />
<dc:title></dc:title>
</cc:Work>
</rdf:RDF>
</metadata>
<defs
id="defs839" />
<path
id="path981"
d="m 509.4924,1187.9324 c -23.10969,-15.3072 -24.95889,-22.4394 -24.95889,-96.2644 0,-53.6265 -0.6789,-63.8585 -4.4898,-67.6694 -2.4694,-2.4694 -6.4253,-4.4898 -8.791,-4.4898 -2.3657,0 -18.1438,7.7143 -35.0625,17.1429 -24.8873,13.8693 -33.2212,17.1428 -43.6436,17.1428 -39.2135,0 -57.4816,-52.2221 -27.5703,-78.81351 3.685,-3.276 24.7001,-16.0084 46.7001,-28.2941 70.519,-39.3807 69.9743,-38.6567 69.9893,-93.0352 0.014,-55.2064 -1.903,-56.0793 -52.2037,-23.7553 -43.6576,28.0551 -42.9251,28.5122 -44.9285,-28.0341 -1.8267,-51.5598 -1.9893,-51.7135 -35.7258,-33.7821 -59.07107,31.397 -56.78435,27.1126 -58.55992,109.7176 l -1.42858,66.4614 -7.77946,10.1894 c -8.27124,10.8337 -28.30292,22.2031 -39.11938,22.2031 -10.27838,0 -24.42072,-7.9575 -31.92947,-17.9659 -6.27533,-8.3643 -7.01158,-12.6174 -8.31407,-48.0281 -1.45553,-39.5711 -3.9214,-48.2917 -13.65516,-48.2917 -4.09092,0 -34.54825,16.2479 -91.939704,49.0466 -20.97105,11.9847 -26.2794,13.8105 -40.1533,13.8105 -11.35163,0 -20.16878,-2.0836 -30.40736,-7.1857 -19.6178903,-9.776 -24.95752026,-20.0819 -23.3647603,-45.0957 1.02745,-16.1359 2.45422,-20.3437 9.3323603,-27.5229 4.46572,-4.6612 32.89836,-22.0919 63.18364,-38.7347 30.285294,-16.6429 56.647694,-32.1678 58.583124,-34.4998 6.7482,-8.1311 -1.05844,-15.7732 -32.96739,-32.2727 -38.031334,-19.6652 -42.897644,-25.3829 -42.897644,-50.4028 0,-13.9512 1.38471,-20.0194 5.98291,-26.2191 9.34149,-12.595 26.15146,-20.8262 42.588524,-20.8539 13.02508,-0.023 18.57186,2.5301 62.85714,28.9206 53.51864,31.8929 63.67188,35.8825 79.9089,31.3993 13.32796,-3.68 81.91317,-43.495 80.78006,-46.8944 -0.45637,-1.3691 -18.03753,-12.394 -39.06921,-24.4998 -21.03167,-12.1059 -39.56829,-24.1173 -41.19247,-26.6922 -4.18161,-6.6292 -0.19592,-10.5065 28.91846,-28.1316 32.05169,-19.4033 34.93998,-22.0124 34.93998,-31.5629 0,-8.9973 -3.13869,-11.5592 -40,-32.6491 -19.84441,-11.3539 -26.89825,-13.927 -38.57143,-14.0702 -15.21153,-0.1866 -14.73726,-0.4169 -89.98591,43.6808 -25.4472,14.9127 -32.05946,17.5725 -43.68525,17.5725 -29.837734,0 -50.848324,-22.3807 -46.505664,-49.5381 3.14311,-19.656 10.59193,-27.9566 40.13389,-44.7233 50.581444,-28.7077 50.228324,-29.867 -20.74354,-68.1008 -29.56601,-15.9278 -57.53379,-32.9023 -62.15063,-37.7213 -7.4133803,-7.7379 -8.5118703,-11.1661 -9.4008803,-29.3391 -0.87182,-17.8217 -0.10999,-21.9166 5.68874,-30.5774 8.6778603,-12.961 28.2907503,-22.8571 45.2999203,-22.8571 11.51426,0 19.98901,3.9181 67.978014,31.4285 53.3956,30.6099 65.23197,35.2289 71.35843,27.847 1.63489,-1.97 3.39946,-21.1666 3.92127,-42.6592 1.05168,-43.31675 2.87903,-48.31038 22.64003,-61.86862 19.06877,-13.0833 51.74607,-0.95172 62.75598,23.29842 4.78,10.5283 5.43474,19.2115 5.49,72.81 0.0694,67.2361 1.04846,71.8407 17.91689,84.257 11.76285,8.6584 53.11042,32.1074 60.86722,34.519 3.3234,1.0333 8.2749,0.064 11.4285,-2.2372 4.9372,-3.6026 5.7255,-8.6767 6.993,-45.0134 1.3775,-39.4881 1.653,-40.9849 7.7034,-41.8459 3.4512,-0.491 17.1168,4.9687 30.3681,12.1327 38.3534,20.7351 40.1781,21.5812 46.5787,21.5997 11.5637,0.033 12.9758,-5.7485 11.9919,-49.1037 -0.8372,-36.8855 -1.5509,-41.7522 -7.3066,-49.8213 -4.2258,-5.9242 -23.8348,-18.9074 -58.0959,-38.4655 -32.7537,-18.69768 -53.9337,-32.63612 -57.7627,-38.01337 -3.8705,-5.4357 -6.4456,-14.21149 -7.157,-24.3916 -1.2277,-17.5662 4.7513,-29.81961 20.0974,-41.18787 14.5721,-10.79488 30.9363,-8.12386 64.0323,10.45157 29.0059,16.27987 39.7009,19.25614 46.1218,12.83517 2.461,-2.46101 3.4419,-21.6106 3.4756,-67.85714 0.045,-61.503186 0.3389,-64.943976 6.4761,-75.779806 9.065,-16.0053 27.41729,-27.6963802 43.47689,-27.6963802 16.0596,0 34.4119,11.6910802 43.4769,27.6963802 6.1193,10.80428 6.4314,14.3546 6.4761,73.681916 0.029,37.40762 1.2296,64.54046 3.0047,67.85715 4.905,9.16516 16.427,6.48599 46.8604,-10.89629 24.6075,-14.05481 30.8452,-16.42359 43.2614,-16.42858 17.48,-0.007 32.366,9.80878 41.2326,27.18873 7.4705,14.64344 7.1591,24.61227 -1.2004,38.42303 -5.7852,9.55767 -15.4728,16.26769 -61.8756,42.85714 -32.0382,18.35835 -57.5087,34.88965 -60.9834,39.58055 -5.2824,7.131 -6.0734,12.8044 -6.8948,49.45 -0.9566,42.6861 0.527,49.6375 10.5946,49.6375 2.5747,0 20.0128,-8.9596 38.7512,-19.9103 25.8426,-15.1023 34.9768,-19.1575 37.8259,-16.7929 2.7501,2.2823 3.7674,13.163 3.7982,40.6245 0.067,59.5934 4.9055,63.0704 51.7853,37.2137 34.9806,-19.2937 44.371,-27.2802 48.1748,-40.9725 1.5527,-5.5894 2.8303,-38.9392 2.839,-74.1109 0.014,-59.8582 0.3978,-64.56627 5.9856,-73.60756 14.7278,-23.83011 53.5301,-26.44232 74.3268,-5.00376 l 9.7036,10.00307 0.9379,39.13555 c 1.0155,42.372 3.8837,50.7915 16.4823,48.3832 3.3831,-0.6468 31.0619,-14.5635 61.5083,-30.9261 48.4871,-26.0581 57.0171,-29.7248 68.732,-29.5457 15.7912,0.2415 33.6915,10.7078 42.6031,24.9098 4.6233,7.3681 5.5672,12.9447 4.8005,28.3614 -1.2809,25.7538 -5.6113,29.9017 -68.4209,65.5363 -27.7907,15.7668 -52.7512,31.123 -55.4678,34.1248 -8.478,9.3681 -1.6424,17.4729 29.0128,34.3999 34.9405,19.2932 42.4453,28.3157 42.575,51.1849 0.1526,26.8816 -16.6225,44.3063 -42.2661,43.9031 -13.5761,-0.2135 -18.9813,-2.7709 -66.2114,-31.3271 -50.12,-30.3034 -51.8666,-31.0972 -68.6428,-31.1961 -16.5187,-0.097 -18.4693,0.7066 -48.2736,19.8985 -17.0826,11 -32.347,22.051 -33.9208,24.5577 -6.2727,9.9913 0.01,17.9345 27.7323,35.0759 32.3235,19.9846 38.4181,25.8211 34.1212,32.6765 -1.666,2.658 -15.4813,11.998 -30.7006,20.7555 -33.3208,19.1735 -37.3272,22.8057 -35.0709,31.7955 1.2944,5.1573 9.9588,11.0874 36.3143,24.8544 32.7868,17.1264 35.4694,18.0182 50.8132,16.8934 13.4013,-0.9823 21.3805,-4.1595 46.1991,-18.3959 63.1714,-36.236 73.1681,-41.0702 84.9292,-41.0702 24.0255,0 45.1587,25.5766 42.8474,51.8565 -1.6609,18.8842 -8.9204,26.9438 -40.9617,45.4761 -26.903,15.5604 -28.2435,16.791 -28.2435,25.9301 0,9.3872 1.0931,10.2484 50.6054,39.8685 59.6301,35.6731 58.8301,35.0657 65.499,49.726 16.6443,36.5898 -10.0847,72.538 -51.6143,69.4166 -13.5904,-1.0215 -21.8064,-4.5757 -56.1754,-24.3008 -63.328,-36.3454 -63.2241,-36.3004 -71.404,-30.9407 -6.691,4.3841 -6.9107,5.648 -6.9107,39.754 0,31.9085 -0.6118,36.1337 -6.4965,44.8661 -9.7453,14.461 -23.5939,22.6334 -38.3536,22.6334 -15.8529,0 -26.9433,-5.6879 -37.145,-19.0504 l -8.0049,-10.485 -1.4285,-65.9466 c -1.3344,-61.5992 -1.8322,-66.5117 -7.5514,-74.5181 -7.0735,-9.9023 -69.9992,-47.1428 -79.6577,-47.1428 -11.7746,0 -14.1283,7.0436 -15.6481,46.8277 -1.2178,31.8808 -2.1286,37.5305 -6.1744,38.3023 -2.6103,0.498 -18.6817,-7.5202 -35.7143,-17.8183 -47.3633,-28.636 -49.5399,-27.4836 -49.5399,26.2307 0,56.3689 -2.0982,53.559 70,93.7416 57.1996,31.879 62.9584,38.2046 60.8846,66.87541 -1.8836,26.0394 -16.3638,40.1263 -41.2467,40.1263 -11.5192,0 -18.7628,-2.7981 -44.3784,-17.1428 -16.8367,-9.4286 -32.5303,-17.1429 -34.8746,-17.1429 -2.3443,0 -6.2828,2.0204 -8.7522,4.4898 -3.8167,3.8168 -4.492,14.2032 -4.5044,69.2857 -0.014,62.5288 -0.2443,65.1753 -6.5791,75.6397 -3.6105,5.9641 -12.2824,14.6371 -19.2711,19.2735 -17.2479,11.4424 -31.6181,11.365 -49.1763,-0.2652 z"
style="fill:#ffffff;fill-opacity:1;stroke:#ffffff;stroke-width:3.77953;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1" />
</svg>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment