Skip to content

Commit 3dbdc32

Browse files
committed
Plotting of numbers of significantly regulated reactions in metabolic subsystems. The plot() method for actiData objects.
1 parent 3d137a2 commit 3dbdc32

File tree

10 files changed

+411
-20
lines changed

10 files changed

+411
-20
lines changed

NAMESPACE

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ S3method(count_regulated,actiData)
55
S3method(get_activity,data.frame)
66
S3method(get_activity,matrix)
77
S3method(identify_regulated,actiData)
8+
S3method(plot,actiData)
89
S3method(print,actiData)
910
S3method(print,reactDB)
1011
S3method(select,actiData)
@@ -20,8 +21,10 @@ export(get_regulation)
2021
export(identify_regulated)
2122
export(is_actiData)
2223
export(is_reactDB)
24+
export(plot.actiData)
2325
export(plot_errors)
2426
export(plot_mc)
27+
export(plot_numbers)
2528
export(reactDB)
2629
export(select.actiData)
2730
export(transform_estimates)
@@ -44,6 +47,7 @@ importFrom(furrr,future_map)
4447
importFrom(future,plan)
4548
importFrom(generics,components)
4649
importFrom(ggplot2,aes)
50+
importFrom(ggplot2,geom_bar)
4751
importFrom(ggplot2,geom_hline)
4852
importFrom(ggplot2,geom_line)
4953
importFrom(ggplot2,geom_point)
@@ -54,6 +58,8 @@ importFrom(ggplot2,ggplot)
5458
importFrom(ggplot2,is.theme)
5559
importFrom(ggplot2,labs)
5660
importFrom(ggplot2,position_jitter)
61+
importFrom(ggplot2,scale_fill_manual)
62+
importFrom(ggplot2,scale_x_continuous)
5763
importFrom(microViz,theme_micro)
5864
importFrom(purrr,compact)
5965
importFrom(purrr,map)

R/actiData_methods.R

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,4 +428,63 @@
428428

429429
}
430430

431+
# Plotting -----------
432+
433+
#' Diagnostic plots and visualization of reaction activity in `actiData` objects.
434+
#'
435+
#' @description
436+
#' Diagnostic plots and visualization of estimates of regulation of reaction
437+
#' activity - the plotting method for \code{\link{actiData}} objects.
438+
#'
439+
#' @details
440+
#' The `plot()` method for \code{\link{actiData}} objects generates the following
441+
#' types of plots specified by `plot_type` argument:
442+
#'
443+
#' * `plot_type = "errors"`: scatter plot of regulation estimates and their errors
444+
#' made with \code{\link{plot_errors}}.
445+
#'
446+
#' * `plot_type = "numbers"`: bar/stack plots of numbers or percentages of
447+
#' differentially regulated
448+
#' reactions in subsystems generated with \code{\link{plot_numbers}}. Please note
449+
#' that the regulation status of metabolic reactions in `x` has to be determined
450+
#' prior to plotting by calling \code{\link{identify_regulated}}.
451+
#'
452+
#' * `plot_type == "mc"`: plots of reactions regulation estimates in single
453+
#' iterations of the Monte Carlo simulation with \code{\link{plot_mc}}.
454+
#'
455+
#' @return a `ggplot` graphic object.
456+
#'
457+
#' @param x an \code{\link{actiData}} object.
458+
#' @param plot_type plot type, see Details.
459+
#' @param ... additional arguments passed to the plotting functions specified
460+
#' in Details.
461+
#'
462+
#' @seealso [get_regulation()]
463+
#'
464+
#' @md
465+
#'
466+
#' @export plot.actiData
467+
#' @export
468+
469+
plot.actiData <- function(x,
470+
plot_type = c("errors",
471+
"numbers",
472+
"mc"), ...) {
473+
474+
## input control --------
475+
476+
stopifnot(is_actiData(x))
477+
478+
plot_type <- match.arg(plot_type[1],
479+
c("errors", "numbers", "mc"))
480+
481+
## re-routing ----------
482+
483+
switch(plot_type,
484+
errors = plot_errors(x, ...),
485+
numbers = plot_numbers(x, ...),
486+
mc = plot_mc(x, ...))
487+
488+
}
489+
431490
# END ---------

