-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest_simple_dice.R
More file actions
168 lines (143 loc) · 5.28 KB
/
test_simple_dice.R
File metadata and controls
168 lines (143 loc) · 5.28 KB
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#!/usr/bin/env Rscript
# Demonstration of pip_scale auto-scaling in geom_dice
library(ggplot2)
library(dplyr)
library(legendry)
library(scales)
library(grid)
library(patchwork)
source("R/utils.R")
source("R/geom-dice-ggprotto.R")
source("R/geom-dice.R")
# ---- shared toy data -------------------------------------------------------
set.seed(42)
test_data <- data.frame(
x = rep(1:4, each = 4),
y = rep(1:2, times = 8),
category = rep(c("A", "B", "C", "D"), times = 4),
value = runif(16, -2, 2)
)
# ---- Plot 1: pip_scale comparison (0.5 / 0.75 / 0.9 / 1.0 / NULL) ----------
make_base_plot <- function(pip_scale_val, title_label) {
ggplot(test_data, aes(x = x, y = y)) +
geom_dice(
aes(dots = category, fill = value),
ndots = 4,
x_length = 4,
y_length = 2,
pip_scale = pip_scale_val
) +
scale_fill_gradient2(
low = "#2166AC", high = "#762A83", mid = "white",
midpoint = 0, name = "value"
) +
labs(title = title_label) +
theme(plot.title = element_text(size = 10))
}
p_050 <- make_base_plot(0.50, "pip_scale = 0.50")
p_075 <- make_base_plot(0.75, "pip_scale = 0.75 (default)")
p_090 <- make_base_plot(0.90, "pip_scale = 0.90")
p_100 <- make_base_plot(1.00, "pip_scale = 1.00 (tight)")
p_off <- make_base_plot(NULL, "pip_scale = NULL (fixed 3 mm)")
comparison <- (p_050 + p_075 + p_090) / (p_100 + p_off + plot_spacer()) +
plot_annotation(
title = "pip_scale comparison",
subtitle = "pip_scale scales both pip size and positions; NULL = legacy fixed size"
)
ggsave("demo_output/pip_scale_comparison.png", comparison, width = 14, height = 9, dpi = 300)
# ---- Plot 2: tile size vs pip_scale = 0.75 and 1.0 -------------------------
make_tile_size_plot <- function(tile_w, title_label, pip_scale_val = 0.75) {
ggplot(test_data, aes(x = x, y = y)) +
geom_dice(
aes(dots = category, fill = value, width = tile_w, height = tile_w),
ndots = 4,
x_length = 4,
y_length = 2,
pip_scale = pip_scale_val
) +
scale_fill_gradient2(
low = "#2166AC", high = "#762A83", mid = "white",
midpoint = 0, name = "value"
) +
labs(title = title_label) +
theme(plot.title = element_text(size = 9))
}
# row 1: pip_scale = 0.75
p_75_04 <- make_tile_size_plot(0.40, "pf=0.75, tile=0.40", 0.75)
p_75_07 <- make_tile_size_plot(0.70, "pf=0.75, tile=0.70", 0.75)
p_75_095 <- make_tile_size_plot(0.95, "pf=0.75, tile=0.95", 0.75)
# row 2: pip_scale = 1.0
p_10_04 <- make_tile_size_plot(0.40, "pf=1.00, tile=0.40", 1.00)
p_10_07 <- make_tile_size_plot(0.70, "pf=1.00, tile=0.70", 1.00)
p_10_095 <- make_tile_size_plot(0.95, "pf=1.00, tile=0.95", 1.00)
tile_comparison <- (p_75_04 + p_75_07 + p_75_095) /
(p_10_04 + p_10_07 + p_10_095) +
plot_annotation(
title = "Auto-scaling adapts to tile width",
subtitle = "Row 1: pip_scale=0.75 | Row 2: pip_scale=1.00 (tight packing)"
)
ggsave("demo_output/tile_size_comparison.png", tile_comparison, width = 14, height = 9, dpi = 300)
# ---- Plot 3: ndots 2 to 6 at pip_scale = 0.75 and 1.0 ---------------------
make_single_tile <- function(n, pip_scale_val) {
d <- data.frame(
x = rep(1, n),
y = rep(1, n),
category = LETTERS[1:n],
value = rep(c(-1, 1, -0.6, 0.6, -0.8, 0.8), length.out = n) # no zeros
)
ggplot(d, aes(x = x, y = y)) +
geom_dice(
aes(dots = category, fill = value),
ndots = n,
x_length = 1,
y_length = 1,
pip_scale = pip_scale_val
) +
scale_fill_gradient2(
low = "#2166AC", high = "#762A83", mid = "white",
midpoint = 0, guide = "none"
) +
labs(title = paste0("n=", n)) +
theme_minimal() +
theme(
axis.text = element_blank(),
axis.title = element_blank(),
axis.ticks = element_blank(),
panel.grid = element_blank(),
plot.title = element_text(size = 10, hjust = 0.5)
)
}
row_75 <- lapply(2:6, make_single_tile, pip_scale_val = 0.75)
row_10 <- lapply(2:6, make_single_tile, pip_scale_val = 1.00)
ndots_comparison <- wrap_plots(c(row_75, row_10), nrow = 2) +
plot_annotation(
title = "ndots 2–6: pip positioning and sizing",
subtitle = "Top row: pip_scale=0.75 | Bottom row: pip_scale=1.00"
)
ggsave("demo_output/ndots_comparison.png", ndots_comparison, width = 14, height = 6, dpi = 300)
# ---- Plot 4: size mapping bypasses auto-scale (existing use case) ----------
set.seed(1)
test_data_sized <- test_data
test_data_sized$sig <- runif(16, 0.5, 4)
p_size_mapped <- ggplot(test_data_sized, aes(x = x, y = y)) +
geom_dice(
aes(dots = category, fill = value, size = sig),
ndots = 4,
x_length = 4,
y_length = 2
) +
scale_fill_gradient2(
low = "#2166AC", high = "#762A83", mid = "white",
midpoint = 0, name = "value"
) +
scale_size_continuous(range = c(1, 5), name = "size") +
labs(
title = "aes(size = ...) bypasses auto-scaling",
subtitle = "pip_scale is ignored; user-mapped sizes are respected"
)
ggsave("demo_output/size_mapped_example.png", p_size_mapped, width = 8, height = 5, dpi = 300)
cat("Generated files:\n")
cat("- demo_output/pip_scale_comparison.png\n")
cat("- demo_output/tile_size_comparison.png\n")
cat("- demo_output/ndots_comparison.png\n")
cat("- demo_output/size_mapped_example.png\n")