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
|
#' @param bins numeric vector giving number of bins in both vertical and
#' horizontal directions. Set to 30 by default.
#' @param binwidth Numeric vector giving bin width in both vertical and
#' horizontal directions. Overrides `bins` if both set.
#' @param drop if `TRUE` removes all cells with 0 counts.
#' @export
#' @rdname geom_bin2d
#' @section Computed variables:
#' \describe{
#' \item{count}{number of points in bin}
#' \item{density}{density of points in bin, scaled to integrate to 1}
#' \item{ncount}{count, scaled to maximum of 1}
#' \item{ndensity}{density, scaled to maximum of 1}
#' }
stat_bin_2d <- function(mapping = NULL, data = NULL,
geom = "tile", position = "identity",
...,
bins = 30,
binwidth = NULL,
drop = TRUE,
na.rm = FALSE,
show.legend = NA,
inherit.aes = TRUE) {
layer(
data = data,
mapping = mapping,
stat = StatBin2d,
geom = geom,
position = position,
show.legend = show.legend,
inherit.aes = inherit.aes,
params = list(
bins = bins,
binwidth = binwidth,
drop = drop,
na.rm = na.rm,
...
)
)
}
#' @export
#' @rdname geom_bin2d
#' @usage NULL
stat_bin2d <- stat_bin_2d
#' @rdname ggplot2-ggproto
#' @format NULL
#' @usage NULL
#' @export
StatBin2d <- ggproto("StatBin2d", Stat,
default_aes = aes(fill = after_stat(count)),
required_aes = c("x", "y"),
compute_group = function(data, scales, binwidth = NULL, bins = 30,
breaks = NULL, origin = NULL, drop = TRUE) {
origin <- dual_param(origin, list(NULL, NULL))
binwidth <- dual_param(binwidth, list(NULL, NULL))
breaks <- dual_param(breaks, list(NULL, NULL))
bins <- dual_param(bins, list(x = 30, y = 30))
xbreaks <- bin2d_breaks(scales$x, breaks$x, origin$x, binwidth$x, bins$x)
ybreaks <- bin2d_breaks(scales$y, breaks$y, origin$y, binwidth$y, bins$y)
xbin <- cut(data$x, xbreaks, include.lowest = TRUE, labels = FALSE)
ybin <- cut(data$y, ybreaks, include.lowest = TRUE, labels = FALSE)
if (is.null(data$weight))
data$weight <- 1
out <- tapply_df(data$weight, list(xbin = xbin, ybin = ybin), sum, drop = drop)
xdim <- bin_loc(xbreaks, out$xbin)
out$x <- xdim$mid
out$width <- xdim$length
ydim <- bin_loc(ybreaks, out$ybin)
out$y <- ydim$mid
out$height <- ydim$length
out$count <- out$value
out$ncount <- out$count / max(out$count, na.rm = TRUE)
out$density <- out$count / sum(out$count, na.rm = TRUE)
out$ndensity <- out$density / max(out$density, na.rm = TRUE)
out
}
)
dual_param <- function(x, default = list(x = NULL, y = NULL)) {
if (is.null(x)) {
default
} else if (length(x) == 2) {
if (is.list(x) && !is.null(names(x))) {
x
} else {
list(x = x[[1]], y = x[[2]])
}
} else {
list(x = x, y = x)
}
}
bin2d_breaks <- function(scale, breaks = NULL, origin = NULL, binwidth = NULL,
bins = 30, right = TRUE) {
# Bins for categorical data should take the width of one level,
# and should show up centered over their tick marks. All other parameters
# are ignored.
if (scale$is_discrete()) {
breaks <- scale$get_breaks()
return(-0.5 + seq_len(length(breaks) + 1))
} else {
if (!is.null(breaks)) {
breaks <- scale$transform(breaks)
}
}
if (!is.null(breaks))
return(breaks)
range <- scale$get_limits()
if (is.null(binwidth) || identical(binwidth, NA)) {
binwidth <- diff(range) / bins
}
if (!(is.numeric(binwidth) && length(binwidth) == 1)) abort("`binwidth` must be a numeric scalar")
if (is.null(origin) || identical(origin, NA)) {
origin <- round_any(range[1], binwidth, floor)
}
if (!(is.numeric(origin) && length(origin) == 1)) abort("`origin` must be a numeric scalar")
breaks <- seq(origin, range[2] + binwidth, binwidth)
adjust_breaks(breaks, right)
}
adjust_breaks <- function(x, right = TRUE) {
diddle <- 1e-07 * stats::median(diff(x))
if (right) {
fuzz <- c(-diddle, rep.int(diddle, length(x) - 1))
} else {
fuzz <- c(rep.int(-diddle, length(x) - 1), diddle)
}
sort(x) + fuzz
}
bin_loc <- function(x, id) {
left <- x[-length(x)]
right <- x[-1]
list(
left = left[id],
right = right[id],
mid = ((left + right) / 2)[id],
length = diff(x)[id]
)
}
|