# ========================================================= # CORRELATION HEATMAP — LIME (Pooled Replicates) # Two styles: corrplot + ggplot # ========================================================= # ----------------------------- # 1. Install packages (run once) # ----------------------------- # install.packages(c("tidyverse", "corrplot", "reshape2", "ggplot2")) # ----------------------------- # 2. Load libraries # ----------------------------- library(tidyverse) library(corrplot) library(reshape2) library(ggplot2) # ----------------------------- # 3. Import data # ----------------------------- data <- read.csv("Lime_Pooled_Data.csv", header = TRUE, stringsAsFactors = FALSE) # ----------------------------- # 4. Rename columns to full names # ----------------------------- data <- data %>% rename( `Number of Fruits` = NoFruits, `Fruit Weight (g)` = FruitWt_g, `Juice Content (%)` = JuiceContent_pct, `Shoot Length (cm)` = ShootLen_cm, `Number of Shoots` = NoShoots, `Number of Leaves` = NoLeaves, `Leaf Area (cm²)` = LeafArea_cm2, `Chlorophyll (SPAD)` = SPAD, `Days to Flowering` = DaysToFlower, `Number of Flowers` = NoFlowers, `Total Soluble Solids (°Brix)` = TSS_pct, `Acidity (%)` = Acidity_pct ) # ----------------------------- # 5. Numeric data for analysis # (Replication INCLUDED) # ----------------------------- numeric_all <- data %>% select(where(is.numeric)) # ----------------------------- # 6. Compute Pearson correlation # ----------------------------- cor_matrix <- cor(numeric_all, method = "pearson", use = "complete.obs") # ----------------------------- # 7. Remove Replication ONLY from display # ----------------------------- cor_matrix_plot <- cor_matrix[ !rownames(cor_matrix) %in% "Replication", !colnames(cor_matrix) %in% "Replication" ] print(round(cor_matrix_plot, 3)) # ========================================================= # HEATMAP 1 — CORRPLOT (blue–white–red) # ========================================================= png("Correlation_Heatmap_corrplot_bluered.png", width = 3000, height = 2400, res = 300) corrplot(cor_matrix_plot, method = "color", type = "upper", order = "hclust", addCoef.col = "black", # values in black tl.col = "black", tl.srt = 45, number.cex = 0.6, col = colorRampPalette(c("#2166AC", "white", "#B2182B"))(200)) dev.off() # ========================================================= # HEATMAP 2 — GGPLOT (same blue–white–red family) # ========================================================= # Melt matrix cor_melt <- melt(cor_matrix_plot) heatmap_plot <- ggplot(cor_melt, aes(x = Var1, y = Var2, fill = value)) + geom_tile(color = "grey85") + geom_text(aes(label = sprintf("%.2f", value)), color = "black", size = 3) + scale_fill_gradient2( low = "#2166AC", mid = "white", high = "#B2182B", midpoint = 0, limit = c(-1, 1), name = "Pearson r" ) + theme_minimal(base_size = 12) + theme( axis.text.x = element_text(angle = 45, hjust = 1), panel.grid = element_blank(), plot.title = element_text(face = "bold", hjust = 0.5) ) + coord_fixed() + labs(title = "Correlation Heatmap of Lime Traits", x = "", y = "") # Display print(heatmap_plot) # Save ggplot version ggsave("Correlation_Heatmap_ggplot_bluered.png", plot = heatmap_plot, width = 10, height = 8, dpi = 300) # ========================================================= # END # =========================================================