Skip to content

default_aes not used in compute_*() methods #3860

Open
@thomasp85

Description

@thomasp85

While writing about extensions I noticed that the content of default_aes in a stat is not added to the data before it is passed through the compute_*() mill. This effectively renders it useless for anything but defining mappings for the geom.

The current code reflects this as it has all sorts of code around extracting non-required aesthetics from the data, e.g. in StatBinHex

ggplot2/R/stat-binhex.r

Lines 45 to 65 in 1223de2

StatBinhex <- ggproto("StatBinhex", Stat,
default_aes = aes(weight = 1, fill = after_stat(count)),
required_aes = c("x", "y"),
compute_group = function(data, scales, binwidth = NULL, bins = 30,
na.rm = FALSE) {
try_require("hexbin", "stat_binhex")
binwidth <- binwidth %||% hex_binwidth(bins, scales)
wt <- data$weight %||% rep(1L, nrow(data))
out <- hexBinSummarise(data$x, data$y, wt, binwidth, sum)
out$density <- as.vector(out$value / sum(out$value, na.rm = TRUE))
out$ndensity <- out$density / max(out$density, na.rm = TRUE)
out$count <- out$value
out$ncount <- out$count / max(out$count, na.rm = TRUE)
out$value <- NULL
out
}
)

we set a default weight, but still has to guard it with data$weight %||% rep(1L, nrow(data)) when using it in compute_group()

I cannot see any meaningful reason why this is so, and the fix seems obvious. Have I missed an underlying reason @hadley?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions