-
Notifications
You must be signed in to change notification settings - Fork 21
/
stat-mosaic-jitter.r
268 lines (220 loc) · 7.64 KB
/
stat-mosaic-jitter.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
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
#' @rdname geom_mosaic_jitter
#' @inheritParams ggplot2::stat_identity
#' @section Computed variables:
#' \describe{
#' \item{xmin}{location of bottom left corner}
#' \item{xmax}{location of bottom right corner}
#' \item{ymin}{location of top left corner}
#' \item{ymax}{location of top right corner}
#' }
#' @export
stat_mosaic_jitter <- function(mapping = NULL, data = NULL, geom = "mosaic_jitter",
position = "identity", na.rm = FALSE, divider = mosaic(),
show.legend = NA, inherit.aes = TRUE, offset = 0.01,
drop_level = FALSE, seed = NA, ...)
{
if (!is.null(mapping$y)) {
stop("stat_mosaic() must not be used with a y aesthetic.", call. = FALSE)
} else mapping$y <- structure(1L, class = "productlist")
aes_x <- mapping$x
if (!is.null(aes_x)) {
aes_x <- rlang::eval_tidy(mapping$x)
var_x <- paste0("x__", as.character(aes_x))
}
aes_fill <- mapping$fill
var_fill <- ""
if (!is.null(aes_fill)) {
aes_fill <- rlang::quo_text(mapping$fill)
var_fill <- paste0("x__fill__", aes_fill)
if (aes_fill %in% as.character(aes_x)) {
idx <- which(aes_x == aes_fill)
var_x[idx] <- var_fill
} else {
mapping[[var_fill]] <- mapping$fill
}
}
aes_alpha <- mapping$alpha
var_alpha <- ""
if (!is.null(aes_alpha)) {
aes_alpha <- rlang::quo_text(mapping$alpha)
var_alpha <- paste0("x__alpha__", aes_alpha)
if (aes_alpha %in% as.character(aes_x)) {
idx <- which(aes_x == aes_alpha)
var_x[idx] <- var_alpha
} else {
mapping[[var_alpha]] <- mapping$alpha
}
}
# aes_x <- mapping$x
if (!is.null(aes_x)) {
mapping$x <- structure(1L, class = "productlist")
for (i in seq_along(var_x)) {
mapping[[var_x[i]]] <- aes_x[[i]]
}
}
aes_conds <- mapping$conds
if (!is.null(aes_conds)) {
aes_conds <- rlang::eval_tidy(mapping$conds)
mapping$conds <- structure(1L, class = "productlist")
var_conds <- paste0("conds", seq_along(aes_conds), "__", as.character(aes_conds))
for (i in seq_along(var_conds)) {
mapping[[var_conds[i]]] <- aes_conds[[i]]
}
}
ggplot2::layer(
data = data,
mapping = mapping,
stat = StatMosaicJitter,
geom = geom,
position = position,
show.legend = show.legend,
inherit.aes = inherit.aes,
check.aes = FALSE,
params = list(
na.rm = na.rm,
divider = divider,
offset = offset,
drop_level = drop_level,
seed = seed,
...
)
)
}
#' Geom proto
#'
#' @format NULL
#' @usage NULL
#' @export
StatMosaicJitter <- ggplot2::ggproto(
"StatMosaicJitter", ggplot2::Stat,
#required_aes = c("x"),
non_missing_aes = c("weight", "size", "shape", "colour"),
default_aes = aes(
shape = 19, colour = "black", size = 1.5, fill = NA,
alpha = NA, stroke = 0.5
),
setup_params = function(data, params) {
#cat("setup_params from StatMosaic\n")
#browser()
# if (!is.null(data$y)) {
# stop("stat_mosaic() must not be used with a y aesthetic.", call. = FALSE)
# }
params
},
setup_data = function(data, params) {
#cat("setup_data from StatMosaic\n")
#browser()
data
},
compute_panel = function(self, data, scales, na.rm=FALSE, drop_level=FALSE, seed = NA, divider, offset) {
#cat("compute_panel from StatMosaic\n")
#browser()
# vars <- names(data)[grep("x[0-9]+__", names(data))]
vars <- names(data)[grep("x__", names(data))]
conds <- names(data)[grep("conds[0-9]+__", names(data))]
if (length(vars) == 0) formula <- "1"
else formula <- paste(vars, collapse="+")
formula <- paste("weight~", formula)
if (length(conds) > 0) formula <- paste(formula, paste(conds, collapse="+"), sep="|")
df <- data
if (!in_data(df, "weight")) {
df$weight <- 1
}
res <- prodcalc(df, formula=as.formula(formula),
divider = divider, cascade=0, scale_max = TRUE,
na.rm = na.rm, offset = offset)
# browser()
# consider 2nd weight for points
if (in_data(df, "weight2")) {
formula2 <- str_replace(formula, "weight", "weight2")
res2 <- prodcalc(df, formula = as.formula(formula2), divider = divider,
cascade = 0, scale_max = TRUE, na.rm = na.rm, offset = offset)
res$.n2 <- res2$.n
}
# need to set x variable - I'd rather set the scales here.
prs <- parse_product_formula(as.formula(formula))
p <- length(c(prs$marg, prs$cond))
if (is.function(divider)) divider <- divider(p)
# the level at which things are labelled could be made a parameter.
# At the moment the deepest level is being labelled.
dflist <- list(data=subset(res, level==max(res$level)), formula=as.formula(formula), divider=divider)
scx <- productplots::scale_x_product(dflist)
scy <- productplots::scale_y_product(dflist)
# res is data frame that has xmin, xmax, ymin, ymax
res <- dplyr::rename(res, xmin=l, xmax=r, ymin=b, ymax=t)
# res <- subset(res, level==max(res$level))
# export the variables with the data - terrible hack
# res$x <- list(scale=scx)
# if (!is.null(scales$y)) {
# # only set the y scale if it is a product scale, otherwise leave it alone
# if ("ScaleContinuousProduct" %in% class(scales$y))
# res$y <- list(scale=scy)
# }
# XXXX add label for res
cols <- c(prs$marg, prs$cond)
if (length(cols) > 1) {
df <- res[,cols]
df <- tidyr::unite(df, "label", cols, sep="\n")
res$label <- df$label
} else res$label <- as.character(res[,cols])
res$x <- list(scale=scx)
if (!is.null(scales$y)) {
# only set the y scale if it is a product scale, otherwise leave it alone
if ("ScaleContinuousProduct" %in% class(scales$y))
res$y <- list(scale=scy)
}
# merge res with data:
# is there a fill/alpha/color variable?
fill_idx <- grep("x__fill", names(data))
if (length(fill_idx) > 0) {
fill_res_idx <- grep("x__fill", names(res))
res$fill <- res[[fill_res_idx]]
}
alpha_idx <- grep("x__alpha", names(data))
if (length(alpha_idx) > 0) {
alpha_res_idx <- grep("x__alpha", names(res))
res$alpha <- res[[alpha_res_idx]]
}
colour_idx <- grep("x__colour", names(data))
if (length(colour_idx) > 0) {
colour_res_idx <- grep("x__colour", names(res)) # find what comes after __colour
res$colour <- res[[colour_res_idx]]
}
res$group <- 1 # unique(data$group) # ignore group variable
res$PANEL <- unique(data$PANEL)
# browser()
# generate points
# consider 2nd weight for point
if (in_data(res, ".n2")) {
res$.n <- res$.n2
}
sub <- subset(res, level==max(res$level))
if(drop_level) {
ll <- subset(res, level==max(res$level)-1)
sub <- dplyr::left_join(select(sub, -(xmin:ymax)), select(ll, contains("x__"), xmin:ymax, -contains("col")))
}
# create a set of uniformly spread points between 0 and 1 once, when the plot is created.
# the transformation to the correct scale happens in compute panel.
# altered from ggrepel:
# Make reproducible if desired.
if (!is.null(seed) && is.na(seed)) {
seed <- sample.int(.Machine$integer.max, 1L)
}
points <- subset(sub, sub$.n>=1)
points <- tidyr::nest(points, data = -label)
points <- with_seed_null(seed,
dplyr::mutate(
points,
coords = purrr::map(data, .f = function(d) {
data.frame(
x = runif(d$.n, min = 0, max = 1),
y = runif(d$.n, min = 0, max = 1),
dplyr::select(d, -x, -y)
)
})
))
points <- tidyr::unnest(points, coords)
# browser()
points
}
)