Skip to contents

This stat computes Bayesian surprise for sf geometries, allowing you to visualize surprise directly in ggplot2.

Usage

stat_surprise(
  mapping = NULL,
  data = NULL,
  geom = "sf",
  position = "identity",
  na.rm = FALSE,
  show.legend = NA,
  inherit.aes = TRUE,
  models = c("uniform", "baserate", "funnel"),
  signed = TRUE,
  ...
)

Arguments

mapping

Aesthetic mapping created by ggplot2::aes(). Required aesthetics are geometry (from sf) and observed. Optional aesthetics include expected and sample_size.

data

Data (typically an sf object)

geom

Geometry to use (default: "sf")

position

Position adjustment

na.rm

Remove NA values?

show.legend

Show legend?

inherit.aes

Inherit aesthetics from ggplot?

models

Character vector of model types to use. Options: "uniform", "baserate", "gaussian", "sampled", "funnel"

signed

Logical; compute signed surprise?

...

Additional arguments passed to the layer

Value

A ggplot2 layer

Computed Variables

surprise

Bayesian surprise (KL-divergence)

signed_surprise

Signed surprise (if signed = TRUE)

Examples

library(ggplot2)
library(sf)

nc <- st_read(system.file("shape/nc.shp", package = "sf"), quiet = TRUE)

# Basic surprise map - geometry must be mapped explicitly
ggplot(nc) +
  stat_surprise(aes(geometry = geometry, observed = SID74, expected = BIR74)) +
  scale_fill_surprise()