HeatMap with clustering and different colors

library(readxl)
library(pheatmap)
library(gplots)
library(grDevices)

windowsFonts(Times = grDevices::windowsFont("Times New Roman"))
data <- read_excel("D:/Projects/Dr Rabab HeatMap&PCA/New PCA with R/Data (3).xlsx")
data_matrix <- as.data.frame(data)           
rownames(data_matrix) <- data_matrix[[1]]   
data_matrix <- data_matrix[,-1]             
data_matrix <- t(as.matrix(data_matrix))      

pheatmap(data_matrix,
         scale = "row",
         fontsize_row = 10,
         fontsize_col = 10,
         fontfamily = "Times",
         cellheight = 17,
         cellwidth = 25,
         treeheight_row = 15,
         treeheight_col = 15,
         border_color = NA
)

PCA Analysis

library(ggplot2)
library(ggbiplot)
library(devtools)
library(factoextra)

newmatrix <- t(data_matrix)

pca_result <- prcomp(newmatrix, center = TRUE, scale. = TRUE)

summary(pca_result)
## Importance of components:
##                           PC1    PC2    PC3     PC4     PC5     PC6     PC7
## Standard deviation     2.9654 1.9116 1.6150 1.01808 0.81874 0.35779 0.33004
## Proportion of Variance 0.5173 0.2150 0.1534 0.06097 0.03943 0.00753 0.00641
## Cumulative Proportion  0.5173 0.7322 0.8857 0.94663 0.98606 0.99359 1.00000
##                              PC8
## Standard deviation     1.161e-15
## Proportion of Variance 0.000e+00
## Cumulative Proportion  1.000e+00
fviz_eig(pca_result, addlabels = TRUE, ylim = c(0, 100) ) + labs(title = "Scree Plot of PCA", x = "Principal Components", y = "Explained Variance (%)") + theme_minimal()

fviz_pca_ind(pca_result,
             col.ind = "cos2",
             gradient.cols = c("blue", "red"),
             repel = TRUE)

fviz_pca_var(
            pca_result,
            col.var = "contrib",
            gradient.cols = c("blue", "red"),
            repel = TRUE)

fviz_pca_biplot(pca_result,
                repel = TRUE,         
                label = "all",        
                title = "PCA Biplot: Treatments and Parameters") +
  theme_minimal()

round(pca_result$rotation, 3)  
##                    PC1    PC2    PC3    PC4    PC5    PC6    PC7    PC8
## Plant height     0.314  0.052 -0.116  0.072  0.210  0.431  0.493 -0.279
## Leaf width       0.309  0.046 -0.040 -0.367  0.116 -0.084  0.054 -0.107
## Leaf length      0.321 -0.088 -0.057 -0.095 -0.145 -0.488  0.089 -0.176
## Leaf No.         0.298  0.170 -0.145  0.094 -0.272 -0.017  0.123  0.023
## Leaf area        0.323 -0.055  0.058 -0.142 -0.234  0.084 -0.170 -0.094
## Petiole length   0.297  0.024 -0.177  0.098  0.440  0.018 -0.058  0.312
## Total Ch        -0.188  0.410  0.101 -0.126 -0.109 -0.439  0.052 -0.056
## Crude fibers    -0.097  0.389  0.351 -0.053 -0.035  0.054  0.602  0.291
## Crude protein    0.123  0.181  0.316  0.671  0.104 -0.314 -0.049 -0.163
## Ascorbic acid   -0.174  0.393 -0.101  0.196  0.368  0.202 -0.267 -0.294
## Proline content -0.225  0.341 -0.043 -0.281 -0.222  0.203 -0.199 -0.112
## K                0.150  0.179 -0.502  0.142 -0.019 -0.213 -0.081  0.350
## Fe               0.005  0.420 -0.366 -0.074 -0.018  0.035 -0.076  0.167
## P                0.245  0.053  0.369  0.165 -0.279  0.324 -0.350  0.414
## Oxalic          -0.163 -0.085 -0.374  0.411 -0.534  0.156  0.207 -0.156
## Phenols          0.292  0.241  0.098 -0.035 -0.126  0.072  0.032  0.081
## Flavonoids       0.294  0.235  0.097 -0.049 -0.107  0.064 -0.204 -0.455

Description:

Principal Component Analysis (PCA) was performed to reduce the dimensionality of the measured parameters and to explore treatment-related variation. The first two principal components (PC1 and PC2) explained 51.7% and 21.5% of the total variance, respectively (las figure).

PC1 had high positive loadings from leaf area (0.323), leaf length (0.321), plant height (0.314), leaf width (0.309), and number of leaves (0.298), indicating it predominantly represents vegetative growth traits. In contrast, PC2 was positively influenced by total chlorophyll (0.410), ascorbic acid (0.393), crude fibers (0.389), and Fe content (0.420), which suggests an axis associated with antioxidant conent and fiber increment for enhanced protection from the elements.

Negative loadings in PC1 from proline content (-0.225) and oxalic acid (-0.163) may reflect stress-related traits, inversely associated with vegetative growth. Likewise, crude protein (0.671) dominated PC3, while oxalic acid (0.411) and flavonoids (-0.455) contributed significantly to PC4.

The PCA biplot (lst figure) revealed distinct clustering of treatments, reflecting their differential influence on both vegetative and antioxidant/protective traits. Treatments positioned closer together were more similar in their overall profiles, while those separated along PC1 or PC2 differed significantly in growth or nutrient composition.