Learn R Programming

berryFunctions (version 1.11.0)

tableColVal: Table with values with value-dependent colored backgrounds in pdf

Description

Table with numbers and corresponding color in the background of each cell. (heatmap)

Usage

tableColVal(mat, pdffile = "table_col_val.pdf", pdf = !missing(pdffile), nameswidth = 0.3, namesheight = 0.1, palette = seqPal(nrow(mat) * ncol(mat)), Range = range(mat, finite = TRUE), argclass = NULL, argrow = NULL, argcol = NULL, argcell = NULL, argmain = NULL, ...)

Arguments

mat
Matrix with values
pdffile
Name of File to write to. DEFAULT: "table_col_val.pdf"
pdf
Should table be written to pdffile? (Else it will plot in x11). DEFAULT: FALSE
nameswidth
Relative width of row names at the left, as a percentage of plot. DEFAULT: 0.3
namesheight
Relative height of column names at the top. DEFAULT: 0.1
palette
Color palette for the heatmap. DEFAULT: seqPal(nrow(mat)*ncol(mat))
Range
Range of values mapped linearly to color palette. DEFAULT: range(mat,finite
argclass
List of arguments specifying how to call classify, eg. method. DEFAULT: NULL
argrow, argcol, argcell, argmain
List of arguments passed to text in row and column names, cell content and topleft cell, respectively. Could be cex, col, srt, etc. DEFAULTS: NULL
...
Further arguments passed to pdf.

Value

None. PDF or plot produced.

Details

I saw a presentation today with a table of values of differences between several models and datasets of global precipitation. I decided I don't like reading 20+ values, and would like to see a corresponding color in the background of each cell. (heatmap) Writing this function took me about 1 hour and 30 minutes and was a nice coding excercise. Feedback welcome at berry-b@gmx.de!

See Also

pdf, heatmap

Examples

Run this code

Bsp <- matrix(c(21,23,26,27, 18,24,25,28, 14,17,23,23, 16,19,21,25), ncol=4, byrow=TRUE)
colnames(Bsp) <- paste0("Measure", LETTERS[1:4])
rownames(Bsp) <- paste("prod", 8:11, sep="_")
Bsp

tableColVal(Bsp)
tableColVal(Bsp, nameswidth=0.1) # relative to plot width
tableColVal(Bsp, namesheight=0.5, argcol=list(srt=90))

tableColVal(Bsp, argrow=list(col="red", cex=2) )
tableColVal(Bsp, Range=c(10,40))
tableColVal(Bsp, Range=c(20,40))
tableColVal(Bsp, palette=heat.colors(12))
tableColVal(Bsp, palette=c(2,4,7), argmain=list(labels="last\ncomparison"))

## Not run: 
# ## Rcmd check --as-cran doesn't like to open external devices such as pdf,
# ## so this example is excluded from running in the checks.
# tableColVal(Bsp, pdf=TRUE, width=12)  # further arguments to pdf possible.
# 
# Bsp2 <- matrix(sample(1:100, 30), ncol=6, byrow=TRUE)
# graphics.off(); X11(height=4)
# tableColVal(Bsp2)
# ## End(Not run)

Run the code above in your browser using DataLab