搜索
您的当前位置:首页正文

【R】数据相关性的可视化/热图 【Python】XY散点图拟合,添加R方,置信区间

来源:易榕旅网

.libPaths(c("/bioinfo/home/software/miniconda3/envs/R4.0/lib/R/library"))
#data("mtcars")
library("PerformanceAnalytics")
# pdf("test.pdf")
# my_data <- mtcars[, c(1,3,4,5,6,7)]
# print (head(my_data))
# chart.Correlation(my_data, histogram=TRUE, pch=19)
# dev.off()

args    <- commandArgs(trailingOnly = TRUE)
infile  <- args[1]
outdir <- args[2]
names<-basename(infile)
df <- read.delim(infile, header = T, stringsAsFactors = F,row.names = NULL)
#df <- df[,-ncol(df)]
print (df)
index <- row.names(df)
print(index)
# q()
# data_T=as.data.frame((data))
# df1 = anno_col[,"types",drop=FALSE]
pdf(paste0(outdir,"/",names,".correlation_chart.pdf"))
chart.Correlation(df, histogram=TRUE, pch=19,method='spearman')
dev.off()

输入:

热图

输入文件

library("pheatmap")


#args <- commandArgs(trailingOnly = TRUE)
#clusternum <- args[1]
#outdir <- args[2]

pdf(file.path('heatmap.pdf'))
data_frame <- read.table("heatmapFile.xls",sep="	",header=T,row.names = 1)
data_frame_sample <- data_frame[, 1:(ncol(data_frame)-1)]
groups1 <- data_frame[,c('clinic_label'),drop=FALSE]
p<-pheatmap(data_frame_sample,
            cluster_row = T,
            cluster_col=F,
            main="sample Heatmap",
            show_rownames = F,
            show_colnames=F,
            vmax = 0.6,
            #display_numbers = TRUE,
            annotation_row = groups1) #, cutree_cols = clusternum) fontsize_col=4
#col_cluster <- cutree(p$tree_col, k=clusternum)
#col_cluster = as.data.frame(col_cluster)
#print(col_cluster)
#write.csv(col_cluster, sep="\t", quote = FALSE, file.path(outdir, "cluster.csv"))
dev.off()

from sklearn.linear_model import LinearRegression
import numpy as np
import statsmodels.api as sm

def getLinearRegressio(x,y):
    slope,intercept, r_value, p_value, std_err=stats.linregress(x, y)
    if intercept <0:
        fangcheng = f'y={slope}*x{intercept}'
    elif intercept>=0: 
        fangcheng = f'y={slope}*x+{intercept}'
    R2 = r_value**2
    textR2 = f'R$^2$ ={R2}'
    ercc_num = x.shape[0]
    return slope,intercept, r_value, p_value, std_err, R2, fangcheng,textR2,ercc_num
    
def drawplot(x,y,data,fangcheng,textR2,ercc_num, outfile):
    sns.lmplot(x=x,y=y,data=data, aspect=1.5, ci=95)
    a,b = plt.ylim()
    print(a,b)
    y95 = np.percentile(data[y], 95)
    x5 = np.percentile(data[x], 5)
    plt.text(x5, y95, fangcheng, size=16, family="Times new roman", color="black", style='italic', weight = "light")
    y90 = np.percentile(data[y], 90)
    plt.text(x5, y90, textR2, size=16, family="Times new roman", color="black", style='italic', weight = "light")
    ercc_numtext = f'n={ercc_num}'
    y85 = np.percentile(data[y], 80)
    plt.text(x5, y85, ercc_numtext, size=16, family="Times new roman", color="black", style='italic', weight = "light")
    plt.savefig(outfile)
    return 

因篇幅问题不能全部显示,请点此查看更多更全内容

Top