美文网首页生物信息学
相关性计算与可视化画

相关性计算与可视化画

作者: bioYIYI | 来源:发表于2021-11-18 09:51 被阅读0次

    用途:

    计算组间相关性,并进行可视化

    1.一组数据,单个矩阵

    使用示例:

    Rscript corrplot.r correlation.format_data_rename/SampleID_correlation.format.txt COR/SampleID
    

    输入数据示例:

    image.png

    代码

    1,输出相关性矩阵,并用ggplot2画热图

    args = commandArgs(T)
    if (length(args) !=2){
        print("Rscript this R <infile> <pre_outfile>")
        q()
    }
    library(ggplot2)
    library(reshape2)
    library(pheatmap)
    
    data <- read.table(args[1],header=T,sep="\t",stringsAsFactors=FALSE,row.names =1)
    corr <- cor(data)
    out <- data.frame(sample=rownames(corr),as.data.frame(corr))
    write.table(out,paste(args[2],".correlation.txt",sep=""),row.names =F,col.names =T,quote =F,sep="\t")
    corr_met <- melt(corr)
    out <- data.frame(sample=rownames(corr_met),as.data.frame(corr_met))
    write.table(out,paste(args[2],".correlation2.txt",sep=""),row.names =F,col.names =T,quote =F,sep="\t")
    ggplot(corr_met,aes(x=Var1,y=Var2,fill=value))+geom_tile()+scale_fill_continuous(low="blue",high="red")+geom_text(aes(label=round(value,3)),angle=0,size=3)+theme_classic()+theme(axis.line = element_blank())+theme(axis.text.x = element_text(size = 10,angle = 90),axis.text.y = element_text(size = 10,angle = 0))+labs(x="",y="",title = "Pearson Correlation Coefficient")+theme(plot.title = element_text(hjust = 0.5))+guides(fill = guide_legend(title = expression("R"),reverse = TRUE))
    ggsave(paste(args[2],".correlation_heatmap.pdf",sep = ""),width = 5,height =5)
    

    2,输出相关性矩阵,并用corrplot画热图

    args = commandArgs(T)
    if (length(args) !=2){
        print("Rscript this R <infile> <pre_outfile>")
        q()
    }
    library("corrplot")
    library(reshape2)
    
    data <- read.table(args[1],header=T,sep="\t",stringsAsFactors=FALSE,row.names =1)
    corr <- cor(data)
    out <- data.frame(sample=rownames(corr),as.data.frame(corr))
    write.table(out,paste(args[2],".correlation.txt",sep=""),row.names =F,col.names =T,quote =F,sep="\t")
    corr_met <- melt(corr)
    out <- data.frame(sample=rownames(corr_met),as.data.frame(corr_met))
    write.table(out,paste(args[2],".correlation2.txt",sep=""),row.names =F,col.names =T,quote =F,sep="\t")
    
    pdf(paste(args[2],".correlation.pdf",sep = ""),width = 6,height = 6)
    color<-colorRampPalette(c("blue", "red"))(200)
    corrplot(corr=corr,order = "AOE",type="upper",tl.pos = "d",col=color)
    #如果想画饼图用以下代码
    #corrplot(corr=corr,order = "AOE",type="upper",tl.pos = "d",col=color,method = "pie")
    corrplot(corr = corr,add=TRUE, type="lower", method="number",order="AOE",diag=FALSE,tl.pos="n", cl.pos="n")
    dev.off()
    

    结果示例

    *.correlation2.txt:


    image.png

    *.correlation.txt:


    image.png

    ggplot可视化:


    image.png

    corrplot可视化:


    image.png image.png

    2.多组数据,多个矩阵组合

    1)组合方式1

    ggplot绘制的结果进行组合:

    library(ggplot2)
    library(reshape2)
    library(pheatmap)
    library(ggpubr)
    data = read.table("/*/COR//Pt17.correlation2.txt",header = T)
    p1 = ggplot(data,aes(x=Var1,y=Var2,fill=value))+geom_tile()+scale_fill_continuous(low="blue",high="red")+geom_text(aes(label=round(value,3)),angle=0,size=3)+theme_classic()+theme(axis.line = element_blank())+theme(axis.text.x = element_text(size = 10,angle = 90),axis.text.y = element_text(size = 10,angle = 0))+labs(x="",y="",title = "Pearson Correlation Coefficient")+theme(plot.title = element_text(hjust = 0.5))+guides(fill = guide_legend(title = expression("R"),reverse = TRUE)) 
    data = read.table("/*/COR//Pt17.correlation2.txt",header = T)
    p2 = ggplot(data,aes(x=Var1,y=Var2,fill=value))+geom_tile()+scale_fill_continuous(low="blue",high="red")+geom_text(aes(label=round(value,3)),angle=0,size=3)+theme_classic()+theme(axis.line = element_blank())+theme(axis.text.x = element_text(size = 10,angle = 90),axis.text.y = element_text(size = 10,angle = 0))+labs(x="",y="",title = "Pearson Correlation Coefficient")+theme(plot.title = element_text(hjust = 0.5))+guides(fill = guide_legend(title = expression("R"),reverse = TRUE)) 
    data = read.table("/*/COR//Pt17.correlation2.txt",header = T)
    p3 = ggplot(data,aes(x=Var1,y=Var2,fill=value))+geom_tile()+scale_fill_continuous(low="blue",high="red")+geom_text(aes(label=round(value,3)),angle=0,size=3)+theme_classic()+theme(axis.line = element_blank())+theme(axis.text.x = element_text(size = 10,angle = 90),axis.text.y = element_text(size = 10,angle = 0))+labs(x="",y="",title = "Pearson Correlation Coefficient")+theme(plot.title = element_text(hjust = 0.5))+guides(fill = guide_legend(title = expression("R"),reverse = TRUE)) 
    data = read.table("/*/COR//Pt75.correlation2.txt",header = T)
    p4 = ggplot(data,aes(x=Var1,y=Var2,fill=value))+geom_tile()+scale_fill_continuous(low="blue",high="red")+geom_text(aes(label=round(value,3)),angle=0,size=3)+theme_classic()+theme(axis.line = element_blank())+theme(axis.text.x = element_text(size = 10,angle = 90),axis.text.y = element_text(size = 10,angle = 0))+labs(x="",y="",title = "Pearson Correlation Coefficient")+theme(plot.title = element_text(hjust = 0.5))+guides(fill = guide_legend(title = expression("R"),reverse = TRUE)) 
    data = read.table("/*/COR//Pt92.correlation2.txt",header = T)
    p5 = ggplot(data,aes(x=Var1,y=Var2,fill=value))+geom_tile()+scale_fill_continuous(low="blue",high="red")+geom_text(aes(label=round(value,3)),angle=0,size=3)+theme_classic()+theme(axis.line = element_blank())+theme(axis.text.x = element_text(size = 10,angle = 90),axis.text.y = element_text(size = 10,angle = 0))+labs(x="",y="",title = "Pearson Correlation Coefficient")+theme(plot.title = element_text(hjust = 0.5))+guides(fill = guide_legend(title = expression("R"),reverse = TRUE)) 
    data = read.table("/*/COR//Pt96.correlation2.txt",header = T)
    p6 = ggplot(data,aes(x=Var1,y=Var2,fill=value))+geom_tile()+scale_fill_continuous(low="blue",high="red")+geom_text(aes(label=round(value,3)),angle=0,size=3)+theme_classic()+theme(axis.line = element_blank())+theme(axis.text.x = element_text(size = 10,angle = 90),axis.text.y = element_text(size = 10,angle = 0))+labs(x="",y="",title = "Pearson Correlation Coefficient")+theme(plot.title = element_text(hjust = 0.5))+guides(fill = guide_legend(title = expression("R"),reverse = TRUE)) 
    data = read.table("/*/COR//Pt107.correlation2.txt",header = T)
    p7 = ggplot(data,aes(x=Var1,y=Var2,fill=value))+geom_tile()+scale_fill_continuous(low="blue",high="red")+geom_text(aes(label=round(value,3)),angle=0,size=3)+theme_classic()+theme(axis.line = element_blank())+theme(axis.text.x = element_text(size = 10,angle = 90),axis.text.y = element_text(size = 10,angle = 0))+labs(x="",y="",title = "Pearson Correlation Coefficient")+theme(plot.title = element_text(hjust = 0.5))+guides(fill = guide_legend(title = expression("R"),reverse = TRUE)) 
    data = read.table("/*/COR//Pt109.correlation2.txt",header = T)
    p8 = ggplot(data,aes(x=Var1,y=Var2,fill=value))+geom_tile()+scale_fill_continuous(low="blue",high="red")+geom_text(aes(label=round(value,3)),angle=0,size=3)+theme_classic()+theme(axis.line = element_blank())+theme(axis.text.x = element_text(size = 10,angle = 90),axis.text.y = element_text(size = 10,angle = 0))+labs(x="",y="",title = "Pearson Correlation Coefficient")+theme(plot.title = element_text(hjust = 0.5))+guides(fill = guide_legend(title = expression("R"),reverse = TRUE)) 
    data = read.table("/*/COR//Pt114.correlation2.txt",header = T)
    p9 = ggplot(data,aes(x=Var1,y=Var2,fill=value))+geom_tile()+scale_fill_continuous(low="blue",high="red")+geom_text(aes(label=round(value,3)),angle=0,size=3)+theme_classic()+theme(axis.line = element_blank())+theme(axis.text.x = element_text(size = 10,angle = 90),axis.text.y = element_text(size = 10,angle = 0))+labs(x="",y="",title = "Pearson Correlation Coefficient")+theme(plot.title = element_text(hjust = 0.5))+guides(fill = guide_legend(title = expression("R"),reverse = TRUE)) 
    data = read.table("/*/COR//Pt116.correlation2.txt",header = T)
    p10 = ggplot(data,aes(x=Var1,y=Var2,fill=value))+geom_tile()+scale_fill_continuous(low="blue",high="red")+geom_text(aes(label=round(value,3)),angle=0,size=3)+theme_classic()+theme(axis.line = element_blank())+theme(axis.text.x = element_text(size = 10,angle = 90),axis.text.y = element_text(size = 10,angle = 0))+labs(x="",y="",title = "Pearson Correlation Coefficient")+theme(plot.title = element_text(hjust = 0.5))+guides(fill = guide_legend(title = expression("R"),reverse = TRUE)) 
    data = read.table("/*/COR//Pt120.correlation2.txt",header = T)
    p11 = ggplot(data,aes(x=Var1,y=Var2,fill=value))+geom_tile()+scale_fill_continuous(low="blue",high="red")+geom_text(aes(label=round(value,3)),angle=0,size=3)+theme_classic()+theme(axis.line = element_blank())+theme(axis.text.x = element_text(size = 10,angle = 90),axis.text.y = element_text(size = 10,angle = 0))+labs(x="",y="",title = "Pearson Correlation Coefficient")+theme(plot.title = element_text(hjust = 0.5))+guides(fill = guide_legend(title = expression("R"),reverse = TRUE)) 
    data = read.table("/*/COR//Pt127.correlation2.txt",header = T)
    p12 = ggplot(data,aes(x=Var1,y=Var2,fill=value))+geom_tile()+scale_fill_continuous(low="blue",high="red")+geom_text(aes(label=round(value,3)),angle=0,size=3)+theme_classic()+theme(axis.line = element_blank())+theme(axis.text.x = element_text(size = 10,angle = 90),axis.text.y = element_text(size = 10,angle = 0))+labs(x="",y="",title = "Pearson Correlation Coefficient")+theme(plot.title = element_text(hjust = 0.5))+guides(fill = guide_legend(title = expression("R"),reverse = TRUE)) 
    ggarrange(p1,p2,p3,p4,p5,p6,p7,p8,p9,p10,p11,p12,ncol=4,nrow=3) 
    ggsave(paste("../IM.txt.correlation_heatmap.pdf",sep = ""),width = 20,height =15) 
    

    结果示例:


    image.png

    2)组合方式2

    ggplot绘制的结果进行组合:

    library(ggplot2)
    library(reshape2)
    library(pheatmap)
    library(ggpubr)
    IM<-read.table("IM.format",header = T)
    MPLC<-read.table("MPLC.format",header = T)
    COHORT2<-read.table("COHORT2.format",header = T)
    
    
    pdf(paste("2/IM.correlation.pdf",sep = ""),width = 30,height =20,onefile=FALSE)
    ggplot(IM,aes(x=Var1,y=Var2,fill=value))+geom_tile()+scale_x_discrete(breaks=NULL)+scale_y_discrete(breaks=NULL)+scale_fill_gradient2(mid="white",high="red",low="blue",midpoint=-0.2)+labs(x="",y="",title = "IM Pearson_Correlation_Coefficient")+theme( plot.title = element_text(size=30,hjust = 0.5),legend.title=element_text(size=30),legend.text=element_text(size=30), panel.background=element_blank(),strip.text=element_text(size=25) )+guides(fill = guide_legend(title = expression("R"),reverse = TRUE))+facet_wrap(~patient, ncol=3,scales="free")
    dev.off()
    
    pdf(paste("2/MPLC.correlation.pdf",sep = ""),width = 30,height =20,onefile=FALSE)
    ggplot(MPLC,aes(x=Var1,y=Var2,fill=value))+geom_tile()+scale_x_discrete(breaks=NULL)+scale_y_discrete(breaks=NULL)+scale_fill_gradient2(mid="white",high="red",low="blue",midpoint=-0.2)+labs(x="",y="",title = "MPLC Pearson_Correlation_Coefficient")+theme( plot.title = element_text(size=30,hjust = 0.5),legend.title=element_text(size=30),legend.text=element_text(size=30), panel.background=element_blank(),strip.text=element_text(size=25) )+guides(fill = guide_legend(title = expression("R"),reverse = TRUE))+facet_wrap(~patient, ncol=4,scales="free")
    dev.off()
    
    
    pdf(paste("2/COHORT2.correlation.pdf",sep = ""),width = 30,height =20,onefile=FALSE)
    ggplot(COHORT2,aes(x=Var1,y=Var2,fill=value))+geom_tile()+scale_x_discrete(breaks=NULL)+scale_y_discrete(breaks=NULL)+scale_fill_gradient2(mid="white",high="red",low="blue",midpoint=-0.2)+labs(x="",y="",title = "COHORT2 Pearson_Correlation_Coefficient")+theme( plot.title = element_text(size=30,hjust = 0.5),legend.title=element_text(size=30),legend.text=element_text(size=30), panel.background=element_blank(),strip.text=element_text(size=25) )+guides(fill = guide_legend(title = expression("R"),reverse = TRUE))+facet_wrap(~patient, ncol=8,scales="free")
    dev.off()
    

    结果示例:


    image.png

    相关文章

      网友评论

        本文标题:相关性计算与可视化画

        本文链接:https://www.haomeiwen.com/subject/axtrtrtx.html