如何在维恩图中添加逗号分隔符

How to add comma separator to Venn diagram

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

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

grid::grid.draw(venn.plot)

生成的图表如下所示。

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

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)

您也可以手动编辑项目。

venn.plot[[5]][["label"]] <- "7,000"
venn.plot[[6]][["label"]] <- "4,000"
venn.plot[[7]][["label"]] <- "3,000"

grid::grid.draw(venn.plot)

这是另一种循环方式

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)