The aim of the package `plot.matrix`

is to visualize a matrix as is with a heatmap. Automatic reordering of rows and columns is only done if necessary. This is different as in similar function like `heatmap`

. Additionally it should be user-friendly and give access to a lot of options if necessary.

Currently the package implements the S3 functions below such that you can use the generic `plot`

function to plot matrices as heatmaps:

`plot.matrix`

for a heatmap for a plain matrix,`plot.loadings`

for a heatmap for a loadings matrix from factor analysis or principal component analysis (reordering of rows!).

The plot itself is composed by a heatmap (usually left) where colors represent matrix entries and a key (usually right) which links the colors to the values.

```
library('plot.matrix')
# numeric matrix
x <- matrix(runif(35), ncol=5) # create a numeric matrix object
class(x)
#> [1] "matrix" "array"
par(mar=c(5.1, 4.1, 4.1, 4.1)) # adapt margins
plot(x)
```

```
# logical matrix
m <- matrix(runif(35)<0.5, ncol=7)
plot(m)
```

```
# text matrix
s <- matrix(sample(letters[1:10], 35, replace=TRUE), ncol=5)
plot(s)
```

```
library('plot.matrix')
library('psych')
data <- na.omit(bfi[,1:25])
fa <- fa(data, 5, rotate="varimax")
par(mar=c(5.1, 4.1, 4.1, 4.1)) # adapt margins
plot(loadings(fa), cex=0.5)
```

`plot.matrix`

uses the command `assignColors`

, also part of `plot.matrix`

, assigns to each value in `x`

a color based on the parameters `breaks`

, `col`

and `na.col`

given.

In case of a numeric matrix `breaks`

can be

- a number, giving the number of intervals covering the range of
`x`

, - a vector of two numbers, given the range to cover with 10 intervals, or
- a vector with more than two numbers, specify the interval borders

In case of a non-numeric vector `breaks`

must contain all values which are will get a color. If `breaks`

is not given then a sensible default is chosen: in case of a numeric vector derived from `pretty`

and otherwise all unique values/levels are used.

`col`

can be either be a vector of colors or a function which generates via `col(n)`

a set of `n`

colors. The default is to use `heat.colors`

.

In case that you want to provide your own color palettes/functions for plotting there are several good choices within R packages:

Source: Datanovia - Top R Color Palettes to Know for Great Data Visualization

The plot is created in several steps

- a call to the
`plot`

command to create the basic plot - draw colored polygons for each matrix entry with the
`polygon`

command - if necessary add the value of each matrix entry with the
`text`

command in a polygon - if necessary draw x- and y-axis with the
`axis`

command into the plot - if necessary draw the key with the
`axis`

and the`polygon`

command

```
#> Warning in kable_pipe(x = structure(c("plot.matrix(", "", "", "", "", "", : The
#> table should have a header (column names)
```

plot.matrix( | x, | ||

y | = | NULL, | |

breaks | = | NULL, | |

col | = | heat.colors, | |

na.col | = | “white”, | |

na.cell | = | TRUE, | |

na.print | = | TRUE, | |

digits | = | NA, | |

fmt.cell | = | NULL, | |

fmt.key | = | NULL, | |

polygon.cell | = | NULL, | |

polygon.key | = | NULL, | |

text.cell | = | NULL, | |

key | = | list(side = 4, las = 1), | |

axis.col | = | list(side = 1), | |

axis.row | = | list(side = 2), | |

axis.key | = | NULL, | |

max.col | = | 70, | |

…) |

You may influence the appearance by setting your own parameters:

`...`

all parameters given here will be given to the`plot`

command, e.g.`xlab`

,`ylab`

, ….`polygon.cell`

list of parameters for drawing polygons for matrix entries`text.cell`

list of parameters for putting for matrix entries as texts`axis.col`

and`axis.row`

list of parameters for drawing for row and column axes`key`

,`axis.key`

and`polygon.key`

to draw the key`max.col`

to determine when text color and background color to near

You may set global parameters for all subsequent calls of `axis`

, `polygon`

and `text`

via the `...`

. The following parameters are supported

function | parameter(s) |
---|---|

`axis` |
`cex.axis` , `col.axis` , `col.ticks` , `font` , `font.axis` , `hadj` , `las` , `lwd.ticks` , `line` , `outer` , `padj` , `tck` , `tcl` , `tick` |

`polygon` |
`angle` , `border` , `density` |

`text` |
`cex` , `font` , `vfont` |

```
par(mar=c(5.1, 4.1, 4.1, 4.1)) # adapt margins
# omit all borders
plot(x, border=NA)
```

Use the parameter `border=NA`

.

Use the parameter `asp=TRUE`

.

Use the parameter `las`

, the style of axis labels, in `axis.col`

and/or `axis.row`

with

`0`

= always parallel to the axis [default],`1`

= always horizontal,`2`

= always perpendicular to the axis, and`3`

= always vertical.

```
x <- matrix(runif(35), ncol=5) # create a numeric matrix object
par(mar=c(5.1, 4.1, 4.1, 4.1)) # adapt margins such that all labels are visible
plot(x, axis.col=list(side=1, las=2), axis.row = list(side=2, las=1))
```

The default plot always draws a heatmap and a key where the colors and breaks are determined by the entries of `x`

. In case of a numeric matrix ten colors from `heat.colors`

are chosen and eleven breaks with cover the range of entries with an equidistant grid. In case of a non-numeric matrix each unique element gets a color determined from `heat.colors`

.

In case of a numeric matrix the `breaks`

give the interval borders for a color otherwise for each unique matrix entry `breaks`

should contain a value. If `breaks`

are not given then they will be determined from the matrix object by using the `pretty`

function of base R.

```
par(mar=c(5.1, 4.1, 4.1, 4.1)) # adapt margins
# we only want the range of x
plot(x, breaks=range(x))
```