Skip to content

Commit a17dbae

Browse files
committed
feat(EnrichmentPlot): add support for heatmap plot type and new values_fill parameter
1 parent 6d50d0b commit a17dbae

File tree

2 files changed

+58
-11
lines changed

2 files changed

+58
-11
lines changed

R/enrichmentplot.R

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
#' Works only for "comparison", "dot" and "lollipop".
2525
#' @param fill_name The legend name for the metric. Default is NULL.
2626
#' Works only for "comparison", "dot" and "lollipop".
27+
#' @param values_fill The value to fill the missing values in the data. Default is 0.
28+
#' Used only for "heatmap" plot.
2729
#' @param character_width The width of the terms in the plot. Default is 50.
2830
#' When the terms are too long, they will be wrapped to fit the width.
2931
#' @param expand A numeric vector of length 1, 2 or 4 to expand the plot. Default is NULL.
@@ -36,7 +38,8 @@
3638
#' @param facet_by A character vector of column names to facet the plots. Default is NULL.
3739
#' @param facet_scales The facet scales. Default is NULL.
3840
#' @param group_by A character vector of column names to group the terms. Default is NULL.
39-
#' Works only for "comparison" plot.
41+
#' Works only for "comparison" and "heatmap" plot.
42+
#' For heatmap, it will be used as `columns_by` in [plotthis::Heatmap()].
4043
#' @param group_by_sep A character to concatenate the group_by columns when there are multiple columns. Default is "_".
4144
#' Works only for "comparison" plot.
4245
#' @param palette The color palette to use for the plot. Default is "Spectral".
@@ -51,20 +54,26 @@
5154
#' * For "enrichmap", [plotthis::EnrichMap()].
5255
#' * For "wordcloud", [plotthis::WordCloudPlot()].
5356
#' * For "comparison", [plotthis::DotPlot()].
57+
#' * For "heatmap", [plotthis::Heatmap()].
5458
#' @importFrom rlang sym syms
5559
#' @importFrom stringr str_wrap
5660
#' @importFrom dplyr %>% group_by slice_min ungroup
57-
#' @importFrom plotthis BarPlot DotPlot LollipopPlot EnrichNetwork EnrichMap WordCloudPlot
61+
#' @importFrom plotthis BarPlot DotPlot LollipopPlot EnrichNetwork EnrichMap WordCloudPlot Heatmap
5862
#' @export
5963
#' @examples
6064
#' set.seed(8525)
6165
#' data(enrich_example, package = "plotthis")
66+
#' enrich_example$Group <- sample(LETTERS[1:3], nrow(enrich_example), replace = TRUE)
6267
#' data(enrich_multidb_example, package = "plotthis")
6368
#'
6469
#' EnrichmentPlot(enrich_example)
6570
#' EnrichmentPlot(enrich_example, cutoff = 0.05)
6671
#' EnrichmentPlot(enrich_example, palette = "Paired")
6772
#'
73+
#' enrich_example$Description <- enrich_example$ID
74+
#' EnrichmentPlot(enrich_example, plot_type = "heatmap", group_by = "Group",
75+
#' show_row_names = TRUE, show_column_names = TRUE, cutoff = 0.05)
76+
#'
6877
#' # Multiple databases#'
6978
#' EnrichmentPlot(enrich_multidb_example, facet_by = "Database", facet_nrow = 2)
7079
#'
@@ -79,8 +88,8 @@
7988
#' EnrichmentPlot(enrich_example, plot_type = "wordcloud", word_type = "feature")
8089
EnrichmentPlot <- function(
8190
data, top_term = NULL,
82-
plot_type = c("bar", "dot", "lollipop", "network", "enrichmap", "wordcloud", "comparison"),
83-
x_by = NULL, size_by = NULL, fill_cutoff_name = NULL, fill_name = NULL,
91+
plot_type = c("bar", "dot", "lollipop", "network", "enrichmap", "wordcloud", "comparison", "heatmap"),
92+
x_by = NULL, size_by = NULL, fill_cutoff_name = NULL, fill_name = NULL, values_fill = 0,
8493
character_width = 50, expand = NULL, word_type = c("term", "feature"),
8594
split_by = NULL, split_by_sep = "_", facet_by = NULL, facet_scales = NULL,
8695
group_by = NULL, group_by_sep = "_", metric = "p.adjust", cutoff = NULL,
@@ -130,18 +139,45 @@ EnrichmentPlot <- function(
130139
}
131140

132141
# preprocessing
133-
if (plot_type %in% c("bar", "comparison", "dot", "lollipop")) {
134-
data$.metric <- -log10(data[[metric]])
135-
# we lost order?
136-
data[[descr_col]] <- str_wrap(data[[descr_col]], width = character_width)
142+
if (plot_type %in% c("bar", "comparison", "dot", "lollipop", "heatmap")) {
143+
if (metric %in% c("pvalue", "p.adjust", "qvalue")) {
144+
data$.metric <- -log10(data[[metric]])
145+
} else {
146+
data$.metric <- data[[metric]]
147+
}
148+
if (plot_type != "heatmap") {
149+
# we lost order?
150+
data[[descr_col]] <- str_wrap(data[[descr_col]], width = character_width)
151+
}
137152
# Convert GeneRatio from something like "38/225" to 0.169
138153
data$GeneRatio <- as.numeric(sapply(strsplit(data$GeneRatio, "/"), function(x) as.numeric(x[1]) / as.numeric(x[2])))
139154
if (!is.null(data$BgRatio) && !all(is.na(data$BgRatio))) {
140155
data$BgRatio <- as.numeric(sapply(strsplit(data$BgRatio, "/"), function(x) as.numeric(x[1]) / as.numeric(x[2])))
141156
}
142157
}
143158

144-
if (plot_type == "bar") {
159+
if (plot_type == "heatmap") {
160+
if (!is.null(facet_by)) {
161+
stop('[EnrichmentPlot] "heatmap" plot does not support "facet_by". Use "split_by" to split the heatmap.')
162+
}
163+
data[[metric]] <- NULL
164+
if (metric %in% c("pvalue", "p.adjust", "qvalue")) {
165+
metric <- paste0("-log10(", metric, ")")
166+
if (!is.null(cutoff)) {
167+
cutoff <- -log10(cutoff)
168+
}
169+
}
170+
if (is.null(cutoff)) {
171+
Heatmap(data, in_form = "long", values_by = ".metric", name = metric,
172+
rows_by = descr_col, columns_by = group_by, values_fill = values_fill, ...)
173+
} else {
174+
Heatmap(data, in_form = "long", values_by = ".metric", name = metric,
175+
rows_by = descr_col, columns_by = group_by, values_fill = values_fill,
176+
cell_type = "label", label = function(x) {
177+
ifelse(x > cutoff, '*', NA)
178+
}, ...)
179+
}
180+
} else if (plot_type == "bar") {
145181
if (!is.null(group_by)) {
146182
stop("'group_by' is not supported for Enrichment bar plot. Use 'facet_by'/'split_by' to split the plots.")
147183
}

man/EnrichmentPlot.Rd

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

0 commit comments

Comments
 (0)