如何创建一个充满数据集中两个变量之间相关性的热图函数?

How to create a heatmap function filled with the correlation between two variables from a data set?

我正在尝试创建一个函数,该函数计算我拥有的数据集中 2 列数据之间的相关系数,并为每个列组合重复此操作。

然后我希望它在热图中绘制所有系数。

This is an outline of the dataset and what I want to include in the heatmap.

我将如何编辑我的函数,以便它循环遍历数据集并能够计算所有列之间的相关系数并在热图中绘制值?我首先尝试创建一个全为 0 的空数据框,然后我希望它填充所有值。

master <- read.table("~/Desktop/Heatmap Project/master.txt", sep = "\t", header = T, stringsAsFactors = F)

vector_a <- master$Median_A
vector_b <- master$Median_B

heatmap_prep <- function(vector_a,vector_b){
    dummy <- as.data.frame(matrix(0, ncol=length(vector_b), nrow=length(vector_a))
    for (i in 1:length(vector_a)){
        first_number <- vector_a[i]
        for(j in 1:length(vector_b)){
            second_number <- vector_b[j]
            result <- cor(vector_a,vector_b)
            dummy [i,j] <- result

        }
    }
    return(dummy)
}

heatmap_data_matrix <- as.matrix(heatmap_prep(vector_a,vector_b))

#Create heatmap:
library(stats)
library(gplots)
library(RColorBrewer)
heatmap(heatmap_data_matrix,Colv = NA, Rowv=NA, revC=T, scale='none', xlab= "B", ylab= "A", main = "Heatmap", col = rev(brewer.pal(11,"RdBu")))

非常感谢!

以下代码应根据您提供的内容提供一个最小的工作示例。

df <- data.frame("A" = c(12,13,15),
                 "B" = c(15,34,15),
                 "C" = c(16,34,56),
                 "D" = c(455,55,45),
                 "E" = c(78,67,65),
                 "F" = c(67,67,56),
                 "G" = c(67,45,64),
                 "H" = c(56,54,56),
                 "I" = c(56,89,90))

library(reshape2)
melted_cor <- melt(cor(df))
library(ggplot2)
ggplot(data = melted_cor, aes(x=X1, y=X2, fill=value)) + 
  geom_tile()

这里有更详细的解释: http://www.sthda.com/english/wiki/ggplot2-quick-correlation-matrix-heatmap-r-software-and-data-visualization