R/imports.R

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@
3939
#' @importFrom ggplot2 geom_violin
4040
#' @importFrom ggplot2 geom_vline
4141
#' @importFrom ggplot2 geom_hline
42-
#'
42+
#' @importFrom ggplot2 geom_bar
43+
#' @importFrom ggplot2 scale_fill_manual
44+
#' @importFrom ggplot2 scale_x_continuous
4345
#' @importFrom ggplot2 position_jitter
4446
#' @importFrom ggplot2 is.theme
4547
#' @importFrom ggplot2 labs

R/plotting_funs.R

Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,4 +385,182 @@
385385

386386
}
387387

388+
# Plotting of numbers of significantly regulate reactions ---------
389+
390+
#' Bar/stack plots with numbers of significantly activated and inhibited reactions.
391+
#'
392+
#' @description
393+
#' Representation of numbers or percentages of differentially regulated
394+
#' reactions in subsystems as bar or stack plot.
395+
#'
396+
#' @details
397+
#' Please make sure that the object contains activity regulation status
398+
#' information.
399+
#' If not, please call \code{\link{identify_regulated}} prior to
400+
#' counting the significant effects.
401+
#'
402+
#' The plot type is determined by `type` argument:
403+
#' `"plus_minus"` (bar plot, inhibited reaction as "negative" numbers),
404+
#' `"bar"` (bar plot, dodged bars for inhibited and activated reactions),
405+
#' `"stack"` plot.
406+
#'
407+
#' @return a `ggplot` graphic.
408+
#'
409+
#' @inheritParams plot_errors
410+
#' @param scale type of the frequency statistic to be shown in the plot:
411+
#' count (default) or percentage of reactions in the subsystem.
412+
#' @param type type of the plot, see Details.
413+
#' @param show_all logical, should frequencies of all regulated reactions be
414+
#' presented in the plot together with counts/percentages for the subsystems?
415+
#' @param palette color palette: a named character vector with at least two elements
416+
#' specifying the colors for activated and inhibited reactions.
417+
#' @param bar_rim_color color of the bar's rim/line.
418+
#' @param show_n_total logical: if `TRUE`, total numbers of reactions in subsystems
419+
#' are displayed in the Y axis along with the subsystem names.
420+
#' @param labeller_fun a function used to transform subsystem names, e.g. into
421+
#' abbreviations, which are shown in the Y axis of the plot.
422+
#' @param fill_lab title of the fill scale.
423+
#' @param ... additional arguments passed to \code{\link[ggplot2]{geom_bar}}.
424+
#'
425+
#' @export
426+
427+
plot_numbers <- function(x,
428+
reactions = NULL,
429+
subsystems = NULL,
430+
scale = c("count", "percent"),
431+
type = c("plus_minus", "bar", "stack"),
432+
palette = c("activated" = "firebrick",
433+
"inhibited" = "steelblue"),
434+
bar_rim_color = "black",
435+
show_all = FALSE,
436+
labeller_fun = identity,
437+
show_n_total = TRUE,
438+
cust_theme = microViz::theme_micro(),
439+
plot_title = NULL,
440+
plot_subtitle = NULL,
441+
x_lab = NULL,
442+
y_lab = "subsystem",
443+
fill_lab = "regulation\nstatus", ...) {
444+
445+
## input control --------
446+
447+
if(!is_actiData(x)) stop("`x` has to be an `actiData` object.", call. = FALSE)
448+
449+
scale <- match.arg(scale[1], c("count", "percent"))
450+
451+
type <- match.arg(type[1], c("plus_minus", "bar", "stack"))
452+
453+
stopifnot(is.logical(show_all))
454+
show_all <- show_all[1]
455+
456+
stopifnot(is.logical(show_n_total))
457+
show_n_total <- show_n_total[1]
458+
459+
stopifnot(is.character(palette))
460+
461+
if(length(palette) < 2) {
462+
463+
stop("`palette` has to have at least two color names.", call. = FALSE)
464+
465+
}
466+
467+
palette <- palette[1:2]
468+
469+
if(!is_function(labeller_fun)) {
470+
471+
stop("`labeller_fun` has to be a function.", call = FALSE)
472+
473+
}
474+
475+
if(!is.null(cust_theme)) {
476+
477+
if(!is.theme(cust_theme)) {
478+
479+
stop("`cust_theme` has to be a `ggplot` theme object.", call. = FALSE)
480+
481+
}
482+
483+
}
484+
485+
## plotting data --------
486+
487+
x <- select(x, reactions, subsystems)
488+
489+
count_data <- count_regulated(x)
490+
491+
if(!show_all) {
492+
493+
count_data <- filter(count_data, .data[["subsystem"]] != "all")
494+
495+
}
496+
497+
plot_variable <- switch(scale,
498+
count = "n",
499+
percent = "percent")
500+
501+
count_data[["axis_label"]] <- labeller_fun(count_data[["subsystem"]])
502+
503+
if(show_n_total) {
504+
505+
count_data[["axis_label"]] <-
506+
paste(count_data[["axis_label"]],
507+
count_data[["n_total"]],
508+
sep = "\nn = ")
509+
510+
}
511+
512+
if(is.null(x_lab)) {
513+
514+
x_lab <- switch(scale,
515+
count = "number of reactions",
516+
percent = "% of subsystem reactions")
517+
518+
}
519+
520+
## the plots ----------
521+
522+
if(type %in% c("bar", "stack")) {
523+
524+
pos_txt <- switch(type,
525+
bar = "dodge",
526+
stack = "stack")
527+
528+
number_plot <- ggplot(count_data,
529+
aes(x = .data[[plot_variable]],
530+
y = reorder(.data[["axis_label"]],
531+
.data[[plot_variable]]),
532+
fill = .data[["regulation"]])) +
533+
geom_bar(stat = "identity",
534+
color = bar_rim_color,
535+
position = pos_txt, ...)
536+
537+
} else {
538+
539+
number_plot <- ggplot(count_data,
540+
aes(x = ifelse(.data[["regulation"]] == "activated",
541+
.data[[plot_variable]],
542+
-.data[[plot_variable]]),
543+
y = reorder(.data[["axis_label"]],
544+
.data[[plot_variable]]),
545+
fill = .data[["regulation"]])) +
546+
geom_bar(stat = "identity",
547+
color = bar_rim_color, ...) +
548+
scale_x_continuous(labels = abs)
549+
550+
}
551+
552+
if(!is.null(cust_theme)) number_plot <- number_plot + cust_theme
553+
554+
number_plot +
555+
geom_vline(xintercept = 0) +
556+
scale_fill_manual(values = palette,
557+
drop = FALSE) +
558+
labs(title = plot_title,
559+
subtitle = plot_subtitle,
560+
x = x_lab,
561+
y = y_lab,
562+
fill = fill_lab)
563+
564+
}
565+
388566
# END --------

