先上图
需准备的数据
npgene.csv
理化end.CSV同上
library(psych)
library(pheatmap)
library(corrplot)
#导入数据
npgene <- as.data.frame(t(read.csv("npgene.csv",row.names = 1)))
feature <- as.data.frame(t(read.csv("理化end.CSV",row.names = 1)))
#制作函数data和data1代表要分析的数据表格,h,w表示格子高度和宽度默认15,40
correlation <- function(data = dataa,data1 = datab,h= 15,w=40){
data1 <- data1[match(rownames(data),rownames(data1)),]
mix <- data.frame(data1,data,check.names = F,check.rows = T)
result <- corr.test(mix, method= "spearman",adjust="fdr")
r<- as.data.frame(result$r)
p<- as.data.frame(result$p)
r<- r[- c(1:ncol(data1)),- c((ncol(data1)+1):ncol(r))]
p<- p[- c(1:ncol(data1)),- c((ncol(data1)+1):ncol(p))]
#p_top10 <- head(order(colSums(t(p)<0.05),decreasing = T),10)#获取前10的位置信息,ture==1 相加并排序
#p<- p[p_top10,]
#r<- r[p_top10,]
marking <- function(x){ifelse(x<0.001,"***",ifelse(x<0.01,"**",ifelse(x<0.05,"*","")))}
marker_data <- marking(p)
pheatmap(r,fontsize = 12,border_color="white",show_colnames =T,show_rownames =T,
cellwidth=w,cellheight=h,
main = "heatmap",display_numbers = marker_data,
angle_col = "45",
fontsize_numbe=15,
cluster_rows=F,cluster_cols =T,treeheight_col = 30,
clustering_distance_rows = "correlation",clustering_distance_cols = "euclidean",
clustering_method = "centroid",
color = colorRampPalette(c("purple","white","red"))(50),
cutree_cols = 2,cutree_rows = 4)
}
#直接出图
correlation(npgene,feature,h=15,w=38)