Learn R Programming

gghinton (version 0.1.0)

GeomHinton: Hinton diagrams for ggplot2

Description

geom_hinton() draws a Hinton diagram: a grid of squares whose area is proportional to the magnitude of each value. For signed data, positive values are shown as white squares and negative values as black squares on a grey background. For unsigned (non-negative) data the background is omitted and squares are drawn in black.

Usage

geom_hinton(
  mapping = NULL,
  data = NULL,
  stat = "hinton",
  position = "identity",
  ...,
  scale_by = c("panel", "global"),
  background = TRUE,
  na.rm = FALSE,
  show.legend = NA,
  inherit.aes = TRUE
)

stat_hinton( mapping = NULL, data = NULL, geom = "hinton", position = "identity", ..., scale_by = c("panel", "global"), na.rm = FALSE, show.legend = NA, inherit.aes = TRUE )

Value

A ggplot2 layer that can be added to a ggplot2::ggplot() object.

Arguments

mapping

Set of aesthetic mappings created by ggplot2::aes().

data

The data to be displayed in this layer.

stat

The statistical transformation to use. For geom_hinton() the default is "hinton".

position

Position adjustment.

...

Other arguments passed on to ggplot2::layer().

scale_by

"panel" (default) normalises each panel independently so the largest value in a panel fills its cell. "global" uses the largest value across all panels, enabling cross-panel magnitude comparison.

background

Logical. Draw a grey background rectangle for signed data? Default TRUE. Set to FALSE to suppress the background.

na.rm

If TRUE, rows where weight is NA are silently dropped before computing square sizes. If FALSE (default), they are dropped without a warning (ggplot2 will not render rectangles whose required aesthetics are NA).

show.legend

Logical. Should this layer be included in the legend?

inherit.aes

If FALSE, overrides the default aesthetics rather than combining with them.

geom

The geometric object to use when drawing. For stat_hinton() the default is "hinton".

Aesthetics

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

  • x: column position (numeric or factor)

  • y: row position (numeric or factor)

  • weight: the value to display; determines square size and colour

  • alpha

  • colour (border colour; NA by default, no border)

  • fill (overrides the automatic sign-based colour)

  • linewidth

  • linetype

Computed variables

stat_hinton() adds the following columns to the data:

xmin, xmax, ymin, ymax

Rectangle bounds for each square.

fill

"positive", "negative", or "unsigned".

hinton_signed

Logical; TRUE when the panel contains any negative values. Read by GeomHinton to decide whether to draw the grey background (after scale_fill_hinton() has already replaced the fill labels, making fill == "negative" checks unreliable).

cell_w, cell_h

Inferred cell spacing used to size the background.

Aspect ratio

For squares to appear as squares on screen, add coord_fixed() to your plot. Without it, the cells may appear rectangular if the plot's x and y axes have different scales.

Examples

Run this code
library(ggplot2)
m <- matrix(c(0.8, -0.3, 0.5, -0.9, 0.1, 0.6, 0.4, -0.7, 0.2), 3, 3)
df <- matrix_to_hinton(m)

ggplot(df, aes(x = col, y = row, weight = weight)) +
  geom_hinton() +
  scale_fill_hinton() +
  coord_fixed() +
  theme_hinton()

Run the code above in your browser using DataLab