inst/examples/development.R

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -155,28 +155,47 @@
155155
# Diagnostic plots ----------
156156

157157
tst_norm_estimates %>%
158-
plot_errors(fun = log2,
159-
line_color = "orangered3",
160-
fill = "gray80")
158+
plot(plot_type = "errors",
159+
fun = log2,
160+
line_color = "orangered3",
161+
fill = "gray80")
161162

162163
tst_mc_estimates %>%
163-
plot_errors(subsystems = "Fatty acid oxidation",
164-
fun = sqrt,
165-
line_color = "orangered3",
166-
line_alpha = 0.5,
167-
fill = "gray80")
164+
plot(plot_type = "errors",
165+
subsystems = "Fatty acid oxidation",
166+
fun = sqrt,
167+
line_color = "orangered3",
168+
line_alpha = 0.5,
169+
fill = "gray80")
168170

169171
tst_norm_estimates %>% plot_mc
170172

171173
tst_mc_estimates %>%
172-
plot_mc(subsystems = "Oxidative phosphorylation",
173-
fun = sqrt)
174+
plot(plot_type = "mc",
175+
subsystems = "Oxidative phosphorylation",
176+
fun = sqrt)
174177

175178
tst_mc_estimates %>%
176-
plot_mc(subsystems = "Oxidative phosphorylation",
177-
fun = sqrt,
178-
type = "violin",
179-
point_alpha = 0.15,
180-
plot_title = "Oxidative phosphorylation")
179+
plot(plot_type = "mc",
180+
subsystems = "Oxidative phosphorylation",
181+
fun = sqrt,
182+
type = "violin",
183+
point_alpha = 0.15,
184+
plot_title = "Oxidative phosphorylation")
185+
186+
tst_mc_estimates %>%
187+
identify_regulated %>%
188+
plot(plot_type = "numbers",
189+
scale = "percent",
190+
type = "plus_minus",
191+
subsystems = c("Oxidative phosphorylation",
192+
"Citric acid cycle",
193+
"Glycolysis/gluconeogenesis",
194+
"Fatty acid synthesis",
195+
"Fatty acid oxidation"),
196+
plot_title = "Energy metabolism")
197+
198+
# Subsystem enrichment ---------
199+
181200

182201
# END --------

man/plot.actiData.Rd

Lines changed: 41 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)