Learn R Programming

gghinton

Hinton diagrams for ggplot2.

A Hinton diagram visualises a numerical matrix. Each element is displayed as a square and where the square's area is proportional to the absolute value of the element. Positive values are white, negative values are black, on a grey background. This simple clear perceptual mapping makes interpretation of the structure of the matrix easy.

pak::pkg_install("robin-foster-rf/gghinton")
# install.packages("gghinton")  # once on CRAN

library(gghinton)

m <- matrix(runif(10*10, -1, 1), nrow = 10)

matrix_to_hinton(m) |>
  ggplot(aes(x = col, y = row, weight = weight)) +
  geom_hinton() +
  scale_fill_hinton() +
  coord_fixed() +
  theme_hinton()

Why not a heatmap?

HeatmapHinton diagram
Encodes magnitudevia colour intensityvia square area
Encodes signrequires diverging palettewhite vs black
Works for colourblind readersdepends on paletteyes
Near-zero valuesinvisible colourinvisible square
Works at a glancepartiallyyes

Colour saturation is a notoriously unreliable channel for magnitude judgements. Square area is not: humans compare areas accurately, pre-attentively. When you care about how big and which sign, a Hinton diagram beats a heatmap.

Heatmaps are better when: you have large matrices (> ~50x50, where at typical plotting sizes you would only have a handful of pixels per element), continuous gradients matter more than individual entries, or values are all positive and the magnitude range is narrow.

Installation

#development version:
pak::pkg_install("robin-foster-rf/gghinton")

# install.packages("gghinton")  # from CRAN (once available)

Core functions

FunctionPurpose
geom_hinton()Draw a Hinton diagram as a ggplot2 layer
stat_hinton()The underlying stat_* (for advanced use)
scale_fill_hinton()White/black colour scale for signed data
theme_hinton()Clean theme: removes grid lines
matrix_to_hinton()Convert a matrix to a tidy data frame
as_hinton_df()Generic converter (matrix, data.frame, table)

Key aesthetics

aes(x = col,    # column position
    y = row,    # row position (row 1 of the matrix is at the top)
    weight = w) # the value: determines size and colour

The scale_by parameter

# Default: each facet panel normalised independently
geom_hinton(scale_by = "panel")

# Global: all panels share the same scale, allows cross-panel comparison
geom_hinton(scale_by = "global")

Use cases

  • Correlation matrices: immediately spot large positive and negative correlations
  • Factor loadings: which variables load strongly on which factors
  • Transition matrices: Markov chain structure at a glance
  • PCA weight matrices: understand what a principal component captures

Design

gghinton follows standard ggplot2 extension conventions:

  • GeomHinton extends GeomRect
  • StatHinton computes rectangle bounds in compute_panel so normalization is consistent within each panel
  • Sign detection is automatic (no need to tell the package whether your data is signed)

Copy Link

Version

Install

install.packages('gghinton')

Version

0.1.0

License

MIT + file LICENSE

Issues

Pull Requests

Stars

Forks

Maintainer

Robin Foster

Last Published

April 1st, 2026

Functions in gghinton (0.1.0)

theme_hinton

A clean ggplot2 theme for Hinton diagrams
matrix_to_hinton

Convert a matrix to a tidy data frame for use with geom_hinton()
alice_bigrams

English character bigram counts from Alice's Adventures in Wonderland
as_hinton_df

Convert an object to a tidy data frame for geom_hinton()
gghinton-package

gghinton: Hinton Diagrams for 'ggplot2'
scale_fill_hinton

Colour scale for Hinton diagrams
GeomHinton

Hinton diagrams for ggplot2