# It is recommended to use the dedicated "heatmap" theme for tile plots
tinytheme("heatmap")
#
## type_tile ----
# Correlation matrix of the base `attitude` dataset in "long" form.
catt = as.data.frame(as.table(cor(attitude)), responseName = "Correlation")
tinyplot(Var1 ~ Var2 | Correlation, data = catt, type = "tile")
# fancier version where we reverse the y-axis (to mimic the usual correlation
# matrix layout), add white borders around each tile, and suppress the legend
# but layer on the values as text
tinyplot(
Var1 ~ Var2 | Correlation, data = catt,
type = "tile",
col = "white",
legend = FALSE,
main = "Correlation matrix of base attitude dataset",
xlab = NA, ylab = NA,
ylim = "rev"
)
tinyplot_add(type = "text", labels = round(catt$Correlation, 2))
# Pass scaled tile widths and heights through type_tile() for a gridded look
tinyplot(
Var1 ~ Var2 | Correlation, data = catt,
type = type_tile(width = 0.9, height = 0.9)
)
# It doesn't really work for this example, but you can easily switch to a
# diverging palettes if it makes sense for your data
tinyplot(
Var1 ~ Var2 | Correlation, data = catt,
type = type_tile(width = 0.9, height = 0.9),
palette = "tropic"
)
# Numeric axes work too, e.g. a (reshaped long) data.frame of volcano heights
volc = data.frame(
x = as.vector(row(volcano)),
y = as.vector(col(volcano)),
elevation = as.vector(volcano)
)
tinyplot(
y ~ x | elevation, data = volc,
type = "tile",
theme = "void", # void theme looks better with this numeric example
xlab = NA, ylab = NA,
main = "Maunga Whau volcano"
)
#
## type_heatmap ----
# Raw data matrices are usually dominated by their largest-magnitude column.
# `type_heatmap()` can rescale within each column to make the rest legible.
mt = as.data.frame(as.table(as.matrix(mtcars)))
# first, the unscaled version: only `disp` and `hp` are visible
tinyplot(
Var1 ~ Var2 | Freq, data = mt,
type = "heatmap",
xlab = NA, ylab = NA
)
# and now scaled within each x variable (i.e., column). The default is to
# z-score, matching base R's `heatmap(scale = "column")`.
tinyplot(
Var1 ~ Var2 | Freq, data = mt,
type = type_heatmap(scale = "x"),
xlab = NA, ylab = NA
)
# `method = "rescale"` maps each column onto [0, 1] instead. This uses the
# colour ramp more fully, at the cost of pinning every column's min and max to
# the same two colours.
tinyplot(
Var1 ~ Var2 | Freq, data = mt,
type = type_heatmap(scale = "x", method = "rescale"),
xlab = NA, ylab = NA
)
#
## tips ----
# tip 1: use tinyplot.matrix() directly to avoid reshaping
tinyplot(as.matrix(mtcars), type = type_heatmap(scale = "x"), col = "white")
# tip 2: use per-axis tick label scaling (cex) for dense heatmaps
tinyplot(as.matrix(mtcars), type = type_heatmap(scale = "x"), col = "white",
theme = list("heatmap", cex.yaxs = 0.75, cex.xaxs = 1.25))
## restore the default theme
tinytheme()
Run the code above in your browser using DataLab