draw_table_extra: Graphical display of a table with grobs of varying scales and...

Description Usage Arguments Value Author(s) See Also Examples

View source: R/draw_table_extra.R

Description

Draw a table containing grobs of varying sizes and colors encoding two different kinds of information. The column names and row names of the table are displayed on the top and left sides of the table respectively.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
draw_table_extra(
  dscale,
  theme,
  output,
  dcolor = NULL,
  dscale_min = NULL,
  dscale_max = NULL,
  cols_more = NULL,
  rows_more = NULL,
  dscale_title_legend = "Scale title",
  dcolor_title_legend = "Color title",
  margin_x = unit(1, "inches"),
  margin_y = unit(1, "inches")
)

Arguments

dscale

a matrix containing the values defining the grobs scales.

theme

a list of theme parameters. Use an instance of ttheme_awesome.

output

path to output file. Only pdf supported for now.

dcolor

(optional) a matrix of size (n,m) containing the values defining the grobs colors.

dscale_min

(optional) value for setting the minimum scale size of foreground grobs. Entries in the dscale matrix below dscale_min will have a scale of 0 (no grob).

dscale_max

(optional) value for setting the maximum scale size of foreground grobs. Entries in the dscale matrix above dscale_max will have a scale of 1.

cols_more

(optional) a named list of additional rows (top-part) of the plot for describing the columns The list names will be used as row headers.

rows_more

(optional) a named list of additional columns (right-part) of the plot for describing the rows. The list names will be used as column headers.

dscale_title_legend

(optional) title for the colorbar providing a legend for scales.

dcolor_title_legend

(optional) title for the colorbar providing a legend for colors

margin_x

(optional) use it to fine-tune the width of the plot if some elements are not displayed correctly.

margin_y

(optional) use it to fine-tune the height of the plot if some elements are not displayed correctly.

Value

No return value, the last instruction calls graphics.off() in order to write the plot to the .pdf file specified via output argument.

Author(s)

Yoann Pradat

See Also

ttheme_awesome(), gtable_table(), gtable_legend()

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
library(dplyr)
library(tableExtra)
library(tibble)

# load data
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()

# draw
output <- file.path(tempdir(),"table_extra_pcawg.pdf")
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")

tableExtra documentation built on Oct. 29, 2021, 5:06 p.m.