library(rbiom)

pals <- sapply(simplify = FALSE, names(rbiom:::PALETTES), function (i) {
  sprintf("<td align='right' style='padding-left:20px; padding-right:0'>%s</td><td>%s</td>", i, 
    paste0(collapse = '', "<span style='color:#", rbiom:::PALETTES[[i]], "'>&#9608;</span>") )})

knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  R.options = list(
    pillar.print_min = 5,
    pillar.print_max = 5 ))

Colors

Automatic colors

All stat.by needs is the name of the metadata field you'd like to color by. In these cases, rbiom will select an appropriate color-blind safe palette to use.

stat.by = "Sex"

Custom colors

You can map your metadata values to any color you'd like using the syntax in the following examples. You can use hexadecimal codes or any of the 657 named R colors (type colors() for the list).

stat.by = "Sex", colors = c("cyan", "#FFC83D")
stat.by = "Sex", colors = c('Male' = "cyan", 'Female' = "#FFC83D")

Built-in palettes

A collection of color-blind friendly palettes are pre-defined in rbiom. They have been compiled from multiple sources:

# Most plots assign colors from a single categorical metadata field.
stat.by = "Body Site", colors = "bright"

# Heatmap plots allow multiple metadata fields of any type.
color.by = c('Body Site' = "okabe", 'Age' = "bamako")

Discrete palettes

cat(glue::glue_data(pals,
'<table>
<tr>{bright}{tableau}</tr>
<tr>{vibrant}{classic}</tr>
<tr>{okabe}{carto}</tr>
<tr>{r4}{tol}</tr>
<tr>{light}{tableau20}</tr>
<tr>{muted}{kelly}</tr>
<tr>{fishy}{alphabet}</tr>
<tr>{sub("<td>", "<td colspan=3>", polychrome)}</tr>
</table>'))

Continuous palettes

cat(glue::glue_data(pals,
'<table>
<tr>{acton}{devon}{oslo}</tr>
<tr>{bam}{grays}{purples}</tr>
<tr>{bamako}{greens}{reds}</tr>
<tr>{batlow}{hawaii}{roma}</tr>
<tr>{berlin}{imola}{tofino}</tr>
<tr>{bilbao}{lajolla}{tokyo}</tr>
<tr>{broc}{lapaz}{turku}</tr>
<tr>{buda}{lisbon}{vanimo}</tr>
<tr>{cork}{nuuk}{vik}</tr>
<tr>{davos}{oranges}<td></td><td></td></tr>
</table>'))

Reversing a palette

Prefix a palette name with a dash (-) to reverse the color order. This is also helpful for assigning colors to a heatmap's main grid.

color.by = c('Age' = "-bamako")
grid = "-imola"

Patterns

Options are "brick", "chevron", "fish", "grid", "herringbone", "hexagon", "octagon", "rain", "saw", "shingle", "rshingle", "stripe", and "wave".

grid::grid.newpage()

invisible(mapply(
  p = c(
    "brick", "chevron", "fish", "grid", "herringbone", "hexagon", 
    "octagon", "saw", "shingle", "rshingle", "stripe", "wave" ), 
  x = rep(1:4 / 4, times = 3) - 1/8, 
  y = rep(3:1 / 3, each  = 4) - 1/6, 
  function (p, x, y) {
    gp <- grid::gpar(fill = fillpattern::fill_pattern(p))
    grid::grid.rect(x = x, y = y - .03, width = .2, height = .2, gp = gp)
    grid::grid.text(x = x, y = y + .11, label = p)
}))

Pattern names can optionally be suffixed with modifiers to change the line width/style, tile size/rotation, and more.

grid::grid.newpage()

invisible(mapply(
  p = c(
    "brick20", "chevron_2dashed", "fish_dotted", "grid_longdash", 
    "herringbone45", "hexagon_sm", "octagon_:sm", "saw_16mm:8mm", 
    "shingle_3lwd", "rshingle_dotted", "stripe35_lg", "wave45_sm" ), 
  x = rep(1:4 / 4, times = 3) - 1/8, 
  y = rep(3:1 / 3, each  = 4) - 1/6, 
  function (p, x, y) {
    gp <- grid::gpar(fill = fillpattern::fill_pattern(p))
    grid::grid.rect(x = x, y = y - .03, width = .2, height = .2, gp = gp)
    grid::grid.text(x = x, y = y + .11, label = p)
}))

You can allow rbiom to choose a set of patterns, or you can explicitly define the mappings.

stat.by = "Body Site", patterns = TRUE
stat.by = "Sex", patterns = c("brick", "stripe35_lg")
stat.by = "Sex", patterns = c('Male' = "brick20", 'Female' = "shingle")

See fillpattern for more complete documentation of pattern options.

Shapes

Shapes can be given as per base R - numbers 0 through 17 for various shapes, or the decimal value of an ascii character, e.g. 65:90 = A-Z; 97:122 = a-z to use letters instead of shapes on the plot. Text strings may used as well.

# One categorical metadata field may be specified.
stat.by = "Body Site"
stat.by = "Sex", shapes = c("Stool", "Saliva")
stat.by = "Sex", shapes = c('Stool' = 7, 'Saliva' = 8)
ggplot2::ggplot(data.frame(shape = c(0:17, 65, 90, 97, 122))) + 
  ggplot2::geom_point(
    mapping = ggplot2::aes(x = 1, y = 1, shape = shape),
    size    = 8,
    color   = "white" ) + 
  ggplot2::scale_shape_identity() +
  ggplot2::facet_wrap(~shape, nrow = 2) +
  ggplot2::theme_void() +
  ggplot2::theme(
    panel.spacing    = ggplot2::unit(10, "pt"),
    panel.background = ggplot2::element_rect(fill = "#222222", colour = NA),
    strip.background = ggplot2::element_rect(fill = "#222222", colour = NA),
    strip.text       = ggplot2::element_text(
      size   = 11, 
      color  = "white",
      face   = "bold",
      margin = ggplot2::margin(4,0,4,0, "pt") ))

Facets

Faceting allows you to split your dataset into multiple sub-plots by one or more categorical metadata fields. For example:

biom <- rarefy(hmp50)

adiv_boxplot(biom, stat.by = "Body Site", facet.by = "Sex")
adiv_boxplot(biom, stat.by = "Sex", facet.by = "Body Site")

Auto-generated facets

Plotting functions often allow you to specify multiple ranks, taxa, metrics, etc which are automatically assigned their own facet dimension. You can still add your own facets to these plots.

adiv_boxplot(biom, bdiv = c("Bray-Curtis", "Euclidean"))
adiv_boxplot(biom, bdiv = c("Bray-Curtis", "Euclidean"), facet.by = "Sex")


cmmr/rbiom documentation built on April 28, 2024, 6:38 a.m.