set.seed(1234)
dat = data.frame(x = rnorm(20000), y = rnorm(20000))
# "hexbin"/"hex" 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 = "viridis", 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 = data.frame(
x = as.vector(row(volcano)),
y = as.vector(col(volcano)),
elevation = as.vector(volcano)
)
tinyplot(
y ~ x | elevation, data = volc,
type = "hexbin", xbins = 50,
palette = terrain.colors(100, rev = TRUE)
)
Run the code above in your browser using DataLab