Skip to content

Commit 3f7727d

Browse files
committed
stat-summary-2d/hex: minor update (handling NA, change the origin for 2d).
1 parent 5327774 commit 3f7727d

File tree

2 files changed

+35
-22
lines changed

2 files changed

+35
-22
lines changed

R/stat-summary-2d.r

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@
99
#' # Specifying function
1010
#' d + stat_summary2d(fun = function(x) sum(x^2))
1111
#' d + stat_summary2d(fun = var)
12-
stat_summary2d <- function (mapping = NULL, data = NULL, geom = "rect", position = "identity",
12+
stat_summary2d <- function (mapping = NULL, data = NULL, geom = NULL, position = "identity",
1313
bins = 30, drop = TRUE, fun = mean, ...) {
14+
1415
StatSummary2d$new(mapping = mapping, data = data, geom = geom, position = position,
1516
bins = bins, drop = drop, fun = fun, ...)
1617
}
@@ -24,11 +25,24 @@ StatSummary2d <- proto(Stat, {
2425

2526
calculate <- function(., data, scales, binwidth = NULL, bins = 30, breaks = NULL, origin = NULL, drop = TRUE, fun = mean, ...) {
2627

28+
data <- remove_missing(data, FALSE, c("x", "y", "z"), name="stat_summary2d")
29+
2730
range <- list(
2831
x = scale_dimension(scales$x, c(0, 0)),
2932
y = scale_dimension(scales$y, c(0, 0))
3033
)
3134

35+
# Determine origin, if omitted
36+
if (is.null(origin)) {
37+
origin <- c(NA, NA)
38+
} else {
39+
stopifnot(is.numeric(origin))
40+
stopifnot(length(origin) == 2)
41+
}
42+
originf <- function(x) if (is.integer(x)) -0.5 else min(x)
43+
if (is.na(origin[1])) origin[1] <- originf(data$x)
44+
if (is.na(origin[2])) origin[2] <- originf(data$y)
45+
3246
# Determine binwidth, if omitted
3347
if (is.null(binwidth)) {
3448
binwidth <- c(NA, NA)
@@ -48,29 +62,25 @@ StatSummary2d <- proto(Stat, {
4862

4963
# Determine breaks, if omitted
5064
if (is.null(breaks)) {
51-
if (is.null(origin)) {
52-
breaks <- list(
53-
fullseq(range$x, binwidth[1]),
54-
fullseq(range$y, binwidth[2])
55-
)
56-
} else {
57-
breaks <- list(
58-
seq(origin[1], max(range$x) + binwidth[1], binwidth[1]),
59-
seq(origin[2], max(range$y) + binwidth[2], binwidth[2])
60-
)
61-
}
65+
breaks <- list(
66+
seq(origin[1], max(range$x) + binwidth[1], binwidth[1]),
67+
seq(origin[2], max(range$y) + binwidth[2], binwidth[2])
68+
)
69+
} else {
70+
stopifnot(is.list(breaks))
71+
stopifnot(length(breaks) == 2)
72+
stopifnot(all(sapply(breaks, is.numeric)))
6273
}
63-
stopifnot(is.list(breaks))
64-
stopifnot(length(breaks) == 2)
65-
stopifnot(all(sapply(breaks, is.numeric)))
6674
names(breaks) <- c("x", "y")
6775

6876
xbin <- cut(data$x, sort(breaks$x), include.lowest=TRUE)
6977
ybin <- cut(data$y, sort(breaks$y), include.lowest=TRUE)
7078

7179
if (is.null(data$weight)) data$weight <- 1
72-
ans <- ddply(data.frame(data, xbin, ybin), .(xbin, ybin), function(d) data.frame(value = fun(d$z, ...)))
7380

81+
ans <- ddply(data.frame(data, xbin, ybin), .(xbin, ybin), function(d) data.frame(value = fun(d$z, ...)))
82+
if (drop) ans <- na.omit(ans)
83+
7484
within(ans,{
7585
xint <- as.numeric(xbin)
7686
xmin <- breaks$x[xint]

R/stat-summary-hex.r

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@
1010
#' d + stat_summary_hex(fun = function(x) sum(x^2))
1111
#' d + stat_summary_hex(fun = var, na.rm = T)
1212
stat_summary_hex <- function (mapping = NULL, data = NULL, geom = "hex", position = "identity",
13-
bins = 30, na.rm = FALSE, fun = mean, ...) {
13+
bins = 30, drop = TRUE, fun = mean, ...) {
14+
1415
StatSummaryhex$new(mapping = mapping, data = data, geom = geom, position = position,
15-
bins = bins, na.rm = na.rm, fun = fun, ...)
16+
bins = bins, drop = drop, fun = fun, ...)
1617
}
1718

1819
StatSummaryhex <- proto(Stat, {
@@ -22,9 +23,9 @@ StatSummaryhex <- proto(Stat, {
2223
required_aes <- c("x", "y", "z")
2324
default_geom <- function(.) GeomHex
2425

25-
calculate <- function(., data, scales, binwidth = NULL, bins = 30, na.rm = FALSE, fun = mean, ...) {
26+
calculate <- function(., data, scales, binwidth = NULL, bins = 30, drop = TRUE, fun = mean, ...) {
2627
try_require("hexbin")
27-
data <- remove_missing(data, na.rm, c("x", "y"), name="stat_summaryhex")
28+
data <- remove_missing(data, FALSE, c("x", "y", "z"), name="stat_summary_hex")
2829

2930
if (is.null(binwidth)) {
3031
binwidth <- c(
@@ -59,8 +60,10 @@ StatSummaryhex <- proto(Stat, {
5960
)
6061

6162
value <- tapply(data$z, hb@cID, fun, ...)
62-
63+
6364
# Convert to data frame
64-
data.frame(hcell2xy(hb), value)
65+
ret <- data.frame(hcell2xy(hb), value)
66+
if (drop) ret <- na.omit(ret)
67+
ret
6568
}
6669
})

0 commit comments

Comments
 (0)