Learn R Programming

tableExtra

tableExtra is an R package that allows you to produce heatmaps with double information encoded in the shapes colors and sizes. The goal of tableExtra is to be able to produce figures like the figure 3 of the paper PCAWG Mutational Signatures Working Group et al. The repertoire of mutational signatures in human cancer. Nature 2020. doi.

Installation

You can install the development version of tableExtra from GitHub with:

# install.packages("devtools")
devtools::install_github("ypradat/tableExtra")

Example code

This is a basic example which shows you how to reproduce the first figure displayed in Example figures section.

library(dplyr)
library(tableExtra)
library(tibble)

load(system.file("testdata", "pcawg_counts.rda", package="tableExtra"))
load(system.file("testdata", "sbs_aetiologies.rda", package="tableExtra"))

pcawg_plot_data <- function(){
  scale_breaks <- seq(from=0, to=1, by=0.1)
  color_palette <- c("#ffc651", "#ffa759", "#ff8962", "#ff6b6b", "#cc6999", "#9968c8", 
                     "#6767f8", "#4459ce", "#224ba5","#013d7c")
  color_breaks <- c(0, 0.05, 0.1, 0.25, 0.5, 1, 2.5, 5, 10, 25, 1e6)
  color_bg <- c("#f8f9fa", "#e9ecef")

  theme <- ttheme_awesome(base_size=12,
                          rep_mode="col",
                          core_size=5, 
                          scale_breaks=scale_breaks,
                          color_palette=color_palette, 
                          color_breaks=color_breaks, 
                          core=list(bg_params=list(fill=color_bg)))

  # define dscale and cols_more from PCAWG data
  dscale <- pcawg_counts %>%
    group_by(Cancer.Types) %>%
    mutate(n=n()) %>%
    summarize_at(vars(-Sample.Names, -Accuracy), ~sum(.x>0)) %>%
    mutate_at(vars(-Cancer.Types,-n), ~./n)

  cols_more <- list("n="=dscale$n)
  dscale$n <- NULL
  dscale <- column_to_rownames(.data=dscale, var="Cancer.Types")
  dscale <- t(as.matrix(dscale))
  
  # define dcolor and rows_more from PCAWG data
  mask <- sbs_aetiologies$Signature %in% rownames(dscale)
  rows_more <- list("Aetiology"=sbs_aetiologies[mask, "Aetiology"])

  dcolor <- pcawg_counts %>%
    group_by(Cancer.Types) %>%
    summarize_at(vars(-Sample.Names, -Accuracy), ~median(.[.!=0]*1e6/3.2e9)) %>%
    replace(is.na(.),0)

  dcolor <- column_to_rownames(.data=dcolor, var="Cancer.Types")
  dcolor <- t(as.matrix(dcolor))

  list(dscale=dscale, dcolor=dcolor, cols_more=cols_more, rows_more=rows_more, theme=theme)
}

# tables needed for the plot and graphical parameters in `theme`
plot_data <- pcawg_plot_data()
output <- "table_extra_pcawg.pdf"

# draw
draw_table_extra(dscale=plot_data$dscale, theme=plot_data$theme, output=output,
                 dcolor=plot_data$dcolor, cols_more=plot_data$cols_more,
                 rows_more=plot_data$rows_more,
                 dscale_title_legend="Prop of tumors with the signature",
                 dcolor_title_legend="Median mut/Mb due to signature")

Example figures

The figures below were produced using this package.

PCAWG Fig 3 SBS signatures

PCAWG Fig 3 SBS signatures variants

The figure may be customized by addition/removal of rows/columns descriptors and position of the legend.

No legend, no rows more

Legend top left, no rows more

Legend top right, no rows more

Legend center, no rows more

Copy Link

Version

Install

install.packages('tableExtra')

Monthly Downloads

50

Version

1.0.1

License

Apache License (>= 2.0)

Maintainer

Yoann Pradat

Last Published

October 29th, 2021

Functions in tableExtra (1.0.1)

gtable_extra

Grob underlying graphical display of a table with grobs of varying scales and colours.
add_table_params

Add parameter specific to the grob class.
tableExtra

An easy-to-use tool for drawing paper-quality tables.
gtable_rbind

rbind two or more gtables
table_params_text

Add text parameters
rect_grob

Vectorised version of rectGrob
draw_table_extra

Graphical display of a table with grobs of varying scales and colours.
cbind_2

cbind two gtables
circle_grob

Vectorised version of circleGrob
text_grob

Vectorised version of textGrob
table_params

Build a table of parameters.
get_table_extra_dimensions

Get width and height of the plot.
table_params_circle

Add circle parameters
table_params_color

Add circle color parameters
ttheme_awesome

Define theme for awesome table plot.
table_params_circle_scale

Add circle scale parameters
table_params_rect

Add rect parameters
gtable_table

Build a table with foreground and background grobs.
insert.unit

Insert unit
gtable_legend

Build a grob containing a legend.
rbind_2

rbind two gtables