|
24 | 24 | #' Works only for "comparison", "dot" and "lollipop". |
25 | 25 | #' @param fill_name The legend name for the metric. Default is NULL. |
26 | 26 | #' 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. |
27 | 29 | #' @param character_width The width of the terms in the plot. Default is 50. |
28 | 30 | #' When the terms are too long, they will be wrapped to fit the width. |
29 | 31 | #' @param expand A numeric vector of length 1, 2 or 4 to expand the plot. Default is NULL. |
|
36 | 38 | #' @param facet_by A character vector of column names to facet the plots. Default is NULL. |
37 | 39 | #' @param facet_scales The facet scales. Default is NULL. |
38 | 40 | #' @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()]. |
40 | 43 | #' @param group_by_sep A character to concatenate the group_by columns when there are multiple columns. Default is "_". |
41 | 44 | #' Works only for "comparison" plot. |
42 | 45 | #' @param palette The color palette to use for the plot. Default is "Spectral". |
|
51 | 54 | #' * For "enrichmap", [plotthis::EnrichMap()]. |
52 | 55 | #' * For "wordcloud", [plotthis::WordCloudPlot()]. |
53 | 56 | #' * For "comparison", [plotthis::DotPlot()]. |
| 57 | +#' * For "heatmap", [plotthis::Heatmap()]. |
54 | 58 | #' @importFrom rlang sym syms |
55 | 59 | #' @importFrom stringr str_wrap |
56 | 60 | #' @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 |
58 | 62 | #' @export |
59 | 63 | #' @examples |
60 | 64 | #' set.seed(8525) |
61 | 65 | #' data(enrich_example, package = "plotthis") |
| 66 | +#' enrich_example$Group <- sample(LETTERS[1:3], nrow(enrich_example), replace = TRUE) |
62 | 67 | #' data(enrich_multidb_example, package = "plotthis") |
63 | 68 | #' |
64 | 69 | #' EnrichmentPlot(enrich_example) |
65 | 70 | #' EnrichmentPlot(enrich_example, cutoff = 0.05) |
66 | 71 | #' EnrichmentPlot(enrich_example, palette = "Paired") |
67 | 72 | #' |
| 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 | +#' |
68 | 77 | #' # Multiple databases#' |
69 | 78 | #' EnrichmentPlot(enrich_multidb_example, facet_by = "Database", facet_nrow = 2) |
70 | 79 | #' |
|
79 | 88 | #' EnrichmentPlot(enrich_example, plot_type = "wordcloud", word_type = "feature") |
80 | 89 | EnrichmentPlot <- function( |
81 | 90 | 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, |
84 | 93 | character_width = 50, expand = NULL, word_type = c("term", "feature"), |
85 | 94 | split_by = NULL, split_by_sep = "_", facet_by = NULL, facet_scales = NULL, |
86 | 95 | group_by = NULL, group_by_sep = "_", metric = "p.adjust", cutoff = NULL, |
@@ -130,18 +139,45 @@ EnrichmentPlot <- function( |
130 | 139 | } |
131 | 140 |
|
132 | 141 | # 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 | + } |
137 | 152 | # Convert GeneRatio from something like "38/225" to 0.169 |
138 | 153 | data$GeneRatio <- as.numeric(sapply(strsplit(data$GeneRatio, "/"), function(x) as.numeric(x[1]) / as.numeric(x[2]))) |
139 | 154 | if (!is.null(data$BgRatio) && !all(is.na(data$BgRatio))) { |
140 | 155 | data$BgRatio <- as.numeric(sapply(strsplit(data$BgRatio, "/"), function(x) as.numeric(x[1]) / as.numeric(x[2]))) |
141 | 156 | } |
142 | 157 | } |
143 | 158 |
|
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") { |
145 | 181 | if (!is.null(group_by)) { |
146 | 182 | stop("'group_by' is not supported for Enrichment bar plot. Use 'facet_by'/'split_by' to split the plots.") |
147 | 183 | } |
|
0 commit comments