grantmcdermott · GitHub

Closes #666

Examples from the ?type_hexbin docs:

pkgload::load_all("~/Documents/Projects/tinyplot/")
#> ℹ Loading tinyplot
tinytheme("clean2")
set.seed(1234)
dat = data.frame(x = rnorm(20000), y = rnorm(20000))
# "hexbin" type convenience string
tinyplot(y ~ x, data = dat, type = "hexbin")

# Use type_hexbin() to pass extra arguments
tinyplot(y ~ x, data = dat, type = type_hexbin(xbins = 40))

# tinyplot's default palette logic maps darker colours (the end of the
# spectrum) to higher densities. For hexbin plots it can sometimes be more
# visually pleasing to reverse this, which users can do manually by passing a
# reversed palette.
tinyplot(
  y ~ x, data = dat, type = "hexbin",
  palette = hcl.colors(100, palette = "agSunset", rev = TRUE)
)

# Passing a `by` grouping variable will colour cells according to a summary
# of this variable (in each hex cell) instead of density count. The default
# summary function depends on whether `by` is discrete or continuous:
# 1) Discrete grouping variable: each cell is coloured by its mode.
dat$g = cut(dat$x, breaks = c(-Inf, -1, 1, Inf), labels = c("lo", "mid", "hi"))
tinyplot(y ~ x | g, data = dat, type = "hexbin")

# 2) Continuous grouping variable: each cell is coloured by its mean. 
#    Example: Create a long version of the `volcano` dataset, and plot its
#    elevations onto a gridded terrain map.
volc = local({
  v = setNames(stack(as.data.frame(volcano)), c("elevation", "y"))
  v$y = as.numeric(gsub("^V", "", v$y))
  v$x = seq_len(nrow(volcano))
  v
})
tinyplot(
  y ~ x | elevation, data = volc,
  type = "hexbin", xbins = 50,
  palette = terrain.colors(100, rev = TRUE)
)

tinytheme()

Created on 2026-07-28 with reprex v2.1.1

Read the original on github.com ↗