我爲此寫了一個新的Stat函數。
需要nbins
,bin_var
,bin_fun
和summary_fun
作爲參數,默認爲全部四個。
nbins
的默認值取決於數據點的數量。
bin_var
的默認值是「x」。您也可以將其設置爲「y」。這指定了送入bin_fun
的變量。
bin_fun
是分箱功能。默認情況下,它是我爲此目的編寫的seq_cut
。您也可以編寫自己的分箱功能。它只需要將數據和nbins作爲參數。
summary_fun
是用於聚合箱的彙總功能。默認情況下,它是mean
。您還可以分別使用fun.x
和fun.y
指定x和y的聚合函數。
- 如果您使用以
ymin
和ymax
作爲美學的幾何,您還可以指定fun.ymin
和fun.ymax
。
請注意,如果您指定aes(group = your_bins),則bin_fun
將被忽略,而是使用分組變量。另請注意,它將創建一個可以作爲..count..
訪問的計數變量。在這種情況下
p <- ggplot(data, aes(x, y)) +
geom_point(aes(size = ..count..), stat = "binner") +
ylim(0, 1)
不是很有用(儘管這表明同方差和方差約爲0.25作爲應景伯爾尼的假設(0.5)個變量):
在你的情況,你可以使用它像這樣但僅僅是爲了例如:
p + geom_linerange(stat = "binner",
fun.ymin = function(y) mean(y) - var(y)/2,
fun.ymax = function(y) mean(y) + var(y)/2)
代碼:
library(proto)
stat_binner <- function (mapping = NULL, data = NULL, geom = "point", position = "identity", ...) {
StatBinner$new(mapping = mapping, data = data, geom = geom, position = position, ...)
}
StatBinner <- proto(ggplot2:::Stat, {
objname <- "binner"
default_geom <- function(.) GeomPoint
required_aes <- c("x", "y")
calculate_groups <- function(., data, scales, bin_var = "x", nbins = NULL, bin_fun = seq_cut, summary_fun = mean,
fun.data = NULL, fun.y = NULL, fun.ymax = NULL, fun.ymin = NULL,
fun.x = NULL, fun.xmax = NULL, fun.xmin = NULL, na.rm = FALSE, ...) {
data <- remove_missing(data, na.rm, c("x", "y"), name = "stat_binner")
# Same rules as binnedplot in arm package
n <- nrow(data)
if (is.null(nbins)) {
nbins <- if (n >= 100) floor(sqrt(n))
else if (n > 10 & n < 100) 10
else floor(n/2)
}
if (length(unique(data$group)) == 1) {
data$group <- bin_fun(data[[bin_var]], nbins)
}
if (!missing(fun.data)) {
# User supplied function that takes complete data frame as input
fun.data <- match.fun(fun.data)
fun <- function(df, ...) {
fun.data(df$y, ...)
}
} else {
if (!is.null(summary_fun)) {
if (!is.null(fun.x)) message("fun.x overriden by summary_fun")
if (!is.null(fun.y)) message("fun.y overriden by summary_fun")
fun.x <- fun.y <- summary_fun
}
# User supplied individual vector functions
fs_x <- compact(list(xmin = fun.x, x = fun.x, xmax = fun.xmax))
fs_y <- compact(list(ymin = fun.ymin, y = fun.y, ymax = fun.ymax))
fun <- function(df, ...) {
res_x <- llply(fs_x, function(f) do.call(f, list(df$x, ...)))
res_y <- llply(fs_y, function(f) do.call(f, list(df$y, ...)))
names(res_y) <- names(fs_y)
names(res_x) <- names(fs_x)
as.data.frame(c(res_y, res_x))
}
}
summarise_by_x_and_y(data, fun, ...)
}
})
summarise_by_x_and_y <- function(data, summary, ...) {
summary <- ddply(data, "group", summary, ...)
count <- ddply(data, "group", summarize, count = length(y))
unique <- ddply(data, "group", ggplot2:::uniquecols)
unique$y <- NULL
unique$x <- NULL
res <- merge(merge(summary, unique, by = "group"), count, by = "group")
# Necessary for, eg, colour aesthetics
other_cols <- setdiff(names(data), c(names(summary), names(unique)))
if (length(other_cols) > 0) {
other <- ddply(data[, c(other_cols, "group")], "group", numcolwise(mean))
res <- merge(res, other, by = "group")
}
res
}
seq_cut <- function(x, nbins) {
bins <- seq(min(x), max(x), length.out = nbins)
findInterval(x, bins, rightmost.closed = TRUE)
}
在ggplot2中沒有直接的方法。你的代碼看起來很簡單。 – kohske 2012-04-09 12:06:19