【问题标题】:How to add comma separator to Venn diagram如何在维恩图中添加逗号分隔符
【发布时间】:2017-03-29 20:53:27
【问题描述】:

在 r 中有没有一种方法可以将逗号分隔符添加到维恩图上的千位。

venn.plot <- VennDiagram::draw.pairwise.venn(10000, 7000, 3000, c("First", "Second"), scaled = FALSE)

grid::grid.draw(venn.plot)

生成的图表如下所示。

【问题讨论】:

    标签: r venn-diagram


    【解决方案1】:

    看起来这个函数不是为了这样做而设计的。如果你真的想使用这个功能,你可以“破解”它来替换它用于标签的默认格式代码。请注意,此方法非常脆弱,因为我们正在编辑特定的“行”代码。先复制一个函数

    myvenn <- VennDiagram::draw.pairwise.venn
    

    这是默认的格式化程序

    body(myvenn)[[46]]
    # wrapLab <- function(num) {
    #     stri = ""
    #     if (print.mode[1] == "percent") {
    #         stri <- paste(signif(num * 100/denom, digits = sigdigs), 
    #             "%", sep = "")
    #         if (isTRUE(print.mode[2] == "raw")) {
    #             stri <- paste(stri, "\n(", num, ")", sep = "")
    #         }
    #     }
    #     if (print.mode[1] == "raw") {
    #         stri <- num
    #         if (isTRUE(print.mode[2] == "percent")) {
    #             stri <- paste(stri, "\n(", paste(signif(num * 100/denom, 
    #                 digits = sigdigs), "%)", sep = ""), sep = "")
    #         }
    #     }
    #     return(stri)
    # }
    

    让我们将其替换为调用prettyNum 以添加逗号

    body(myvenn)[[46]][[3]] <- quote(function(x) {
        prettyNum(x ,big.mark=",",scientific=FALSE)
    })
    

    现在我们可以调用我们的函数版本了

    venn.plot <- myvenn(10000, 7000, 3000, c("First", "Second"), scaled = FALSE)
    grid::grid.draw(venn.plot)
    

    【讨论】:

      【解决方案2】:

      您也可以手动编辑项目。

      venn.plot[[5]][["label"]] <- "7,000"
      venn.plot[[6]][["label"]] <- "4,000"
      venn.plot[[7]][["label"]] <- "3,000"
      
      grid::grid.draw(venn.plot)
      

      【讨论】:

        【解决方案3】:

        这是另一种循环方式

        venn.plot <- VennDiagram::draw.pairwise.venn(10000, 7000, 3000, c("First", "Second"), scaled = FALSE)
        
        for(i in 1:length(venn.plot)){
        
         if(!is.null(venn.plot[[i]][["label"]]) &&
            !is.na(as.numeric(venn.plot[[i]][["label"]]))
         ) {
        
            venn.plot[[i]][["label"]] <- prettyNum(venn.plot[[i]][["label"]], big.mark = ",")
        
         }
         }
        
        Warning messages:
        1: NAs introduced by coercion 
        2: NAs introduced by coercion 
        grid::grid.draw(venn.plot)
        

        【讨论】:

          猜你喜欢
          • 2020-07-22
          • 2020-07-01
          • 2018-08-29
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-06-23
          • 2016-08-04
          相关资源
          最近更新 更多