Calcul des coefficients de Pearson et création du Heatmap

# Installer les packages nécessaires si pas encore installés
install.packages(c("readxl", "ggplot2", "reshape2", "pheatmap", "plotly"))
## Installing packages into '/cloud/lib/x86_64-pc-linux-gnu-library/4.4'
## (as 'lib' is unspecified)
# Charger les packages
library(readxl)
library(ggplot2)
library(reshape2)
library(pheatmap)
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
# Charger les données depuis le fichier Excel
cropping <- read_excel("biocontrol.xlsx", sheet=1)  
soil <- read_excel("biocontrol.xlsx", sheet=2)  
landscape <- read_excel("biocontrol.xlsx", sheet=3)  
seminatural <- read_excel("biocontrol.xlsx", sheet=4)  
aphids <- read_excel("biocontrol.xlsx", sheet=5)
yield <- read_excel("biocontrol.xlsx", sheet=12)


# Fusionner les données d'intérêt. Dans cet exemple, je m'intéresse aux coefficients de Pearson entre le nombre de pucerons et les caractéristiques du sol. Je joins donc les feuilles "soil" et "aphids" en une seule table
merged_data <- soil %>%
  full_join(aphids, by = "Farm_code")

# Sélectionner uniquement les colonnes numériques pour le calcul des corrélations
numeric_columns <- merged_data %>%
  select(where(is.numeric))

# Calculer la matrice de corrélation
correlation_matrix <- cor(numeric_columns, use = "complete.obs", method = "pearson")

# Convertir la matrice de corrélation en un format long pour ggplot2
melted_correlation_matrix <- melt(correlation_matrix)

# Créer la heatmap avec ggplot2
heatmap_plot <- ggplot(data = melted_correlation_matrix, aes(x = Var1, y = Var2, fill = value)) +
  geom_tile(color = "white") +
  scale_fill_gradient2(low = "blue", high = "red", mid = "white",
                       midpoint = 0, limit = c(-1, 1), space = "Lab",
                       name = "Pearson\nCorrelation") +
  theme_minimal() + 
  theme(axis.text.x = element_text(angle = 45, vjust = 1, size = 12, hjust = 1)) +
  coord_fixed() +
  geom_text(aes(label = sprintf("%.2f", value)), color = "black", size = 2)

# Affichez la heatmap
heatmap_plot

# Transformez l'objet ggplot2 en un objet plotly pour l'interactivité
interactive_heatmap <- ggplotly(heatmap_plot)

# Affichez la heatmap interactive
interactive_heatmap