|
a |
|
b/R/FacetDensityFoldchange.R |
|
|
1 |
#' Create faceted high-density region plots with optional points and density contours |
|
|
2 |
#' |
|
|
3 |
#' This function creates faceted high-density region plots using ggdensity for |
|
|
4 |
#' adding optional density rug and density contours, and scatter points. It also adds a regression line |
|
|
5 |
#' and Pearson correlation label. The plot is faceted by a grouping variable. |
|
|
6 |
#' |
|
|
7 |
#' @param data Data frame containing variables for plotting. |
|
|
8 |
#' @param x_var Name of the x-axis variable as a string. |
|
|
9 |
#' @param y_var Name of the y-axis variable as a string. |
|
|
10 |
#' @param group_var Name of the grouping variable for color mapping as a string. |
|
|
11 |
#' @param facet_var Name of the faceting variable. |
|
|
12 |
#' @param palette Color palette for the plot as a character vector. |
|
|
13 |
#' @param show_points Logical, if TRUE adds scatter points to the plot. |
|
|
14 |
#' @param show_density Logical, if TRUE adds filled density contours to the plot. |
|
|
15 |
#' @param point_size Size of the points, relevant if show_points is TRUE. |
|
|
16 |
#' @param point_alpha Transparency level of the points, relevant if show_points is TRUE. |
|
|
17 |
#' @param line_size Size of the regression line. |
|
|
18 |
#' @param cor_method Method to calculate correlation ("pearson" or "spearman"). |
|
|
19 |
#' @param cor_label_pos Vector of length 2 indicating the position of the correlation label (x and y). |
|
|
20 |
#' @param cor_vjust Vertical justification for correlation label, default is NULL. |
|
|
21 |
#' @return A `ggplot` object representing the high-density region plot. |
|
|
22 |
#' @importFrom ggplot2 ggplot aes_string geom_point geom_smooth scale_fill_manual scale_color_manual facet_wrap theme margin |
|
|
23 |
#' @importFrom ggdensity geom_hdr geom_hdr_rug |
|
|
24 |
#' @importFrom ggpubr stat_cor |
|
|
25 |
#' @importFrom hrbrthemes theme_ipsum |
|
|
26 |
#' @importFrom grid unit |
|
|
27 |
#' @importFrom stats as.formula |
|
|
28 |
#' @examples |
|
|
29 |
#' combined_df_file <- system.file("extdata", "combined_df.rds", package = "TransProR") |
|
|
30 |
#' combined_df <- readRDS(combined_df_file) |
|
|
31 |
#' pal2 = c("#2787e0","#1a9ae0","#1dabbf","#00897b","#43a047","#7cb342") |
|
|
32 |
#' all_facet_density_foldchange_name1 <- facet_density_foldchange( |
|
|
33 |
#' data = combined_df, |
|
|
34 |
#' x_var = "log2FoldChange_1", |
|
|
35 |
#' y_var = "log2FoldChange_2", |
|
|
36 |
#' group_var = "name", |
|
|
37 |
#' facet_var = "name", |
|
|
38 |
#' palette = pal2, |
|
|
39 |
#' show_points = TRUE, |
|
|
40 |
#' show_density = FALSE, |
|
|
41 |
#' point_size = 2, |
|
|
42 |
#' point_alpha = 0.1, |
|
|
43 |
#' line_size = 1.6, |
|
|
44 |
#' cor_method = "pearson", |
|
|
45 |
#' cor_label_pos = c("left", "top"), |
|
|
46 |
#' cor_vjust = 1 |
|
|
47 |
#' ) |
|
|
48 |
#' @export |
|
|
49 |
facet_density_foldchange <- function(data, |
|
|
50 |
x_var, |
|
|
51 |
y_var, |
|
|
52 |
group_var, |
|
|
53 |
facet_var, |
|
|
54 |
palette, |
|
|
55 |
show_points = FALSE, |
|
|
56 |
show_density = TRUE, |
|
|
57 |
point_size = 2.5, |
|
|
58 |
point_alpha = 0.1, |
|
|
59 |
line_size = 1.6, |
|
|
60 |
cor_method = "pearson", |
|
|
61 |
cor_label_pos = c("left", 0.97), |
|
|
62 |
cor_vjust = NULL) { |
|
|
63 |
# Begin constructing the ggplot |
|
|
64 |
plot <- ggplot2::ggplot(data, ggplot2::aes_string(x = x_var, y = y_var, fill = group_var)) |
|
|
65 |
|
|
|
66 |
# Optionally add density rug |
|
|
67 |
plot <- plot + ggdensity::geom_hdr_rug() |
|
|
68 |
|
|
|
69 |
# Optionally add density contours |
|
|
70 |
if (show_density) { |
|
|
71 |
plot <- plot + ggdensity::geom_hdr() |
|
|
72 |
} |
|
|
73 |
|
|
|
74 |
# Optionally add points |
|
|
75 |
if (show_points) { |
|
|
76 |
plot <- plot + ggplot2::geom_point(ggplot2::aes_string(color = group_var), shape = 21, |
|
|
77 |
size = point_size, alpha = point_alpha) |
|
|
78 |
} |
|
|
79 |
|
|
|
80 |
# Add regression line and correlation label |
|
|
81 |
plot <- plot + |
|
|
82 |
ggplot2::geom_smooth(ggplot2::aes_string(x = x_var, y = y_var, color = group_var), |
|
|
83 |
method = 'lm', level = 0.95, size = line_size) |
|
|
84 |
|
|
|
85 |
# Add regression line and correlation label |
|
|
86 |
if (is.null(cor_vjust)) { |
|
|
87 |
plot <- plot + ggpubr::stat_cor(ggplot2::aes_string(color = group_var), method = cor_method, label.x.npc = cor_label_pos[1], label.y.npc = cor_label_pos[2]) |
|
|
88 |
} else { |
|
|
89 |
plot <- plot + ggpubr::stat_cor(ggplot2::aes_string(color = group_var), method = cor_method, label.x.npc = cor_label_pos[1], label.y.npc = cor_label_pos[2], vjust = cor_vjust) |
|
|
90 |
} |
|
|
91 |
|
|
|
92 |
|
|
|
93 |
# Customize scales and facet wrapping |
|
|
94 |
plot <- plot + |
|
|
95 |
ggplot2::scale_fill_manual(values = palette) + |
|
|
96 |
ggplot2::scale_color_manual(values = palette) + |
|
|
97 |
ggplot2::facet_wrap(stats::as.formula(paste0("~ ", facet_var)), scales = "free_x") + |
|
|
98 |
hrbrthemes::theme_ipsum() + |
|
|
99 |
ggplot2::theme(plot.margin = ggplot2::margin(10, 10, 10, 10), |
|
|
100 |
plot.background = ggplot2::element_rect(fill = "white", color = "white"), |
|
|
101 |
panel.spacing = grid::unit(2, "mm")) |
|
|
102 |
|
|
|
103 |
# Return the `ggplot` object |
|
|
104 |
return(plot) |
|
|
105 |
} |