| Title: | Multiple Summary Statistics for Binned Stats/Geometries |
|---|---|
| Description: | Provides the ggplot binning layer stat_summaries_hex(), which functions similar to its singular form, but allows the use of multiple statistics per bin. Those statistics can be mapped to multiple bin aesthetics. |
| Authors: | Philipp Angerer [aut, cre] (ORCID: <https://orcid.org/0000-0002-0369-2888>) |
| Maintainer: | Philipp Angerer <[email protected]> |
| License: | GPL-3 |
| Version: | 1.0.1 |
| Built: | 2026-06-07 06:50:07 UTC |
| Source: | https://github.com/flying-sheep/ggplot.multistats |
The default legend key drawing function for stat_summaries_hex.
This function can be used as key_glyph parameter by any layer.
draw_key_hexagon(data, params, size)draw_key_hexagon(data, params, size)
data |
A single row data frame containing the scaled aesthetics to display in this key |
params |
A list of additional parameters supplied to the geom. |
size |
Width and height of key in mm. |
A hexagonal polygonGrob.
The legend key drawing functions built into ggplot:
draw_key.
library(ggplot2) ggplot(iris, aes(Sepal.Length, Sepal.Width)) + geom_hex(key_glyph = 'hexagon') + guides(fill = 'legend')library(ggplot2) ggplot(iris, aes(Sepal.Length, Sepal.Width)) + geom_hex(key_glyph = 'hexagon') + guides(fill = 'legend')
Takes a list of functions and function names (or a vector of function names) and names it. Requires all entries with functions to be named and adds names to functions that were specified as names.
normalize_function_list(funs)normalize_function_list(funs)
funs |
Valid list or vector of function names and/or functions. |
Named list or character vector of functions.
normalize_function_list(c(value = 'mean')) normalize_function_list(c('median', n = 'length')) normalize_function_list(list('median', n = length)) normalize_function_list(list(Sum = sum, Custom = function(x) sum(nchar(as.character(x)))))normalize_function_list(c(value = 'mean')) normalize_function_list(c('median', n = 'length')) normalize_function_list(list('median', n = length)) normalize_function_list(list(Sum = sum, Custom = function(x) sum(nchar(as.character(x)))))
Very similar to stat_summary_hex, but allows
for multiple stats to be captured using the funs parameter.
stat_summaries_hex( mapping = NULL, data = NULL, geom = "hex", position = "identity", ..., bins = 30, binwidth = NULL, drop = TRUE, funs = c(value = "mean"), na.rm = FALSE, show.legend = NA, inherit.aes = TRUE, key_glyph = NULL ) StatSummariesHexstat_summaries_hex( mapping = NULL, data = NULL, geom = "hex", position = "identity", ..., bins = 30, binwidth = NULL, drop = TRUE, funs = c(value = "mean"), na.rm = FALSE, show.legend = NA, inherit.aes = TRUE, key_glyph = NULL ) StatSummariesHex
mapping |
Set of aesthetic mappings created by |
data |
The data to be displayed in this layer. There are three options: If A A |
geom |
The geometric object to use to display the data, either as a
|
position |
Position adjustment, either as a string naming the adjustment
(e.g. |
... |
Other arguments passed on to |
bins |
numeric vector giving number of bins in both vertical and horizontal directions. Set to 30 by default. |
binwidth |
Numeric vector giving bin width in both vertical and
horizontal directions. Overrides |
drop |
drop if the output of |
funs |
A list or vector of functions and function names.
See |
na.rm |
If |
show.legend |
logical. Should this layer be included in the legends?
|
inherit.aes |
If |
key_glyph |
A legend key drawing function or a string providing
the function name minus the |
An object of class StatSummariesHex (inherits from Stat, ggproto, gg) of length 4.
normalize_function_list for the funs parameter
and draw_key_hexagon for the legend entry.
library(ggplot2) # Define the variable used for the stats using z ggplot_base <- ggplot(iris, aes(Sepal.Width, Sepal.Length, z = Petal.Width)) # The default is creating `after_stat(value)` containing the mean ggplot_base + stat_summaries_hex(aes(fill = after_stat(value)), bins = 5) # but you can specify your own stats ggplot_base + stat_summaries_hex( aes(fill = after_stat(median), alpha = after_stat(n)), funs = c('median', n = 'length'), bins = 5)library(ggplot2) # Define the variable used for the stats using z ggplot_base <- ggplot(iris, aes(Sepal.Width, Sepal.Length, z = Petal.Width)) # The default is creating `after_stat(value)` containing the mean ggplot_base + stat_summaries_hex(aes(fill = after_stat(value)), bins = 5) # but you can specify your own stats ggplot_base + stat_summaries_hex( aes(fill = after_stat(median), alpha = after_stat(n)), funs = c('median', n = 'length'), bins = 5)