美文网首页
简简单单的韦恩图

简简单单的韦恩图

作者: 深圳大学在读生信菜鸡 | 来源:发表于2023-03-29 16:42 被阅读0次

    rm(list=ls())
    library(ggplot2)
    library(ggvenn)
    library(paletteer)

    if(!require(paletteer))install.packages("paletteer")

    if(!require(scico))install.packages('scico')

    if(!require(nord))install.packages('nord')

    # 韦恩图的绘制学习

    第一个韦恩图的画法

    样例数据

    a <- list(Set 1 = c(1, 3, 5, 7, 9),
    Set 2 = c(1, 5, 9, 13),
    Set 3 = c(1, 2, 8, 9),
    Set 4 = c(6, 7, 10, 12))

    可视化绘制

    opar <- par(family = "Roboto Condensed")
    mypal= c('#E31A1CFF', '#FB9A99FF','#6A3D9AFF','#B2DF8AFF')##自定义颜色,选择paletteer的配色
    ggvenn(a,
    fill_color=mypal,#填充颜色
    fill_alpha = .7,##调节透明度

       stroke_linetype = "longdash",#调节圈的线条样式:默认是"solid",这里我选择虚线
       stroke_color='white',#(线条颜色):默认是"black"
       #stroke_alpha(线条透明度):默认是1
       #stroke_size(线条宽度):默认是1
       
       set_name_size = 8,#文本名大小
       #set_name_color = ,#(文本名颜色):默认是"black" 
       text_size=5 #文本大小默认是4.
       ) 
    

    可以在下面找到自己的配色

    paletteer_c("scico::berlin", n = 10)

    ## <colors>

    #9EB0FFFF #5AA3DAFF #2D7597FF #194155FF #11181DFF #270C01FF #501802FF #8A3F2AFF #C37469FF #FFACACFF

    paletteer_d("RColorBrewer::Paired")

    ## <colors>

    ## #A6CEE3FF #1F78B4FF #B2DF8AFF #33A02CFF #FB9A99FF #E31A1CFF #FDBF6FFF #FF7F00FF #CAB2D6FF #6A3D9AFF #FFFF99FF #B15928FF

    paletteer_dynamic("cartography::green.pal", 5)

    ## <colors>

    ## #B8D9A9FF #8DBC80FF #5D9D52FF #287A22FF #17692CFF

    另一种韦恩图的画法

    if(!require(VennDiagram))install.packages('VennDiagram')

    library (VennDiagram)

    A <- c("C", "D", "B", "E")

    B <- c("C", "E", "B", "A")

    C <- c("C", "E", "X", "Y")

    venn.diagram(x= list(A = A,B = B,C = C),

    filename = "pic.png",

    height = 450, width = 450,

    resolution =300,

    imagetype="png",

    col="transparent",

    fill=c("green","yellow","darkorchid1"),

    alpha = 0.50,

    cex=0.45,

    cat.cex=0.45)

    #输入三个向量,最后一个参数是名字,根据实际情况修改

    venn <- function(x,y,z,name,title){

    if(!require(VennDiagram))install.packages('VennDiagram')

    library (VennDiagram)

    venn.diagram(x= list(Aff = x,Bio = y,Mine = z),

    imagetype ="tiff",

    filename=paste(name,".tiff"),

    lwd=1,#圈线粗度

    lty=1, #圈线类型

    col=c('#0099CC','#FF6666','#FFCC99'), #圈线颜色

    fill=c('#0099CC','#FF6666','#FFCC99'), #填充颜色

    cat.col=c('#0099CC','#FF6666','#FFCC99'),#A和B的颜色

    cat.cex = 1.5,# A和B的大小

    rotation.degree = 0,#旋转角度

    main = title,#主标题内容

    main.cex = 1.5,#主标题大小

    cex=1.5,#里面交集字的大小

    alpha = 0.5,#透明度

    reverse=TRUE)

    }

    venn(A,B,C)

    BiocManager::install('ggVennDiagram')

    BiocManager::install('hrbrthemes')

    这个适合自己的个性化定制韦恩图

    library(ggVennDiagram)

    样例数据

    genes <- paste("gene",1:1000,sep="")
    set.seed(123)

    随机抽取基因组成列表的四个元素

    x <- list(A=sample(genes,300),
    B=sample(genes,525),
    C=sample(genes,440),
    D=sample(genes,350))

    可视化绘制

    之后可以个性化定制自己需要的韦恩图

    library(ggplot2)
    ggVennDiagram(x, category.names = c("Stage 1","Stage 2","Stage 3", "Stage4"),
    size=1,lty="longdash",color="gray60") +
    scale_fill_gradient(name="Count",low="#EC7D85",high = "#182F6F") +
    hrbrthemes::theme_ipsum(base_family = "sans") +
    labs(title = "Example of ggVennDiagram:: ggVennDiagram function",
    subtitle = "processed charts with ggVennDiagram()",
    caption = "Visualization by DataCharm") +
    theme(plot.title = element_text(hjust = 0.5,vjust = .5,color = "black",face = 'bold',
    size = 20, margin = margin(t = 1, b = 12)),
    plot.subtitle = element_text(hjust = 0,vjust = .5,size=15),
    plot.caption = element_text(face = 'bold',size = 12),
    axis.text.x = element_blank(),
    axis.text.y = element_blank(),
    axis.title.x = element_blank(),
    axis.title.y = element_blank())

    下面这个韦恩图函数有问题,会在getName报错,大家可以学习一下

    # 需要这两个包 ggvenn、 export

    library(grid)

    library(ggvenn)

    library(export)

    venn <- function(..., Rplot = TRUE, PDF = NULL, PPT = NULL,

    cex.label = 10, cex.num = 8,

    alpha.fill = 0.7, col.fill = c("#E5D2DD", "#53A85F", "#F1BB72", "#F3B1A0"),

    alpha.stroke = 1, size.stroke = 1,

    col.stroke = "black",

    show_percentage = FALSE

    ){

    data_list = list(...)

    names(data_list) = getName(...)

    # build venn table

    Len = length(data_list)

    vennM = matrix("", Len, Len, dimnames = list(getName(...), getName(...)))

    for(i in 1:Len){

    for(j in i:Len){

    vennM[i, j] = length(intersect(data_list[[i]], data_list[[j]]))

    }

    }

    resM = apply(vennM, 1, as.numeric)

    dimnames(resM) = dimnames(vennM)

    if(Len > 4){

    message("More than 4 objects, will not plot venn.")

    return(resM)

    }

    # plot venn

    require(ggvenn)

    p <- suppressWarnings(ggvenn(

    data = data_list,

    show_percentage = show_percentage,

    set_name_size = cex.label,

    text_size = cex.num,

    stroke_alpha = alpha.stroke,

    stroke_size = size.stroke,

    fill_alpha = alpha.fill,

    fill_color = col.fill,

    stroke_linetype = "longdash",

    ))

    if(Rplot){

    print(p)

    }

    if(!is.null(PDF)){

    pdf(PDF, 8, 8)

    print(p)

    dev.off()

    }

    if(!is.null(PPT)){

    require(export)

    graph2ppt(p, file = PPT, width = 8, height = 8)

    }

    return(resM)

    }

    set.seed(123)

    s1 = sample(LETTERS, 6)

    s2 = sample(LETTERS, 8)

    s3 = sample(LETTERS, 10)

    s4 = sample(LETTERS, 12)

    s5 = sample(LETTERS, 14)

    venn(s1, s2, s3)

    venn(s1, s2, s3, s4)

    venn(s1, s2, s3, s4, s5)##五个以上的对象是建议花瓣图或者upset图

    getAnywhere(getName)

    相关文章

      网友评论

          本文标题:简简单单的韦恩图

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