f9b388c stat-summary-2d/hex: update documents. kohske authored Dec 20, 2011 1 ##' Apply function for 2D hexagonal bins. 2 ##' 3 ##' \code{stat_summary2d} is hexagonal version of \code{\link{stat_summary}}. The data are devided by \code{x} and \code{y}. 4 ##' \code{z} in each cell is passed to arbitral summary function. 5 ##' 6 ##' \code{stat_summary-hex} requires the following aesthetics: 7 ##' 8 ##' \itemize{ 9 ##' \item \code{x}: horizontal position 10 ##' \item \code{y}: vertical position 11 ##' \item \code{z}: value passed to the summary function 12 ##' } 13 ##' 14 ##' @seealso \code{\link{stat_summary2d}} for rectangular summarization. \code{\link{stat_bin2d}} for the hexagon-ing options. 15 ##' @title Apply funciton for 2D hexagonal bins. 35c2a79 Inherit stat parameters hadley authored Dec 28, 2011 16 ##' @inheritParams stat_identity f9b388c stat-summary-2d/hex: update documents. kohske authored Dec 20, 2011 17 ##' @param bins see \code{\link{stat_binhex}} 18 ##' @param drop drop if the output of \code{fun} is \code{NA}. 19 ##' @param fun function for summary. 20 ##' @param ... parameters passed to \code{fun} 21 ##' @export 22 ##' @examples 23 ##' d <- ggplot(diamonds, aes(carat, depth, z = price)) 24 ##' d + stat_summary_hex() 25 ##' 26 ##' # Specifying function 27 ##' d + stat_summary_hex(fun = function(x) sum(x^2)) 16c0e55 More example fixes hadley authored Dec 29, 2011 28 ##' d + stat_summary_hex(fun = var, na.rm = TRUE) f81c948 2d-summary: change the function names and pass ... to fun kohske authored Dec 9, 2011 29 stat_summary_hex <- function (mapping = NULL, data = NULL, geom = "hex", position = "identity", 3f7727d stat-summary-2d/hex: minor update (handling NA, change the origin for… kohske authored Dec 12, 2011 30 bins = 30, drop = TRUE, fun = mean, ...) { 31 f81c948 2d-summary: change the function names and pass ... to fun kohske authored Dec 8, 2011 32 StatSummaryhex$new(mapping = mapping, data = data, geom = geom, position = position, 3f7727d stat-summary-2d/hex: minor update (handling NA, change the origin for… kohske authored Dec 12, 2011 33 bins = bins, drop = drop, fun = fun, ...) 89fff49 stat-aggr2d and stat-aggrhex: implementation of 2d aggregation. kohske authored Nov 5, 2011 34 } 35 f81c948 2d-summary: change the function names and pass ... to fun kohske authored Dec 8, 2011 36 StatSummaryhex <- proto(Stat, { 37 objname <- "summaryhex" 89fff49 stat-aggr2d and stat-aggrhex: implementation of 2d aggregation. kohske authored Nov 5, 2011 38 39 default_aes <- function(.) aes(fill = ..value..) 40 required_aes <- c("x", "y", "z") 41 default_geom <- function(.) GeomHex 42 3f7727d stat-summary-2d/hex: minor update (handling NA, change the origin for… kohske authored Dec 12, 2011 43 calculate <- function(., data, scales, binwidth = NULL, bins = 30, drop = TRUE, fun = mean, ...) { 89fff49 stat-aggr2d and stat-aggrhex: implementation of 2d aggregation. kohske authored Nov 5, 2011 44 try_require("hexbin") 3f7727d stat-summary-2d/hex: minor update (handling NA, change the origin for… kohske authored Dec 12, 2011 45 data <- remove_missing(data, FALSE, c("x", "y", "z"), name="stat_summary_hex") 89fff49 stat-aggr2d and stat-aggrhex: implementation of 2d aggregation. kohske authored Nov 5, 2011 46 47 if (is.null(binwidth)) { 48 binwidth <- c( 49 diff(scale_dimension(scales$x, c(0, 0))) / bins, 50 diff(scale_dimension(scales$y, c(0, 0))) / bins 51 ) 52 } 53 54 try_require("hexbin") 55 56 # Convert binwidths into bounds + nbins 57 x <- data$x 58 y <- data$y 59 60 xbnds <- c( 61 round_any(min(x), binwidth[1], floor) - 1e-6, 62 round_any(max(x), binwidth[1], ceiling) + 1e-6 63 ) 64 xbins <- diff(xbnds) / binwidth[1] 65 66 ybnds <- c( 67 round_any(min(y), binwidth[1], floor) - 1e-6, 68 round_any(max(y), binwidth[2], ceiling) + 1e-6 69 ) 70 ybins <- diff(ybnds) / binwidth[2] 71 72 # Call hexbin 73 hb <- hexbin( 74 x, xbnds = xbnds, xbins = xbins, 75 y, ybnds = ybnds, shape = ybins / xbins, 76 IDs = TRUE 77 ) 78 f81c948 2d-summary: change the function names and pass ... to fun kohske authored Dec 8, 2011 79 value <- tapply(data$z, hb@cID, fun, ...) 3f7727d stat-summary-2d/hex: minor update (handling NA, change the origin for… kohske authored Dec 12, 2011 80 89fff49 stat-aggr2d and stat-aggrhex: implementation of 2d aggregation. kohske authored Nov 5, 2011 81 # Convert to data frame 3f7727d stat-summary-2d/hex: minor update (handling NA, change the origin for… kohske authored Dec 12, 2011 82 ret <- data.frame(hcell2xy(hb), value) 83 if (drop) ret <- na.omit(ret) 84 ret 89fff49 stat-aggr2d and stat-aggrhex: implementation of 2d aggregation. kohske authored Nov 5, 2011 85 } 86 })