|
a |
|
b/R/EnrichPolarBubble.R |
|
|
1 |
#' Enrichment Polar Bubble Plot |
|
|
2 |
#' |
|
|
3 |
#' This function creates a polar bubble plot using 'ggplot2'. It is designed to visually represent data with methods and positional metrics integrated, highlighting specific IDs if necessary. |
|
|
4 |
#' |
|
|
5 |
#' @param final_combined_df_with_id_and_position A data frame containing 'id', 'Count', 'method', 'Description', 'point_position', 'test_color'. |
|
|
6 |
#' @param pal A named vector of colors corresponding to the 'method' values. |
|
|
7 |
#' @param highlight_ids A vector of IDs to highlight. |
|
|
8 |
#' @importFrom ggplot2 ggplot aes geom_point geom_hline geom_segment scale_fill_manual scale_size coord_polar theme_void scale_color_manual scale_x_continuous scale_y_continuous annotate theme scale_fill_identity |
|
|
9 |
#' @importFrom dplyr filter |
|
|
10 |
#' @importFrom stringr str_c |
|
|
11 |
#' @importFrom tibble tibble |
|
|
12 |
#' @importFrom geomtextpath geom_textpath |
|
|
13 |
#' @importFrom ggalt stat_xspline |
|
|
14 |
#' @importFrom ggnewscale new_scale_fill |
|
|
15 |
#' @return A `ggplot` object representing the enriched polar bubble plot. |
|
|
16 |
#' @examples |
|
|
17 |
#' final_df <- data.frame(id = 1:10, Count = c(10, 20, 30, 40, 50, 60, 70, 80, 90, 100), |
|
|
18 |
#' method = rep("Method1", 10), |
|
|
19 |
#' Description = LETTERS[1:10], |
|
|
20 |
#' point_position = seq(10, 100, 10), |
|
|
21 |
#' test_color = sample(c("red", "blue"), 10, replace = TRUE)) |
|
|
22 |
#' pal <- c("Method1" = "blue") |
|
|
23 |
#' highlight_ids <- c(1, 5, 9) |
|
|
24 |
#' enrich_polar_bubble(final_df, pal, highlight_ids) |
|
|
25 |
#' @export |
|
|
26 |
enrich_polar_bubble <- function(final_combined_df_with_id_and_position, pal, highlight_ids) { |
|
|
27 |
# Extract unique levels of point_position and prepend 0 |
|
|
28 |
levels <- c(0, sort(unique(final_combined_df_with_id_and_position$point_position))) |
|
|
29 |
max_level_plus_five <- max(levels) + 5 |
|
|
30 |
max_id_plus_one <- max(final_combined_df_with_id_and_position$id) + 1 |
|
|
31 |
|
|
|
32 |
gg <- ggplot2::ggplot(data = final_combined_df_with_id_and_position, |
|
|
33 |
ggplot2::aes(x = .data$id, y = .data$Count, group = .data$method, fill = .data$method, color = .data$method)) + |
|
|
34 |
ggplot2::geom_hline(yintercept = levels, color = "grey85") + |
|
|
35 |
ggplot2::geom_hline(yintercept = max_level_plus_five, color = "grey15") + |
|
|
36 |
ggplot2::geom_segment(data = tibble::tibble(x = 1:max_id_plus_one, y = 0, yend = max_level_plus_five), |
|
|
37 |
ggplot2::aes(x = .data$x, xend = .data$x, y = .data$y, yend = .data$yend), |
|
|
38 |
inherit.aes = FALSE, color = "grey85") + |
|
|
39 |
ggplot2::geom_segment(data = tibble::tibble(x = 1:max_id_plus_one, y = max(levels), yend = max_level_plus_five), |
|
|
40 |
ggplot2::aes(x = .data$x, xend = .data$x, y = .data$y, yend = .data$yend), |
|
|
41 |
inherit.aes = FALSE, color = "grey15") + |
|
|
42 |
geomtextpath::geom_textpath(ggplot2::aes(x = final_combined_df_with_id_and_position$id, y = max_level_plus_five + 5, label = final_combined_df_with_id_and_position$Description, angle = 55), |
|
|
43 |
inherit.aes = FALSE, hjust = 0, size = 5.5, color = final_combined_df_with_id_and_position$test_color) + |
|
|
44 |
ggplot2::geom_point(ggplot2::aes(x = .data$id, y = .data$point_position, size = .data$Count), |
|
|
45 |
shape = 21, alpha = 0.6) + |
|
|
46 |
ggalt::stat_xspline(geom = "line", spline_shape = 0.25, linewidth = 0.75, alpha = 0.4) + |
|
|
47 |
ggalt::stat_xspline(geom = "area", alpha = 0.25, spline_shape = 0.25, outline.type = "upper") + |
|
|
48 |
ggplot2::scale_size(range = c(0, 14), breaks = levels, |
|
|
49 |
guide = ggplot2::guide_legend(title = "Count")) + |
|
|
50 |
ggplot2::scale_fill_manual(values = pal) + |
|
|
51 |
ggplot2::scale_color_manual(values = pal) + |
|
|
52 |
ggplot2::scale_x_continuous(expand = c(0, 0)) + |
|
|
53 |
ggplot2::scale_y_continuous(limits = c(-50, max_level_plus_five + 15), expand = c(0, 0)) + |
|
|
54 |
ggplot2::coord_polar() + |
|
|
55 |
ggplot2::annotate("text", x = 1, y = levels, label = "-", hjust = 1, size = 4) + |
|
|
56 |
ggplot2::annotate("text", x = 1.1, y = levels, label = levels, hjust = 0, size = 4) + |
|
|
57 |
ggplot2::annotate("text", x = 1, y = -50, label = stringr::str_c("BP/CC/MF/KEGG/DO/REACTOME"), size = 4) + |
|
|
58 |
ggplot2::theme_void() + |
|
|
59 |
ggplot2::theme(plot.background = ggplot2::element_rect(fill = "white", color = NA)) + |
|
|
60 |
ggnewscale::new_scale_fill() + |
|
|
61 |
ggplot2::geom_rect(data = dplyr::filter(final_combined_df_with_id_and_position, .data$id %in% highlight_ids), |
|
|
62 |
ggplot2::aes(xmin = .data$id - 0.5, xmax = .data$id + 0.5, ymin = 0, ymax = max_level_plus_five, fill = .data$test_color), |
|
|
63 |
alpha = 0.1, inherit.aes = FALSE) + |
|
|
64 |
ggplot2::scale_fill_identity() # Use colors specified in 'test_color' |
|
|
65 |
return(gg) |
|
|
66 |
} |