axis_combmatrix: Convert delimited text labels into a combination matrix axis

View source: R/axis_combmatrix.R

axis_combmatrixR Documentation

Convert delimited text labels into a combination matrix axis

Description

The function splits the text based on the sep argument and views each occurring element as potential set.

Usage

axis_combmatrix(
  sep = "[^[:alnum:]]+",
  levels = NULL,
  override_plotting_function = NULL,
  xlim = NULL,
  ylim = NULL,
  expand = TRUE,
  clip = "on",
  ytrans = "identity"
)

Arguments

sep

The separator that is used to split the string labels. Can be a regex. Default: "[^[:alnum:]]+"

levels

The selection of string elements that are displayed in the combination matrix axis. Default: NULL, which means simply all elements in the text labels are used

override_plotting_function

to achieve maximum flexibility, you can provide a custom plotting function. For more information, see details. Default: NULL

xlim, ylim

The limits fort the x and y axes

expand

Boolean with the same effect as in ggplot2::coord_cartesian(). Default: TRUE

clip

String with the same effect as in ggplot2::coord_cartesian(). Default: "on"

ytrans

transformers for y axis. For more information see ggplot2::coord_trans(). Default: "identity"

Details

Technically the function appends a coord system to the ggplot object. To maintain compatibility additional arguments like ytrans, ylim, and clip are forwarded to coord_trans().

Note: make sure that the argument to the 'x' aesthetic is character vector that contains the sep sequence. The only exception is if axis_combmatrix() is combined with a scale_x_mergelist(). This pattern works because in the first step scale_x_mergelist() turns a list argument to 'x' into a character vector that axis_combmatrix() can work with.

For maximum flexibility, you can use the 'override_plotting_function' parameter which returns a ggplot and is called with a tibble with one entry per point of the combination matrix. Specifically, it contains

labels

the collapsed label string

single_label

an ordered factor with the labels on the left of the plot

id

consecutive numbering of the points

labels_split

a list column that contains the splitted labels

at

the x-position of the point

observed

boolean to indicate if this element is active in the intersection

index

the row of the point

See the examples how the override_plotting_function looks that recreates the default combination matrix

Examples

  library(ggplot2)
  mtcars$combined <- paste0("Cyl: ", mtcars$cyl, "_Gears: ", mtcars$gear)
  head(mtcars)
  ggplot(mtcars, aes(x=combined)) +
    geom_bar() +
    axis_combmatrix(sep = "_")

# Example of 'override_plotting_function'

ggplot(mtcars, aes(x=combined)) +
  geom_bar() +
    axis_combmatrix(sep = "_", override_plotting_function = function(df){
      ggplot(df, aes(x= at, y= single_label)) +
        geom_rect(aes(fill= index %% 2 == 0), ymin=df$index-0.5,
                  ymax=df$index+0.5, xmin=0, xmax=1) +
        geom_point(aes(color= observed), size = 3) +
        geom_line(data= function(dat) dat[dat$observed, ,drop=FALSE],
                  aes(group = labels), size= 1.2) +
        ylab("") + xlab("") +
        scale_x_continuous(limits = c(0, 1), expand = c(0, 0)) +
        scale_fill_manual(values= c(`TRUE` = "white", `FALSE` = "#F7F7F7")) +
        scale_color_manual(values= c(`TRUE` = "black", `FALSE` = "#E0E0E0")) +
        guides(color="none", fill="none") +
        theme(
          panel.background = element_blank(),
          axis.text.x = element_blank(),
          axis.ticks.y = element_blank(),
          axis.ticks.length = unit(0, "pt"),
          axis.title.y = element_blank(),
          axis.title.x = element_blank(),
          axis.line = element_blank(),
          panel.border = element_blank()
        )
    })


ggupset documentation built on June 24, 2024, 5:08 p.m.