##Correlation and regression analysis
library(tidyverse)
library(ggcorrplot)
library(tidyverse)
library(ggcorrplot)
library(corrplot)
library(readxl)
library(metan)
library(readxl)

Data <- AUGMENTED_DESIGN_DATA_FOR_ANALYSIS


print(Data)


# Generate a new dataset averaged by A	B	C	D	E	F	G	H	I	J	K	L	M	DS)
Data <- Data %>%
  group_by(genotype) %>%
  summarise(
    A = mean(A, na.rm = TRUE),
    B = mean(B, na.rm = TRUE),
    C = mean(C, na.rm = TRUE),
    D = mean(D, na.rm = TRUE),
    E = mean(E, na.rm = TRUE),
    `F` = mean(`F`, na.rm = TRUE),    
    G = mean(G, na.rm = TRUE),
    H = mean(H, na.rm = TRUE),
    I = mean(I, na.rm = TRUE),
    J = mean(J, na.rm = TRUE),
    K = mean(K, na.rm = TRUE),
    L = mean(L, na.rm = TRUE),
    M = mean(M, na.rm = TRUE),
    DS = mean(DS, na.rm = TRUE),
    .groups = 'drop'
  )

# View the result
print(Data)

# Saving the result to a CSV file
write.csv(Data, "Grouped_Summary.csv", row.names = FALSE)


#Relationship between quantitative variables

Data_Corr <- AUGMENTED_DESIGN_DATA_FOR_ANALYSIS
print(Data_Corr)

str(Data_Corr) #Structure of the data

# Select the relevant columns
selected_variables <- Data_Corr[, c("A", "B",	"C",	"D",	"E",	"F",	"G",	"H",	"I",	"J",	"K",	"L",	"M",	"DS")]

# Correlation matrix
correlation_matrix <- cor(selected_variables)


ALL1 <- corr_coef(selected_variables)

# Plot the correlation matrix with adjusted font size
Plot_ALL1 <- plot(ALL1, tl.cex = 5.0, number.cex = 5.0, cl.cex = 5.0)

Plot_ALL1

ggsave("Relationship_Plot.png", plot = Plot_ALL1, width = 16, height = 18, units = "cm", dpi = 300)




