From d5da923a745112b8aaf0d9122754799217c12d8d Mon Sep 17 00:00:00 2001 From: Tanushree Tunstall Date: Sun, 28 Aug 2022 13:16:46 +0100 Subject: [PATCH] stuff --- .../plotting_thesis/alr/gg_pairs_all_alr.R | 125 ++++++++++++++---- 1 file changed, 102 insertions(+), 23 deletions(-) diff --git a/scripts/plotting/plotting_thesis/alr/gg_pairs_all_alr.R b/scripts/plotting/plotting_thesis/alr/gg_pairs_all_alr.R index 1b28902..2236258 100644 --- a/scripts/plotting/plotting_thesis/alr/gg_pairs_all_alr.R +++ b/scripts/plotting/plotting_thesis/alr/gg_pairs_all_alr.R @@ -1,6 +1,6 @@ -#source("~/git/LSHTM_analysis/config/alr.R") -#source("~/git/LSHTM_analysis/scripts/plotting/get_plotting_dfs.R") -#source("~/git/LSHTM_analysis/scripts/plotting/plotting_colnames.R") +source("~/git/LSHTM_analysis/config/alr.R") +source("~/git/LSHTM_analysis/scripts/plotting/get_plotting_dfs.R") +source("~/git/LSHTM_analysis/scripts/plotting/plotting_colnames.R") #======= # output @@ -8,14 +8,31 @@ #outdir_images = paste0("/home/pub/Work/LSHTM/Thesis_Plots/pairs/") #cat("plots will output to:", outdir_images) -custom_cor <- function(data, mapping, method, ...){ +custom_cor <- function( + data, + mapping, + method, + digits=2, + use="complete.obs", + stars=TRUE, + ... +){ x <- eval_data_col(data, mapping$x) y <- eval_data_col(data, mapping$y) - corr <- cor(x, y, method=method, use='complete.obs') - + corr_obj <- stats::cor.test(x, y, method=method, use=use) + cor_est <- as.numeric(corr_obj$estimate) + cor_txt <- formatC(cor_est, digits = digits, format = "f") + + cor_txt = paste0("ρ: ",as.character(round(cor_txt, 2))) + if (isTRUE(stars)) { + cor_txt <- str_c(cor_txt, signif_stars(corr_obj$p.value)) + } + corr_obj$p.value + cor_txt + ggally_text( - label = paste0("ρ: ",as.character(round(corr, 2))), + label = cor_txt, mapping = aes(), xP = 0.5, yP = 0.5, color = 'black', @@ -24,22 +41,83 @@ custom_cor <- function(data, mapping, method, ...){ } +custom_corr2=function (data, mapping, ..., stars = TRUE, method = "pearson", + use = "complete.obs", display_grid = FALSE, digits = 3, + title_args = list(...), group_args = list(...), justify_labels = "right", + align_percent = 0.5, title = "ρ", alignPercent = warning("deprecated. Use `align_percent`"), + displayGrid = warning("deprecated. Use `display_grid`")) +{ + if (!missing(alignPercent)) { + warning("`alignPercent` is deprecated. Please use `align_percent` if alignment still needs to be adjusted") + align_percent <- alignPercent + } + if (!missing(displayGrid)) { + warning("`displayGrid` is deprecated. Please use `display_grid`") + display_grid <- displayGrid + } + na.rm <- if (missing(use)) { + NA + } + else { + (use %in% c("complete.obs", "pairwise.complete.obs", + "na.or.complete")) + } + + + + ggally_statistic(data = data, + mapping = mapping, + na.rm = na.rm, + align_percent = align_percent, + display_grid = display_grid, + title_args = title_args, + group_args = group_args, + justify_labels = justify_labels, + justify_text = "left", + sep = if ("colour" %in% names(mapping)) + ": " + else ": ", title = title, text_fn = function(x, y) { + # if (is_date(x)) { + # x <- as.numeric(x) + # } + # if (is_date(y)) { + # y <- as.numeric(y) + # } + corObj <- stats::cor.test(x, y, method = method, + use = use) + cor_est <- as.numeric(corObj$estimate) + cor_txt <- formatC(cor_est, digits = digits, format = "f") + if (isTRUE(stars)) { + cor_txt <- str_c(cor_txt, signif_stars(corObj$p.value)) + } + cor_txt + }) +} -my_gg_pairs=function(plot_df, plot_title - , tt_args_size = 2.5 - , gp_args_size = 2.5){ + + + + +my_gg_pairs=function( + plot_df, + plot_title + , tt_args_size = 5 + , gp_args_size = 5 +){ ggpairs(plot_df, columns = 1:(ncol(plot_df)-1), upper = list( - continuous = wrap(custom_cor, method="spearman" - # ggally_cor, # ggally_cor() - # method = "spearman", - # use = "pairwise.complete.obs", - # title="XXX ρ", - # digits=2, - # justify_labels = "centre", - # title_args=list(size=tt_args_size, colour="black")#,#2.5 - # #group_args=list(size=gp_args_size)#2.5 + continuous = wrap( + #ustom_cor, method="spearman" + custom_corr2, # ggally_cor() + method = "spearman", + use = "pairwise.complete.obs", + #title="XXX ", + digits=2, + justify_labels = "centre", + title_args=list(size=tt_args_size, colour="black")#,#2.5 + #group_args=list(size=gp_args_size)#2.5 + , size=10 ) ), lower = list( @@ -80,7 +158,7 @@ corr_plotdf = corr_data_extract(merged_df3 aff_dist_cols = colnames(corr_plotdf)[grep("Dist", colnames(corr_plotdf))] static_cols = c("Log10(MAF)" - #, "Log10(OR)" + # , "Log10(OR)" ) ############################################################ #============================================= @@ -175,12 +253,13 @@ plot_corr_df_aff = my_gg_pairs(corr_df_aff #, gp_args_size = 4 ) + #### Combine plots ##### # #png("/home/tanu/tmp/gg_pairs_all.png", height = 6, width=11.75, unit="in",res=300) # png(paste0(outdir_images # ,tolower(gene) # ,"_CorrAB.png"), height = 6, width=11.75, unit="in",res=300) -# +# # cowplot::plot_grid(ggmatrix_gtable(plot_corr_df_ps), # ggmatrix_gtable(plot_corr_df_cons), # # ggmatrix_gtable(plot_corr_df_aff), @@ -190,13 +269,13 @@ plot_corr_df_aff = my_gg_pairs(corr_df_aff # labels = "AUTO", # label_size = 12) # dev.off() -# +# # # affinity corr # #png("/home/tanu/tmp/gg_pairs_affinity.png", height =7, width=7, unit="in",res=300) # png(paste0(outdir_images # ,tolower(gene) # ,"_CorrC.png"), height =7, width=7, unit="in",res=300) -# +# # cowplot::plot_grid(ggmatrix_gtable(plot_corr_df_aff), # labels = "C", # label_size = 12)