1

I am a beginner in R and I have a functioning code that looks like this

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. Can someone help me out?

jay.sf
  • 60,139
  • 8
  • 53
  • 110
astulaaa
  • 11
  • 1

1 Answers1

0

We could hack the grob thrown by invisible output of 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)
# [...]
# $ :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=. After that we replace untouched temporary object tmp2 with modified temporary object tmp1 where numeric values are non-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".

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

Now, we can plot the grob using grid.draw.

grid.newpage()
grid.draw(V)

enter image description here

jay.sf
  • 60,139
  • 8
  • 53
  • 110