简体   繁体   English

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

[英]How to add comma separator in triple venn diagram?

I am a beginner in R and I have a functioning code that looks like this我是 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"))

I would like to add comma separators for big numbers but can't figure out how to add prettyNum or some similar function.我想为大数字添加逗号分隔符,但不知道如何添加 prettyNum 或一些类似的 function。 Can someone help me out?有人可以帮我吗?

We could hack the grob thrown by invisible output of draw.triple.venn .我们可以破解 draw.triple.venn 的隐形 output 抛出的draw.triple.venn

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

Exploring the object's structure using str(v) reveals that it's basically a list where there are labels defined in some of the list objects.使用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"
# [...]

We can extract them using the bracket function `[[`() , save them in two distinct temporary objects, and formatC them using the desired argument big.mark= .我们可以使用方括号 function `[[`()提取它们,将它们保存在两个不同的临时对象中,并使用所需的参数formatC big.mark=对它们进行格式化。 After that we replace untouched temporary object tmp2 with modified temporary object tmp1 where numeric values are non- NA .之后,我们将未触及的临时 object tmp2替换为修改后的临时 object 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)]

Finally, we replace the labels modified with big marks in the grob using Map and tell R that the class is "gList" .最后,我们使用Map中用大标记修改的标签,并告诉 R class"gList"

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

Now, we can plot the grob using grid.draw .现在,我们可以使用grid.draw

grid.newpage()
grid.draw(V)

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM