Package 'ggtilematrix'

Title: Create customisable ggplot2 matrix
Description: More about what it does (maybe more than one line) Use four spaces when indenting paragraphs within the Description.
Authors: Cynthia Huang [aut, cre]
Maintainer: Cynthia Huang <[email protected]>
License: GPL (>= 3)
Version: 0.0.0.9000
Built: 2024-11-10 03:59:46 UTC
Source: https://github.com/cynthiahqy/ggtilematrix

Help Index


Plot a matrix as a ggplot2 object

Description

Plot a matrix as a ggplot2 object

Usage

gg_tilematrix.matrix(
  .matrix,
  .geom = list(geom_tile(color = "pink", fill = "blue"), geom_text()),
  .scale_coord = list(scale_y_discrete(limits = base::rev), scale_x_discrete(position =
    "top"), coord_fixed()),
  .theme = list(theme_bw()),
  .layers = list(labs(x = NULL, y = NULL))
)

Arguments

.matrix

A matrix to plot

.layers

Examples

library(ggplot2)
matrices$unnamed |>
  gg_tilematrix.matrix()

Pivots a matrix into (bi-graph) triples

Description

Converts a named (adjacency) matrix into (graph) triples, dropping any NA cells by default.

Usage

matrix_to_triples(
  matrix,
  x_names_to = "x_name",
  y_names_to = "y_name",
  values_to = "value",
  drop_na = TRUE
)

triples_from_matrix(
  matrix,
  x_names_to = "x_name",
  y_names_to = "y_name",
  values_to = "value",
  drop_na = TRUE
)

Arguments

matrix

A matrix to pivot

x_names_to

A string specifying the new column for the names of the first/x-dimension of matrix

y_names_to

A string specifying the new column for the names of the second/y-dimension matrix

values_to

A character vector specifying the new column to create from the cell values of matrix

drop_na

Boolean option to drop x-y pairs with missing values.

Details

Uses pivot_longer and reverses triples_to_matrix(). Setting drop_na=FALSE retain the complete set of graph edges. If matrix is unnamed, default names are generated by base::as.data.frame()

Examples

## named matrices
blx_mtx <- triples$blx_tbl |>
  triples_to_matrix(from, to, weighted)
return_triples <- blx_mtx |>
  triples_from_matrix("from", "to", "weighted")

identical(triples$blx_tbl, return_triples)

## Setting `drop_na=FALSE` retains the `x`-`y` pairs with missing values,
return_with_na <- blx_mtx |>
  triples_from_matrix("from", "to", "weighted", drop_na = FALSE)

## which is equivalent to using `tidyr::complete()` on the original triples
complete_triples <- triples$blx_tbl |>
  tidyr::complete(from, to)

identical(return_with_na, complete_triples)

## names are generated for unnamed matrices
matrices$unnamed |>
  matrix_to_triples(drop_na = FALSE)

Pivot (bi-graph) triples into (bi-adjacency) matrix

Description

Converts a data frame of triples (x_names, y_names, values) into a matrix with x_names and y_names respectively for row and column names and cell values from values.

Usage

triples_to_matrix(
  triples,
  x_names_from = x_name,
  y_names_from = y_name,
  values_from = value,
  values_fill = NA
)

matrix_from_tripes(
  triples,
  x_names_from = x_name,
  y_names_from = y_name,
  values_from = value,
  values_fill = NA
)

Arguments

triples

A data frame to pivot. Unused columns are dropped.

x_names_from

column to get matrix row row names from.

y_names_from

column to get matrix column names from.

values_from

column to get cell values from.

values_fill

Optionally, a (scalar) value that specifies what each value should be filled in with when missing.

This can be a named list if you want to apply different fill values to different value columns.

Details

The triples define graph edges between x_names and y_names with attribute value. x_names and y_names are treated as disjoint nodes sets of the bi-partite graph, and the resultant matrix corresponds to the bi-adjacency matrix.

Uses pivot_wider and makes explicit all possible pairs of x_names and y_names.

Examples

triples$blx_tbl

triples$blx_tbl |>
  triples_to_matrix(from, to, weighted)