# Extract Correlation Parameterslibrary(dplyr)library(broom)library(tidyr)# Function to perform correlation test and return tidy resultscor_test_tidy <-function(data, x, y, region =NULL) { cor_test <-cor.test(data[[x]], data[[y]], method ="spearman", exact =FALSE) tidy_result <-tidy(cor_test) %>%mutate(x_var = x,y_var = y,region = region ) %>%select(region, x_var, y_var, estimate, p.value)return(tidy_result)}# Function to extract correlation parametersextract_correlations <-function(data, x_var, y_var, region_col ="Region", region_order =c("EI", "RR", "HM", "Overall")) { correlation_table <-bind_rows( data %>%group_by(!!sym(region_col)) %>%group_modify(~cor_test_tidy(.x, x_var, y_var, .y[[region_col]])),cor_test_tidy(data, x_var, y_var, "Overall") ) %>%arrange(factor(region, levels = region_order))return(correlation_table)}# Create a table for each correlations : correlation_table_1 <-extract_correlations(data, "DHg_Mean", "DOC_Mean") correlation_table_2 <-extract_correlations(data, "DHg_Mean", "C1") correlation_table_3 <-extract_correlations(data, "DHg_Mean", "C4") correlation_table_4 <-extract_correlations(data, "DMeHg_DHg_ratio", "DOC_Mean") correlation_table_5 <-extract_correlations(data, "DMeHg_DHg_ratio", "C1") correlation_table_6 <-extract_correlations(data, "DMeHg_DHg_ratio", "C4") correlation_table_7 <-extract_correlations(data, "DMeHg_DHg_DMeHG_ratio","Cgas_DOC_ratio") correlation_table_8 <-extract_correlations(data, "DMeHg_Mean", "CH4_plus_MOX") correlation_table_9 <-extract_correlations(data, "DMeHg_Mean", "CH4_mgCL")#Combine correlation_table_all=rbind(correlation_table_1, correlation_table_2,correlation_table_3, correlation_table_4,correlation_table_5,correlation_table_6, correlation_table_7,correlation_table_8, correlation_table_9)# Display the table#datatable(correlation_table_all)# Create a label combining x and y variablescorrelation_table_all_2 <- correlation_table_all %>%mutate(label =paste(x_var, y_var, sep =" vs "))# Create size categories based on p-valuecorrelation_table_all_2 <- correlation_table_all_2 %>%mutate(p_value_size_cat =case_when( p.value <0.001~"<0.001", p.value <0.01~"<0.01", p.value <0.05~"<0.05",TRUE~"N.S." ))# Create a separate dataframe for overall estimatesoverall_estimates <- correlation_table_all_2 %>%filter(region =="Overall") %>%#Keep only overall estimatesarrange(estimate) %>%#arrange it in ascendent ordermutate(order =row_number()) #apply this order to the row number# Join the order back to the main dataframecorrelation_table_all_2 <- correlation_table_all_2 %>%left_join(overall_estimates %>%select(label, order), by ="label") #combine the ordered estimates with the complete correlation table based on the labels# Reorder the region factorcorrelation_table_all_2 <- correlation_table_all_2 %>%mutate(region =factor(region, levels =c("EI", "HM", "RR", "Overall"))) # make sure the region order is EI, HM, RR, Overall# Create a new column with formatted labelscorrelation_table_all_2 <- correlation_table_all_2 %>%mutate(label_formatted =case_when( label =="DHg_Mean vs C1"~"[Hg] vs C1(%)", label =="DHg_Mean vs C4"~"[Hg] vs C4(%)", label =="DHg_Mean vs DOC_Mean"~"[Hg] vs [DOC]", label =="DMeHg_DHg_DMeHG_ratio vs Cgas_DOC_ratio"~"MeHg:Hg vs Cgas:DOC", label =="DMeHg_DHg_ratio vs C1"~"MeHg:Hg vs C1(%)", label =="DMeHg_DHg_ratio vs C4"~"MeHg:Hg vs C4(%)", label =="DMeHg_DHg_ratio vs DOC_Mean"~"MeHg:Hg vs [DOC]", label =="DMeHg_Mean vs CH4_mgCL"~"[MeHg] vs [CH4]", label =="DMeHg_Mean vs CH4_plus_MOX"~"[MeHg] vs [CH4 + MOX]",TRUE~ label # Keep original label if not specified above ))# Create the lollipop plotlollipop=ggplot(correlation_table_all_2, aes(x = estimate, y =reorder(label_formatted, order))) +geom_segment(aes(x =0, xend = estimate, yend = label_formatted), color ="black") +geom_point(aes(fill = region, size = p_value_size_cat), alpha =0.7, shape =21) +geom_point(aes(size = p_value_size_cat), color ="black", shape =21) +geom_vline(xintercept =0, linetype ="dashed", color ="black") +scale_fill_manual(values =c(colorramp, "black")) +scale_size_discrete(range =c(6, 2)) +scale_x_continuous(limits=c(-0.8,0.8),breaks =seq(-1, 1, 0.2)) +theme_minimal(base_size =16) +theme(axis.text.x =element_text(color ="black"), # Also changed x-axis text to black for consistencyaxis.text.y =element_text(angle =0, hjust =1, color ="black"),legend.position ="top",plot.title =element_text(size =20)) +labs(x ="Spearman rho", y ="", fill ="Region", size ="p-value")+guides(fill ="none", # Increase size for fill legendsize =guide_legend(override.aes =list(fill ="black")) # Make size legend points black )ggplotly(lollipop, tooltip =c("x","size"))