美文网首页R可视化小本本
R|可视化|boxplot+beeswarm

R|可视化|boxplot+beeswarm

作者: 高大石头 | 来源:发表于2021-03-13 23:02 被阅读0次

    实际科研中,往往需要联合箱式图(boxplot)和其他类型的图形样式,如蜂蜜图(beeswarm)进行数据展示,下面就联合boxplot+beeswarm来探索下。

    方法一:

    基础绘图函数boxplot,联合beeswarm::beeswarm绘制。

    rm(list = ls())
    library(limma)
    library(tidyverse)
    gene="ZEB1-AS1"
    
    rt <- data.table::fread("uniq.symbol.txt",data.table = F) %>% 
      column_to_rownames("ID")
    
    # FPKM to TPM
    fpkm2tpm <- function(fpkm){
      exp(log(fpkm)-log(sum(fpkm))+log(1e6))
    }
    rt <- apply(rt, 2, fpkm2tpm)
    rt1 <- log2(rt+1)
    Type=c(rep("Normal",72),rep("Tumor",539))
    single <- cbind(ID=colnames(rt1),expression=round(rt1[gene,],3),Type=Type)
    single <- as.data.frame(single)
    single$expression <- as.numeric(as.vector(single$expression))
    # 开始画图
    yMin=0                      #纵轴最小值
    yMax= 5                    #纵轴最大值
    ySeg=yMax*0.94
    labels=c("Normal (n=72)","Tumor (n=539)")
    wilcoxTest <- wilcox.test(expression~Type,data = single)
    wilcoxP <- wilcoxTest$p.value
    pvalue <- signif(wilcoxP,4)
    pval=0
    if(pvalue<0.001){
      pval=signif(pvalue,4)
      pval=format(pval,scientific = TRUE)
    }else{
      pval=round(pvalue,3)
    }
    
    library(beeswarm)
    par(mar = c(4,7,3,3))
    boxplot(expression ~ Type, data = single,names=labels,
            xlab = NA,
            ylab = "ZEB1-AS1 expression log2(TPM+1)",
            cex.main=1.5,cex.lab=1.3,cex.axis=1.2,ylim=c(yMin,yMax),outline=FALSE)
    beeswarm(expression ~ Type, data = single, col = c("blue","red"),lwd=0.2,
             pch = 16, add = TRUE, corral="wrap")
    segments(1,ySeg, 2,ySeg);segments(1,ySeg, 1,ySeg*0.96);segments(2,ySeg, 2,ySeg*0.96)
    text(1.5,ySeg*1.05,labels=paste("p=",pval,sep=""),cex=1.2)
    
    boxplot_beeswarm

    相关文章

      网友评论

        本文标题:R|可视化|boxplot+beeswarm

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