@@ -24,5 +24,133 @@ x + ggprism::theme_prism(base_size = 9)
2424### Heatmap Plot
2525
2626``` {r}
27- x <- quiet(PAVER::PAVER_hunter_plot(.[[1]], unit = .[[2]]))
28- ```
27+ #' PAVER_hunter_plot
28+ #'
29+ #' This function takes a PAVER result object and generates a heatmap of the enrichment analysis
30+ #' with clustering and color-coded values based on the direction of regulation.
31+ #'
32+ #' @param PAVER_result a list containing the output of PAVER analysis
33+ #' @param unit optionally, the unit of enrichment analysis for the figure legend title.
34+ #' @param show_row_titles a logical indicating whether to show row titles in the heatmap.
35+ #'
36+ #' @return A heatmap of the expression data with clustering and color-coded values based on the direction of regulation.
37+ #'
38+ #' @export
39+ PAVER_hunter_plot <- function(PAVER_result, unit = NULL, show_row_titles = TRUE) {
40+ data <- PAVER_result$prepared_data %>%
41+ tidyr::pivot_wider(
42+ names_from = "Group",
43+ values_from = c("value"),
44+ id_cols = "GOID"
45+ ) %>%
46+ dplyr::inner_join(
47+ PAVER_result$clustering %>%
48+ dplyr::select(.data$GOID, .data$Cluster),
49+ by = "GOID"
50+ ) %>%
51+ dplyr::mutate(Cluster = forcats::fct_drop(.data$Cluster)) %>%
52+ dplyr::mutate(dplyr::across(
53+ .cols = dplyr::where(is.numeric),
54+ .fns = ~ tidyr::replace_na(.x, 0)
55+ )) %>%
56+ dplyr::distinct(.data$GOID, .keep_all = TRUE)
57+
58+ mat <- data %>%
59+ dplyr::select(dplyr::where(is.numeric)) %>%
60+ as.matrix()
61+
62+ min <- min(mat, na.rm = T)
63+ max <- max(mat, na.rm = T)
64+
65+ if (nlevels(PAVER_result$prepared_data$Direction) != 1) {
66+ col_fun <- circlize::colorRamp2(c(min, 0, max), c("blue", "white", "red"))
67+ at <- c(min, 0, max)
68+ labels <- c(round(min, 2), 0, round(max, 2))
69+ } else {
70+ at <- c(min, max)
71+ labels <- c(round(min, 2), round(max, 2))
72+ if (levels(PAVER_result$prepared_data$Direction) == "+") {
73+ col_fun <- circlize::colorRamp2(c(min, max), c("white", "red"))
74+ } else {
75+ col_fun <- circlize::colorRamp2(c(min, max), c("white", "blue"))
76+ }
77+ }
78+
79+ lgd <- ComplexHeatmap::Legend(
80+ col_fun = col_fun,
81+ title = unit,
82+ at = at,
83+ labels = labels,
84+ direction = "horizontal",
85+ title_position = "lefttop",
86+ title_gp = grid::gpar(fontsize = 8, fontface = "bold"),
87+ labels_gp = grid::gpar(fontsize = 8, fontface = "bold"),
88+ legend_width = grid::unit(1, "cm"),
89+ )
90+
91+ c_data <- mat %>% t()
92+
93+ if (ncol(mat) >= 1) {
94+ c_data <- rbind(c_data, c_data)
95+ }
96+
97+ dend <- ComplexHeatmap::cluster_within_group(c_data, data$Cluster)
98+
99+ color_order <- data[stats::order.dendrogram(dend), ]$Cluster %>%
100+ forcats::fct_inorder() %>%
101+ levels()
102+
103+ plot_colors <- PAVER_result$colors %>%
104+ magrittr::set_names(levels(data$Cluster))
105+
106+ plot_colors <- plot_colors[color_order]
107+
108+ cluster_annotation <- ComplexHeatmap::rowAnnotation(
109+ Cluster = ComplexHeatmap::anno_block(gp = grid::gpar(fill = plot_colors)),
110+ width = grid::unit(.25, "cm")
111+ )
112+
113+ args <- list(
114+ matrix = mat,
115+ col = col_fun,
116+ left_annotation = cluster_annotation,
117+ row_title_gp = grid::gpar(fontsize = 8, fontface = "bold"),
118+ column_names_gp = grid::gpar(fontsize = 8, fontface = "bold"),
119+ # --- EDITED SECTION START ---
120+ column_names_rot = 90, # Changed from 0 to 90 for vertical labels
121+ column_names_centered = FALSE, # Setting to FALSE often looks better with rotation
122+ column_names_side = "bottom", # Ensures labels are at the bottom of the columns
123+ # --- EDITED SECTION END ---
124+ row_title_rot = 0,
125+ row_title_side = "right",
126+ row_gap = grid::unit(1, "mm"),
127+ show_row_dend = FALSE,
128+ show_column_dend = FALSE,
129+ cluster_rows = dend,
130+ cluster_columns = ncol(mat) >= 3,
131+ split = nlevels(data$Cluster),
132+ show_heatmap_legend = FALSE,
133+ border = TRUE,
134+ row_title = NULL
135+ )
136+
137+ if (show_row_titles) {
138+ args$row_title <- character()
139+ }
140+
141+ ht <- do.call(ComplexHeatmap::Heatmap, args)
142+
143+ ht_draw <- ComplexHeatmap::draw(
144+ ht,
145+ heatmap_legend_list = lgd,
146+ heatmap_legend_side = "bottom",
147+ padding = grid::unit(c(1, 1, 5, 1), "mm") # Increased bottom padding for vertical labels
148+ )
149+
150+ plot <- grid::grid.grabExpr(ComplexHeatmap::draw(ht_draw)) %>%
151+ ggplotify::as.ggplot()
152+
153+ plot
154+ }
155+ x <- quiet(PAVER_hunter_plot(.[[1]], unit = .[[2]]))
156+ ```
0 commit comments