-
Notifications
You must be signed in to change notification settings - Fork 181
/
02_functions.R
127 lines (113 loc) · 3.76 KB
/
02_functions.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
126
127
proc_data <- function(x, .id = "x", color_fun = colorize_keys, color_when = c("after", "before"), ...) {
color_when <- match.arg(color_when)
n_colors <- max(x$id)
if (color_when == "before") x <- color_fun(x, n_colors, ...)
x <- x %>%
mutate(.y = -row_number()) %>%
tidyr::gather("label", "value", setdiff(colnames(x), c(".y", "color"))) %>%
mutate(value = as.character(value)) %>%
group_by(.y) %>%
mutate(
.x = 1:n(),
.id = .id,
.width = 1
) %>%
ungroup(.y)
if (color_when == "after") x <- color_fun(x, n_colors, ...)
x
}
colorize_keys <- function(df, n_colors, key_col = "id", color_other = "#d0d0d0", color_missing = "#ffffff") {
# Assumes that key_col is integer
colors <- scales::brewer_pal(type = "qual", "Set1")(n_colors)
mutate(
df,
color = ifelse(label == key_col, value, n_colors + 1),
color = colors[as.integer(color)],
color = ifelse(is.na(color), "#d0d0d0", color),
color = ifelse(is.na(value), "#ffffff", color)
)
}
colorize_row_id <- function(df, n_colors, key_col = "id") {
# Assumes that key_col is integer
colors <- scales::brewer_pal(type = "qual", "Set1")(n_colors)
df$color <- colors[df[[key_col]]]
df
}
colorize_wide_tidyr <- function(df, n_colors, key_col = "id") {
n_colors <- n_colors + length(setdiff(unique(df$label), key_col))
colors <- scales::brewer_pal(type = "qual", "Set1")(n_colors)
df$value_int <- as.integer(gsub("[a-zA-Z]", "0", df$value))
max_id_color <- max(df$value_int)
df %>%
bind_rows(
filter(df, .y == "-1") %>% mutate(.y = 0)
) %>%
mutate(
idcp = max_id_color - 1L,
idc = case_when(
label == "id" ~ value_int,
TRUE ~ map_int(label, ~which(. == unique(label))) + idcp
)
) %>%
select(-idcp, -value_int) %>%
mutate(
idc = ifelse(.y == 0 & label == "id", 100, idc),
value = ifelse(.y == 0, label, value),
.id = ifelse(.y == 0, "n", .id),
color = colors[idc],
) %>%
filter(!is.na(color)) %>%
mutate(alpha = ifelse(label != "id" & .y < 0, 0.6, 1.0)) %>%
select(-idc)
}
copy_rows <- function(x, ..., n = 1) {
y <- filter(x, ...)
for (i in seq_len(n)) {
x <- bind_rows(x, y)
}
x
}
plot_data <- function(x, title = "") {
if (!"alpha" %in% colnames(x)) x$alpha <- 1
if (!".text_color" %in% colnames(x)) x$`.text_color` <- "white"
if (!".text_size" %in% colnames(x)) x$`.text_size` <- 12
if (!".text_alpha" %in% colnames(x)) x$`.text_alpha` <- 1
ggplot(x) +
aes(.x, .y, fill = color, label = value) +
geom_tile(aes(alpha = alpha), width = 0.9, height = 0.9) +
geom_text(
aes(x = .x, color = .text_color, size = .text_size, alpha = .text_alpha),
hjust = 0.5,
family = "Fira Sans"
) +
scale_fill_identity() +
scale_alpha_identity() +
scale_color_identity() +
scale_size_identity() +
coord_equal() +
ggtitle(title) +
theme_void() +
theme(
plot.title = element_text(family = "Fira Mono", hjust = 0.5, size = 24),
plot.background = element_rect(fill = "white", color = "white")
) +
guides(fill = "none")
}
animate_plot <- function(x, transition_length = 2, state_length = 1, wrap = TRUE) {
x +
transition_states(frame, transition_length, state_length, wrap = wrap) +
enter_fade() +
exit_fade() +
ease_aes("sine-in-out")
}
save_static_plot <- function(g, filename, formats = c("png", "svg"), ...) {
ggsave <- purrr::partial(ggplot2::ggsave, plot = g, ...)
formats %>%
purrr::set_names() %>%
purrr::map_chr(static_plot_filename, x = filename) %>%
purrr::iwalk(~ ggsave(filename = .x, device = .y))
}
static_plot_filename <- function(x, ext) {
here::here("images", "static", ext, paste0(x, ".", ext))
}
options(tidy_verb_anim.functions_loaded = TRUE)