R中三元维恩图中的逗号分隔符

问题描述 投票:1回答:1

我是R语言的初学者,我有一个类似这样的正常运行代码

library(VennDiagram) grid.newpage() draw.triple.venn(area1 = 49644, area2 = 38697, area3 = 33281, n12 = 14221, n23 = 11026, n13 = 13635, n123 = 4242, category = c("DOGS", "CATS", "HORSES"), cex = 1.6, cat.cex = 1.8, lwd = 2, fill = c("blue", "pink1", "grey50"))

我想为大数字添加逗号分隔符,但不知道如何添加prettyNum或某些类似函数。有人可以帮我吗?

r number-formatting venn-diagram
1个回答
0
投票

我们可以破解draw.triple.venn的不可见输出所引发的混乱。

V <- draw.triple.venn(...)  ## catch it in an object

使用str(v)探索对象的结构表明已定义标签。

str(V)
# $ :List of 11
# ..$ label        : chr "26030"                ## <-- here "label"
# ..$ x            : 'unit' num 0.2npc
# .. ..- attr(*, "valid.unit")= int 0
# .. ..- attr(*, "unit")= chr "npc"
# ..$ y            : 'unit' num 0.739npc
# .. ..- attr(*, "valid.unit")= int 0
# .. ..- attr(*, "unit")= chr "npc"
# ..$ just         : chr "centre"

我们可以提取它们,将它们保存在两个不同的临时对象中,然后使用所需的参数formatCbig.mark=。之后,我们用修改后的临时对象tmp2替换未触摸的临时对象tmp1,其中数值为非NA

tmp1 <- tmp2 <- lapply(V, `[[`, "label")
tmp1[sapply(lapply(V, `[[`, "label"), is.null)] <- NA
tmp1[] <- ifelse(is.na(as.numeric(tmp1)), NA, formatC(as.numeric(tmp1), format="d", big.mark=","))
tmp2[!is.na(tmp1)] <- tmp1[!is.na(tmp1)]

最后,我们使用Map将grob中的大标记修改标签替换为,并告诉R class"gList"

V <- `class<-`(Map(`[[<-`, V, "label", tmp2), "gList")

现在,我们可以使用grid.draw绘制该grob。

grid.newpage()
grid.draw(V)

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.