Description
Type functions for tile plots, i.e. a grid of rectangles whose fill colour encodes a third variable. type_tile() is the default building block for these gridded shapes, drawing the values exactly as supplied. It underpins heatmaps, correlation matrices, calendar plots, confusion matrices, and similar displays.
type_heatmap() is a specialised case that first rescales the fill values within each category of one axis. Reach for it when those values are not already on a common scale.
Details
Tile plots are specified as z ~ x with the fill variable passed as the by grouping, i.e. tinyplot(y ~ x | z, type = “tile”). The x and y variables may be factors, characters, or numerics; the by variable supplies the fill and will typically be numeric, yielding a continuous colour gradient and colourbar legend. Omitting by leaves the tiles unfilled, since there is nothing for the fill to encode; pass an explicit fill (or bg) if you want a uniform colour in that case.
Unlike the closely-related type_rect, which requires explicit xmin/xmax/ymin/ymax bounds, type_tile() derives the tile bounds for you: each tile is centred on its x/y position and extends width/2 and height/2 in each direction. Categorical axes are converted to consecutive integer positions and the axis tick labels are taken from the factor levels automatically.
Explicit bounds still take precedence. Passing any of xmin, xmax, ymin, or ymax leaves that dimension untouched, which is useful for irregular or unequal-width tiles (e.g. binned continuous data). Bounds may be given for one axis while the other is derived.
Note that tiles are opaque and drawn edge-to-edge, so the default axis padding and grid lines of most themes are redundant (and the grid is hidden behind the tiles in any case). We therefore ship a dedicated “heatmap” theme that removes the padding and grid, rotates the tick labels, and switches to a sequential palette. See tinytheme() and the Examples.
type_heatmap()’s scale argument is the analogue of the scale argument in base R’s heatmap, and like the latter it z-scores along the chosen margin by default. Pass method = “rescale” to map each group onto the unit [0, 1] interval instead.
type_heatmap() also reverses the y-axis by default, so that the first row sits at the top, matching how one reads a matrix (and again cf. base R’s heatmap() and image). Pass an explicit ylim to override. type_tile() makes no such adjustment, since it draws the values exactly as supplied.
Either way, note that scaling along a margin necessarily discards the relative spread of each group: a narrow-range column will occupy as much of the colour ramp as a wide-range one, since both are divided by their own measure of spread. That is the price of making a matrix of incomparable units legible; use scale = “none” (or type_tile()) when preserving cross-group magnitudes matters more.