美文网首页ggplot2绘图
ggplot绘制韦恩图-添加文字/图片注释

ggplot绘制韦恩图-添加文字/图片注释

作者: KS科研分享与服务 | 来源:发表于2024-03-27 16:29 被阅读0次

    之前有小伙伴咨询了一个韦恩图,其实韦恩图很简单,很多在线工具,代码的话不论是R还是python都有各种各样的包或者方法实现作图。但是他的需求是如何指示文字,一般的方法当然是使用AI很方便,但是代码可不可以实现呢?答案是肯定的!这里我们介绍一下使用ggveen/ggplot实现韦恩图的做法,并添加指示文字!参考:
    https://www.bilibili.com/video/BV1TC4y1N7b2/?spm_id_from=333.999.0.0&vd_source=05b5479545ba945a8f5d7b2e7160ea34
    首先我们构建一个差异基因分析,用来做韦恩图:

    celltype_DEGs <- list()
    cells <- c("UEC", "CEC")
    for (i in 1:2) {
      
      data = subset(sce, celltype==cells[i])
      df <- FindMarkers(data,
                        group.by="orig.ident",
                        ident.1="AEH",
                        ident.2="HC",
                        logfc.threshold = 0.4,
                        min.pct = 0.4)
      celltype_DEGs[[i]] <- df
    }
    
    # adjust p 显著
    for(i in seq_along(celltype_DEGs)){
      
      celltype_DEGs[[i]] <- celltype_DEGs[[i]][which(celltype_DEGs[[i]]$p_val_adj<=0.05),]
    }
    

    我们直接使用ggvenn作图:

    
    library(ggvenn)
    library(tidyverse)
    library(ggtext)
    
    
    Venn_list <- list(UEC=rownames(celltype_DEGs[[1]]),
                      CEC=rownames(celltype_DEGs[[2]]))
    #使用list_to_data_frame将list转化为data.frame
    data_veen = list_to_data_frame(Venn_list)
    
    #鉴定集合基因
    Common_gene <- data_veen[which(data_veen$`UEC`=="TRUE" & data_veen$`CEC`=="TRUE"),]
    UEC_gene <- data_veen[which(data_veen$`UEC`=="TRUE" & data_veen$`CEC`=="FALSE"),]
    CEC_gene <- data_veen[which(data_veen$`UEC`=="FALSE" & data_veen$`CEC`=="TRUE"),]
    #保存文件
    write.csv(Common_gene, file = "Common_gene.csv")
    write.csv(UEC_gene, file = "UEC_gene.csv")
    write.csv(CEC_gene, file = "CEC_gene.csv")
    
    
    #method1,用list数据、ggvenn作图
    ggvenn(Venn_list,
           show_percentage = T,
           show_elements = F,
           text_size=3,
           digits = 1,
           auto_scale =T,
           set_name_size=4,
           stroke_color = "grey30",
           fill_color = c("#FF8C00","#4DAF4A"),
           set_name_color = c("#FF8C00","#4DAF4A"))+
      geom_segment(aes(x = 0.2, y = -0.2,xend = 0.2,yend = -0.8),
                 arrow = arrow(length = unit(0.07, "inch")),size = 1,
                 color = "grey30")+
      geom_text(aes(0.2, -1, label = "ASTL GCLC\nIGFBP4 PDE4C\nMMP7 TRH"),
                hjust = -0.2, vjust =0, fontface="italic")+
      geom_curve(aes(x = -1, y = 0,xend = -0.5,yend = 1.8),
               arrow = arrow(length = unit(0.1, "inch")),size = 1,
               color = "grey30", curvature = -0.4)+
      geom_text(aes(-0.5, 2, label = "SYT14 MUC16\nPCDH7 HPN\nITGA6 CXCL3"),
                hjust = -0.1, vjust =1, fontface="italic")+
      geom_curve(aes(x = 1, y = 0,xend = 0.5,yend = 1),
                 arrow = arrow(length = unit(0.1, "inch")),size = 1,
                 color = "grey30", curvature = 0.2)+
      geom_text(aes(0.5, 1, label = "CASC9 SFTA2\nCOL1A1 SPARC"),
                hjust = 1, vjust =-0.1, fontface="italic")
    

    使用ggplot,geom_veen作图:

    p = ggplot(data_veen, aes(A = `UEC`, B = `CEC`)) +
      geom_venn(auto_scale =T,
                set_name_size=4,
                fill_color = c("#BCCC35","#5DBBA1"),
                set_name_color = c("#BCCC35","#5DBBA1")) + 
      theme_void() + 
      coord_fixed()+
      geom_segment(aes(x = 0.2, y = -0.2,xend = 0.2,yend = -1),
                   arrow = arrow(length = unit(0.07, "inch")),size = 1,
                   color = "grey30")+
      geom_text(aes(0.2, -1.2, label = "ASTL GCLC\nIGFBP4 PDE4C\nMMP7 TRH"),
                hjust = 0.5, vjust =1, fontface="italic")+
      geom_curve(aes(x = -1, y = 0,xend = -0.5,yend = 1.8),
                 arrow = arrow(length = unit(0.1, "inch")),size = 1,
                 color = "grey30", curvature = -0.4)+
      geom_text(aes(-0.5, 2, label = "SYT14 MUC16\nPCDH7 HPN\nITGA6 CXCL3"),
                hjust = -0.1, vjust =1, fontface="italic", color="#BCCC35")+
      geom_curve(aes(x = 1, y = 0,xend = 1.2,yend = 1),
                 arrow = arrow(length = unit(0.1, "inch")),size = 1,
                 color = "grey30", curvature = 0.2)+
      geom_text(aes(1.2, 1, label = "CASC9 SFTA2\nCOL1A1 SPARC"),
                hjust = -0.1, vjust =1, fontface="italic", color="#5DBBA1")
    

    最后,我们还以添加矢量图表示不同的样本:简笔画网站:
    http://phylopic.org/image/browse/
    #读入图片
    library(grid)
    library(png)
    library(RCurl)
    
    
    rayur1 = "https://images.phylopic.org/images/9fae30cd-fb59-4a81-a39c-e1826a35f612/raster/187x512.png"
    raylogo1 = readPNG(getURLContent(rayur1), native = T)
    im1 <- rasterGrob(raylogo1, interpolate=TRUE)
    
    
    rayur2 = "https://images.phylopic.org/images/acf1cbec-f6ef-4a82-8ab5-be2f963b93f5/raster/192x512.png"
    raylogo2 = readPNG(getURLContent(rayur2), native = T)
    im2 <- rasterGrob(raylogo2, interpolate=TRUE)
    
    
    p+
      annotation_custom(im1, xmin=-1.8, xmax=-1.3, ymin=-0.5, ymax=0.5)+
      annotation_custom(im2, xmin=1.1, xmax=1.6, ymin=-0.5, ymax=0.5)
    
    

    今天的内容就分享到这里了,希望对你有用,点个赞在走呗!

    相关文章

      网友评论

        本文标题:ggplot绘制韦恩图-添加文字/图片注释

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