Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>Update</strong> With the release of ggplot2 0.9.0, much of this functionality is covered by the new additions of <code>stat_summary2d</code> and <code>stat_summary_bin</code>.</p> <p>here is a gist for this answer: <a href="https://gist.github.com/1341218">https://gist.github.com/1341218</a></p> <p>here is a slight modification of stat_bin2d so as to accept arbitrary function:</p> <pre><code>StatAggr2d &lt;- proto(Stat, { objname &lt;- "aggr2d" default_aes &lt;- function(.) aes(fill = ..value..) required_aes &lt;- c("x", "y", "z") default_geom &lt;- function(.) GeomRect calculate &lt;- function(., data, scales, binwidth = NULL, bins = 30, breaks = NULL, origin = NULL, drop = TRUE, fun = mean, ...) { range &lt;- list( x = scales$x$output_set(), y = scales$y$output_set() ) # Determine binwidth, if omitted if (is.null(binwidth)) { binwidth &lt;- c(NA, NA) if (is.integer(data$x)) { binwidth[1] &lt;- 1 } else { binwidth[1] &lt;- diff(range$x) / bins } if (is.integer(data$y)) { binwidth[2] &lt;- 1 } else { binwidth[2] &lt;- diff(range$y) / bins } } stopifnot(is.numeric(binwidth)) stopifnot(length(binwidth) == 2) # Determine breaks, if omitted if (is.null(breaks)) { if (is.null(origin)) { breaks &lt;- list( fullseq(range$x, binwidth[1]), fullseq(range$y, binwidth[2]) ) } else { breaks &lt;- list( seq(origin[1], max(range$x) + binwidth[1], binwidth[1]), seq(origin[2], max(range$y) + binwidth[2], binwidth[2]) ) } } stopifnot(is.list(breaks)) stopifnot(length(breaks) == 2) stopifnot(all(sapply(breaks, is.numeric))) names(breaks) &lt;- c("x", "y") xbin &lt;- cut(data$x, sort(breaks$x), include.lowest=TRUE) ybin &lt;- cut(data$y, sort(breaks$y), include.lowest=TRUE) if (is.null(data$weight)) data$weight &lt;- 1 ans &lt;- ddply(data.frame(data, xbin, ybin), .(xbin, ybin), function(d) data.frame(value = fun(d$z))) within(ans,{ xint &lt;- as.numeric(xbin) xmin &lt;- breaks$x[xint] xmax &lt;- breaks$x[xint + 1] yint &lt;- as.numeric(ybin) ymin &lt;- breaks$y[yint] ymax &lt;- breaks$y[yint + 1] }) } }) stat_aggr2d &lt;- StatAggr2d$build_accessor() </code></pre> <p>and usage:</p> <pre><code>ggplot(data = testDF,aes(x=x,y=y, z=rts))+stat_aggr2d(bins=3) ggplot(data = testDF,aes(x=x,y=y, z=rts))+ stat_aggr2d(bins=3, fun = function(x) sum(x^2)) </code></pre> <p><img src="https://i.stack.imgur.com/8V3Z0.png" alt="enter image description here"></p> <p>As well, here is a slight modification of stat_binhex:</p> <pre><code>StatAggrhex &lt;- proto(Stat, { objname &lt;- "aggrhex" default_aes &lt;- function(.) aes(fill = ..value..) required_aes &lt;- c("x", "y", "z") default_geom &lt;- function(.) GeomHex calculate &lt;- function(., data, scales, binwidth = NULL, bins = 30, na.rm = FALSE, fun = mean, ...) { try_require("hexbin") data &lt;- remove_missing(data, na.rm, c("x", "y"), name="stat_hexbin") if (is.null(binwidth)) { binwidth &lt;- c( diff(scales$x$input_set()) / bins, diff(scales$y$input_set() ) / bins ) } try_require("hexbin") x &lt;- data$x y &lt;- data$y # Convert binwidths into bounds + nbins xbnds &lt;- c( round_any(min(x), binwidth[1], floor) - 1e-6, round_any(max(x), binwidth[1], ceiling) + 1e-6 ) xbins &lt;- diff(xbnds) / binwidth[1] ybnds &lt;- c( round_any(min(y), binwidth[1], floor) - 1e-6, round_any(max(y), binwidth[2], ceiling) + 1e-6 ) ybins &lt;- diff(ybnds) / binwidth[2] # Call hexbin hb &lt;- hexbin( x, xbnds = xbnds, xbins = xbins, y, ybnds = ybnds, shape = ybins / xbins, IDs = TRUE ) value &lt;- tapply(data$z, hb@cID, fun) # Convert to data frame data.frame(hcell2xy(hb), value) } }) stat_aggrhex &lt;- StatAggrhex$build_accessor() </code></pre> <p>and usage:</p> <pre><code>ggplot(data = testDF,aes(x=x,y=y, z=rts))+stat_aggrhex(bins=3) ggplot(data = testDF,aes(x=x,y=y, z=rts))+ stat_aggrhex(bins=3, fun = function(x) sum(x^2)) </code></pre> <p><img src="https://i.stack.imgur.com/vYgc3.png" alt="enter image description here"></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload