stat_summary_address: Summarize IP addresses on a heatmap

View source: R/stat-summary-address.R

stat_summary_addressR Documentation

Summarize IP addresses on a heatmap


Addresses are grouped into networks determined by the pixel_prefix argument of coord_ip(). Then the z values are summarized with summary function fun.


  mapping = NULL,
  data = NULL,
  fun = NULL,
  fun.args = list(),
  na.rm = FALSE,
  show.legend = NA,
  inherit.aes = TRUE



Set of aesthetic mappings created by aes(). If specified and inherit.aes = TRUE (the default), it is combined with the default mapping at the top level of the plot. You must supply mapping if there is no plot mapping.


The data to be displayed in this layer. There are three options:

If NULL, the default, the data is inherited from the plot data as specified in the call to ggplot().

A data.frame, or other object, will override the plot data. All objects will be fortified to produce a data frame. See fortify() for which variables will be created.

A function will be called with a single argument, the plot data. The return value must be a data.frame, and will be used as the layer data. A function can be created from a formula (e.g. ~ head(.x, 10)).


Other arguments passed on to layer(). These are often aesthetics, used to set an aesthetic to a fixed value, like colour = "red" or size = 3. They may also be parameters to the paired geom/stat.


Summary function (see section below for details). If NULL (the default), the observations are simply counted.


A list of extra arguments to pass to fun.


If FALSE, the default, missing values are removed with a warning. If TRUE, missing values are silently removed.


logical. Should this layer be included in the legends? NA, the default, includes if any aesthetics are mapped. FALSE never includes, and TRUE always includes. It can also be a named logical vector to finely select the aesthetics to display.


If FALSE, overrides the default aesthetics, rather than combining with them. This is most useful for helper functions that define both data and aesthetics and shouldn't inherit behaviour from the default plot specification, e.g. borders().


stat_summary_address() understands the following aesthetics (required aesthetics are in bold):

  • ip: An ip_address column

  • z: Value passed to the summary function (required if fun is used)

  • fill: Default is after_stat(value)

  • alpha

Computed variables

The following variables are available to after_stat():

  • value: Value of summary statistic

  • count: Number of observations

Summary function

The data might contain multiple rows per pixel of the heatmap, so a summary function reduces this information to a single value to display. This function receives the data column specified by the z aesthetic and also receives arguments specified by fun.args.

The fun argument can be specified in multiple ways:


If no summary function is provided, the number of observations is computed. In this case, you don't need to specify the z aesthetic, and the computed variables value and count will be equal.


The name of an existing function (e.g. fun = "mean").


Either provide an existing function (e.g. fun = mean) or define a new function (e.g. fun = function(x) sum(x^2)).


A function can also be created from a formula. This uses .x as the summarized variable (e.g. fun = ~ sum(.x^2)).


dat <- data.frame(
  ip = sample_ipv4(10000),
  weight = runif(10000)

p <- ggplot(dat, aes(ip = ip)) +
  coord_ip() +

# simple count of observations
p +
  stat_summary_address() +
  scale_fill_viridis_c(trans = "log2", na.value = "black", guide = "none")

# compute mean weight
p +
  stat_summary_address(aes(z = weight), fun = ~ mean(.x)) +
  scale_fill_viridis_c(na.value = "black", guide = "none")

ggip documentation built on Nov. 10, 2022, 5:08 p.m.