/
stat-summary-address.R
166 lines (150 loc) · 5.7 KB
/
stat-summary-address.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
#' Summarize IP addresses on a heatmap
#'
#' Addresses are grouped into networks determined by the `pixel_prefix` argument
#' of `coord_ip()`. Then the `z` values are summarized with summary function `fun`.
#'
#' @inheritParams ggplot2::layer
#' @inheritParams ggplot2::geom_point
#' @param fun Summary function (see section below for details). If `NULL` (the
#' default), the observations are simply counted.
#' @param fun.args A list of extra arguments to pass to `fun`.
#'
#' @section Aesthetics:
#' `stat_summary_address()` understands the following aesthetics (required
#' aesthetics are in bold):
#' - **`ip`**: An [`ip_address`][`ipaddress::ip_address`] column
#' - `z`: Value passed to the summary function (required if `fun` is used)
#' - `fill`: Default is `after_stat(value)`
#' - `alpha`
#'
#' @section Computed variables:
#' The following variables are available to [`after_stat()`][ggplot2::after_stat()]:
#' - `value`: Value of summary statistic
#' - `count`: Number of observations
#'
#' @section Summary function:
#' The `data` might contain multiple rows per pixel of the heatmap, so a summary
#' function reduces this information to a single value to display.
#' This function receives the `data` column specified by the `z` aesthetic
#' and also receives arguments specified by `fun.args`.
#'
#' The `fun` argument can be specified in multiple ways:
#' \describe{
#' \item{`NULL`}{If no summary function is provided, the number of observations
#' is computed. In this case, you don't need to specify the `z` aesthetic,
#' and the computed variables `value` and `count` will be equal.}
#' \item{string}{The name of an existing function (e.g. `fun = "mean"`).}
#' \item{function}{Either provide an existing function (e.g. `fun = mean`) or
#' define a new function (e.g. `fun = function(x) sum(x^2)`).}
#' \item{formula}{A function can also be created from a formula. This uses `.x`
#' as the summarized variable (e.g. `fun = ~ sum(.x^2)`).}
#' }
#'
#' @examples
#' dat <- data.frame(
#' ip = sample_ipv4(10000),
#' weight = runif(10000)
#' )
#'
#' p <- ggplot(dat, aes(ip = ip)) +
#' coord_ip() +
#' theme_ip_light()
#'
#' # simple count of observations
#' p +
#' stat_summary_address() +
#' scale_fill_viridis_c(trans = "log2", na.value = "black", guide = "none")
#'
#' # compute mean weight
#' p +
#' stat_summary_address(aes(z = weight), fun = ~ mean(.x)) +
#' scale_fill_viridis_c(na.value = "black", guide = "none")
#' @export
stat_summary_address <- function(mapping = NULL, data = NULL, ...,
fun = NULL, fun.args = list(),
na.rm = FALSE, show.legend = NA,
inherit.aes = TRUE) {
ggplot2::layer(
stat = StatSummaryAddress, data = data, mapping = mapping, geom = "raster",
position = "identity", show.legend = show.legend, inherit.aes = inherit.aes,
params = list(
na.rm = na.rm,
fun = fun,
fun.args = fun.args,
...
)
)
}
StatSummaryAddress <- ggplot2::ggproto("StatSummaryAddress", ggplot2::Stat,
default_aes = ggplot2::aes(
ip = NULL,
z = NULL,
fill = ggplot2::after_stat(value)
),
# The `ip` aesthetic is required, but putting it in required_aes causes a very
# slow check for missing values. It's much faster to simply check `x` and `y`
# for missing values. These are always NA when `ip` is NA.
required_aes = c("x", "y"),
dropped_aes = c("ip", "z"),
extra_params = c("na.rm", "fun", "fun.args"),
compute_layer = function(self, data, params, layout) {
if (!is_CoordIp(layout$coord)) {
cli::cli_abort("{.pkg ggip} plots require {.fn coord_ip}.")
}
# validate ip aesthetic
if (is.null(data$ip)) {
cli::cli_abort("{.fn {snake_class(self)}} requires an {.field ip} aesthetic.")
} else if (is_ip_address_coords(data$ip)) {
data$x <- data$ip$x
data$y <- data$ip$y
data$ip <- data$ip$ip
} else if (is_ip_address(data$ip)) {
cli::cli_abort("The {.field ip} aesthetic of {.fn {snake_class(self)}} must map to a {.arg data} variable.")
} else {
cli::cli_abort(c(
"The {.field ip} aesthetic of {.fn {snake_class(self)}} must be {.type {ip_address()}}.",
"x" = "You supplied {.type {data$ip}}."
))
}
if (!is.null(params$fun) && !("z" %in% colnames(data))) {
cli::cli_abort("{.fn {snake_class(self)}} requires a {.field z} aesthetic when using the {.arg fun} argument.")
}
# add coord to the params, so it reaches compute_group()
params$coord <- layout$coord
ggproto_parent(Stat, self)$compute_layer(data, params, layout)
},
compute_group = function(data, scales, coord,
fun = NULL, fun.args = list(), ...) {
summarize_addresses(data, scales, coord, fun, fun.args)
}
)
summarize_addresses <- function(data, scales, coord, fun, fun.args) {
summarize_grid <- function(x, index, fun) {
grps <- split(x, index)
names(grps) <- NULL
unlist(lapply(grps, fun))
}
# support formula interface
if (is_formula(fun)) {
fun <- as_function(fun)
}
summarize_count <- is.null(fun)
# summarize grid found in data
index <- list(x = data$x, y = data$y)
labels <- lapply(index, function(x) sort(unique(x)))
out <- expand.grid(labels, KEEP.OUT.ATTRS = FALSE)
out$count <- summarize_grid(data$x, index, length)
out$value <- if (summarize_count) {
out$count
} else {
f <- function(x) do.call(fun, c(list(quote(x)), fun.args))
summarize_grid(data$z, index, f)
}
# fill remaining grid so raster works
range <- coord$limits$x[1]:coord$limits$x[2]
fill_na <- list(count = 0)
if (summarize_count) {
fill_na$value <- 0
}
tidyr::complete(out, tidyr::expand(out, x = range, y = range), fill = fill_na)
}