-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path04-magical.R
125 lines (110 loc) · 3.22 KB
/
04-magical.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# Load packages -----------------------------------------------------------
library(tidyverse)
library(magick)
library(imager)
library(packcircles)
library(scales)
library(gganimate)
#' Codes are adapted from https://chichacha.netlify.app/2018/12/22/bubble-packed-chart-with-r-using-packcircles-package/
# Preprocess and load image file ------------------------------------------
rid_logo_img <-
image_read("asset/r-indonesia.png")
tmp <- tempfile(fileext = ".png")
rid_logo_img %>%
image_background(color = "#F2F2F0") %>%
image_write(tmp)
rid_logo <-
load.image(tmp) %>%
as.data.frame(wide = "c") %>%
transmute(
img_x = x,
img_y = y,
colour = rgb(c.1, c.2, c.3)
)
# Calculate layout --------------------------------------------------------
headcount <- 3319 * 0.95 # take 5% of as bot :p
iconsize <- rbeta(headcount, 1, 0.7)
pack_layout <-
circleProgressiveLayout(
iconsize,
sizetype = "area"
) %>%
mutate(
img_x = floor(rescale(x, to = range(rid_logo$img_x))),
img_y = floor(rescale(y, to = range(rid_logo$img_y)))
) %>%
rowid_to_column("id") %>%
inner_join(rid_logo, by = c("img_x", "img_y"))
# Create dataset for plot -------------------------------------------------
to_plot <-
circleLayoutVertices(
pack_layout,
xysizecols = c("x", "y", "radius")
) %>%
inner_join(
select(pack_layout, id, colour),
by = "id"
) %>%
group_by(id) %>%
summarise(
x = mean(x),
y = mean(y),
colour = unique(colour)
) %>%
mutate(
icon = sample(
c(letters, LETTERS),
size = n(),
replace = TRUE
),
frame = sample(
seq.Date(
from = as.Date("2016-08-13"),
to = as.Date("2021-04-06"),
length.out = 7
),
size = n(),
replace = TRUE
)
)
# Create animation --------------------------------------------------------
anim <-
to_plot %>%
ggplot(aes(x, y, colour = colour)) +
geom_text(aes(label = icon), size = 10, family = "Wee People") +
scale_y_reverse() +
scale_colour_identity() +
labs(
title = "You are truly magical!",
subtitle = "a tribute to Indonesian #rstats family",
caption = "Headcount of member in Komunitas R Indonesia group\nVisualized by Muhammad Aswan Syahputra"
) +
theme_void(base_family = "Public Sans") +
theme(
plot.background = element_rect(fill = "#E5E5E3", colour = NA),
panel.background = element_rect(fill = "#E5E5E3", colour = NA),
plot.title.position = "plot",
plot.caption.position = "plot",
plot.title = element_text(hjust = 0.5, size = rel(2.75), family = "BF Tiny Hand", colour = "gray10"),
plot.subtitle = element_text(hjust = 0.5, size = rel(1.5), family = "Homemade Apple", colour = "gray30"),
plot.caption = element_text(hjust = 0.5, size = rel(1.25), colour = "gray30"),
plot.margin = margin(30, 25, 10, 25)
) +
coord_equal() +
transition_manual(frame, cumulative = TRUE) +
enter_appear() +
ease_aes("circular-in")
# Save animation ----------------------------------------------------------
anim_save(
"outfile/04-magical.gif",
animation = anim,
duration = 15,
fps = 30,
width = 8,
height = 9.5,
units = "in",
device = "png",
type = "cairo-png",
res = 150,
renderer = gifski_renderer(loop = TRUE)
)