# Load necessary packages library(ggplot2) library(reshape2) # Create correlation matrix cor_matrix <- matrix(c(1.000, 0.505, 0.530, 0.386, 0.505, 1.000, 0.280, 0.442, 0.530, 0.280, 1.000, 0.234, 0.386, 0.442, 0.234, 1.000), nrow = 4, byrow = TRUE) # Set variable names as NLR, PLR, MLR, SII rownames(cor_matrix) <- c("NLR", "PLR", "MLR", "SII") colnames(cor_matrix) <- c("NLR", "PLR", "MLR", "SII") # Convert matrix to long format cor_data <- melt(cor_matrix) # Add significance symbols (assuming all P < 0.01, except for the diagonal) cor_data$sig <- ifelse(cor_data$Var1 != cor_data$Var2, "**", "") # Create heatmap ggplot(cor_data, aes(Var1, Var2, fill = value)) + geom_tile() + # Create tile plot geom_text(aes(label = paste0(sprintf("%.2f", value), sig)), vjust = 1) + # Add correlation coefficients and significance symbols scale_fill_gradient2(low = "blue", high = "red", mid = "white", midpoint = 0, limit = c(-1, 1), space = "Lab", name = "Spearman\nCorrelation") + # Set color gradient theme_minimal() + # Use minimal theme theme(axis.text.x = element_text(angle = 45, vjust = 1, size = 12, hjust = 1), axis.title.x = element_blank(), # Remove x-axis title axis.title.y = element_blank()) + # Remove y-axis title coord_fixed() + # Fix coordinate ratio labs(title = "Spearman Correlation Heatmap of Systemic Inflammation Indices") # Add title # Save plot as PNG file ggsave("correlation_heatmap_with_sig.png", width = 8, height = 6)