-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path03-venndir-viz.Rmd
More file actions
2223 lines (1710 loc) · 81.2 KB
/
Copy path03-venndir-viz.Rmd
File metadata and controls
2223 lines (1710 loc) · 81.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Venndir Basics
```{r setup-03, include=FALSE}
knitr::opts_chunk$set(
# fig.retina = 2, out.width = '100%'
cache=TRUE,
fig.align="center",
fig.width=8,
fig.height=7
)
```
```{r html-latex-color, echo=FALSE}
# custom function to colorize latex output
html_to_latex_color <- function
(tvt,
verbose=FALSE,
...)
{
#
tvt2 <- gsub(" ", " ", gsub("<span style..font-family[^>]+>|</span>", "", tvt))
# cat(gsub("<", "\n<", tvt2))
tvl <- unlist(strsplit(gsub("<", "\n<", tvt2), "\n"))
tvdf <- data.frame(jamba::rbindList(strsplit(gsub(">", ">!", tvl), "!")));
tvl <- lapply(seq_len(nrow(tvdf)), function(irow){
# compress spaces?
# tvdf[irow, 2] <- gsub(" ", " ", tvdf[irow, 2]);
tvdf[irow, 2] <- gsub("\u2191", "\\\\uparrow", tvdf[irow, 2])
tvdf[irow, 2] <- gsub("\u2193", "\\\\downarrow", tvdf[irow, 2])
tvdf[irow, 2] <- gsub("((\\\\(up|down)arrow)+)", "$\\1$", tvdf[irow, 2])
if (all(grepl("^<br[^a-z]", tvdf[irow, 1]))) {
tvdf[irow, 2] <- "\\newline\n";
} else if (all(grepl("^[ ]*$", tvdf[irow, 2]))) {
k <- paste0("\\colorbox{white!0}{",
"\\textcolor{white!0}{",
tvdf[irow, 2], "}}")
} else {
i <- gsub("<span style=.|.>", "", tvdf[irow, 1])
fg <- gsub("(^|;)color:#([A-F0-9]{6}).*$", "\\2", i)
# escape the ampersand
tvdf[irow, 2] <- gsub("([&_])", "\\\\\\1", tvdf[irow, 2]);
if (grepl("background-color", i)) {
# \colorbox{yellow}{This text has a yellow background.}
bg <- gsub("^.*background-color:#([A-F0-9]{6}).*$", "\\1", i)
k <- paste0("\\colorbox[HTML]{", bg, "}{",
"\\textcolor[HTML]{", fg,
"}{", tvdf[irow, 2], "}}")
} else {
k <- paste0("\\colorbox{white!0}{",
"\\textcolor[HTML]{", fg,
"}{", tvdf[irow, 2], "}}")
# k <- paste0(
# "\\textcolor[HTML]{", fg,
# "}{", tvdf[irow, 2], "}")
}
}
})
# print(tvl)
tv_text <- jamba::cPaste(sep="", unlist(tvl))
tv_lines <- jamba::cPaste(sep="",
lapply(strsplit(tv_text, "\\\\newline\n"), function(i){
paste0("\\mbox{\\texttt{", i, "}}\\newline\n")
}))
paste0(
"\\setlength{\\fboxsep}{1pt}\n",
gsub(" ", "\\\\phantom{M}", tv_lines),
"\\setlength{\\fboxsep}{8.5pt}\n")
}
```
A Venn diagram seems relatively simple and straightforward.
It uses circles (or sometimes ellipses) to represent the
shared and distinct elements of each set.
Proportional [Euler diagrams](#euler-diagram) use areas proportional to
the number of elements of each set, and are somewhat unpredictable.
<!-- The R package **euler** [@R-eulerr] is used to provide the -->
<!-- best possible Euler diagram, but sometimes it is not possible -->
<!-- to represent all the areas perfectly. -->
An important feature of any good Venn diagram is clear labeling.
Venndir is as much about labeling as it is about the diagram.
Adding *signed labels* only heightens the challenge.
(ref:venndir-manual-label) Three-way proportional Euler diagram, showing the difficulty of labeling each region. Here, the overlap of set_A and set_C has 2 items. The label is placed outside, connected by a line segment.
(ref:venndir-manual-label-alt) Three-way Euler diagram showing the difficulty with placing labels in each overlap region. One region is labeled outside the diagram, with a connecting line segment.
```{r venndir-manual-label, echo=FALSE, out.width="65%", fig.cap="(ref:venndir-manual-label)", fig.alt="(ref:venndir-manual-label-alt)"}
library(venndir)
setlist <- make_venn_test(n_sets=3,
do_signed=TRUE, concordance=0.7,
n_items=110,
sizes=c(20, 30, 10))
v2 <- venndir(setlist, proportional=TRUE, show_labels="ncs",
do_plot=FALSE,
setlist_labels=paste("Set", LETTERS[1:3]),
font_cex=c(1, 1, 0.7) * 1.6,
circle_nudge=list(set_A=c(0.3, 0.3)), inside_percent_threshold=1)
v2h <- highlight_venndir_overlap(v2, overlap_set="set_A&set_C",
outerborder="red", outerborder.lwd=2)
plot(v2h,
expand_fraction=c(1, 1, 1, 1) * -0.1,
draw_legend=FALSE)
```
Venndir employs a variety of strategies to automate labeling,
and adds approaches to allow for manual adjustments.
Figure \@ref(fig:venndir-manual-label) demonstrates that sometimes
there is no other option but to place a label outside.
Most of this chapter describes basic features of Venndir.
Although some techniques may feel advanced,
I promise it becomes more "advanced" in the next chapter!
## Default Venndir
The following steps begin with a `setlist`, which contains varying
examples for the purpose of demonstrating various features.
To create a `setlist`, refer to [Data Import] for detailed
examples.
### Data for Testing
Most examples use `make_venn_test()` to create a `setlist`.
This function creates simple sets by default,
or signed sets with argument `do_signed=TRUE`.
The arguments help customize useful characteristics:
* `n_items=200`: Overall number of items in the "universe".
* `n_sets=3`: Number of sets.
* `do_signed=FALSE`: Whether to create signed sets.
* `concordance=0.5`: The directional concordance on a scale of -1 to 1.
* `sizes`: Specific sizes for each set, optional.
* `min_size`,`max_size`: Minimum and maximum set size, when `sizes` is NULL.
* `items`: The full universe of items to use, optional.
* `set_names`: Specific set names, optional.
```{r make-setlist}
setlist <- make_venn_test()
```
### Create the Venn
Given a `setlist`, a Venn diagram is straightforward with `venndir()`.
(ref:default-venn) Default Venn diagram with three sets.
```{r default-venn, out.width="60%", fig.width=5, fig.height=7, fig.cap="(ref:default-venn)", fig.alt="(ref:default-venn)"}
venndir(setlist)
```
The output of `venndir()` is an object with class `Venndir`,
returned invisibly. Returning the object invisibly means that the
object is not printed to screen, but can be assigned to a variable
if desired.
The `Venndir` object contains all the supporting data
required to make a figure, in addition to other relevant data that can
be exported for review, or edited to customize the figure.
A `Venndir` object can be printed which shows a brief summary.
```{r show-venndir}
v <- venndir(setlist, do_plot=FALSE)
v
```
There are many customization options, which will be explored in detail
throughout this user guide.
### Create the Euler
To make a proportional [Euler diagram](#euler-diagram), use argument
`proportional=TRUE`, Figure \@ref(fig:default-euler).
(ref:default-euler) Default proportional Euler diagram with three sets.
```{r default-euler, out.width="70%", fig.width=6, fig.height=7, fig.cap="(ref:default-euler)", fig.alt="(ref:default-euler)"}
venndir(setlist, proportional=TRUE)
```
#### Euler with ellipse
Venndir uses R package **eulerr** [@R-eulerr] to define the
model Euler diagram, which uses circles by default.
However, it can be coerced to use ellipse shapes with
argument `shape="ellipse"`.
Figure \@ref(fig:default-euler-2) compares circular and elliptical shapes,
and illustrates that neither option is perfect. The circular option
shows one empty set
(ref:default-euler-2) Proportional Euler diagram using circle (left) and ellipse (right).
```{r default-euler-2, out.width="50%", fig.ncol=2, fig.width=5, fig.height=7, fig.cap="(ref:default-euler-2)", fig.alt="(ref:default-euler-2)", fig.subcap=c("Circular sets.", "Elliptical sets.")}
setlist <- make_venn_test(sizes=c(33, 18, 69))
v1 <- venndir(setlist,
vector_method="label",
proportional=TRUE)
v2 <- venndir(setlist,
proportional=TRUE,
vector_method="label",
shape="ellipse")
```
Euler diagrams are imperfect with three or more sets, which means
there is no guaranteed geometry (with circles or ellipses) that
guarantees the overlap regions will be exactly proportional to
the number of items in each region.
Now is a good time to mention that Euler diagrams are useful but imperfect.
:::: {.tipbox data-latex=""}
**Key Points:**
1. Euler diagrams are only *approximately proportional* for three or more sets.
2. Some overlap regions may be shown which have no items.
3. Some overlap regions may not be shown which contain items.
A diagram with any [hidden overlaps](#hidden-overlaps-glossary) will display a small
footnote symbol $`r "\u2020"`$ in the bottom-right corner.
::::
Points **1** and **2** test one's coping skills:
"When is it *okay* not to be perfect?"
Point **3** is more concerning, and is discussed in detail
in the section [Hidden Overlaps]. Briefly:
* Hidden overlaps are indicated by a footnote.
* Hidden overlaps and are stored in the `Venndir` object.
* Information about "hidden overlaps" can be reviewed
using: `print(v)`, `footnotes(v)`, `warnings(v)`, or
`overlaplist(v)`.
The footnote is rendered on the bottom-left corner by default,
and can be controlled using arguments in `render_venndir_footnotes()`.
Notably, argument `footnote_style` can be:
* **`'symbol'`**: (default) only the footnote symbol
* **`'footnote'`**: one line for each footnote
* **`'header'`**: Simple summary of footnote marks.
The intention is for the footnote to be subtle, but visible, so
there is clear indication when something should be reviewed in detail.
(ref:default-euler-3) Euler diagrams showing a footnote symbol (left panel), and footnote comment (right panel).
```{r default-euler-3, out.width="50%", fig.ncol=2, fig.width=5, fig.height=7, fig.cap="(ref:default-euler-3)", fig.alt="(ref:default-euler-3)", fig.subcap=c("Footnote symbol.", "Footnote with comment.")}
setlist3 <- make_venn_test(500, n_sets=4)
v3 <- venndir(setlist3,
draw_legend=FALSE,
proportional=TRUE)
v3b <- venndir(setlist3,
footnote_style="footnote",
footnote_fontsize=12,
draw_legend=FALSE,
proportional=TRUE)
```
## Venn Set Colors
The argument `set_colors` is used to define colors for each set.
When it is `NULL`, as by default, it assigns rainbow categorical
colors by calling the **colorjam** [@R-colorjam] R package.
Figure \@ref(fig:default-venn-set-colors) demonstrates user-defined colors (left), and monochrome transparent colors (right).
(ref:default-venn-set-colors) Venn diagram showing custom set colors: red, orange, blue.
```{r default-venn-set-colors, out.width="50%", fig.ncol=2, fig.cap="(ref:default-venn-set-colors)", fig.alt="(ref:default-venn-set-colors)", fig.subcap=c("Custom colors.", "Transparent blue colors.")}
venndir(setlist,
set_colors=c("firebrick", "orange", "royalblue"))
# blue transparent colors
venndir(setlist,
poly_alpha=0.4,
set_colors=c("royalblue", "royalblue", "royalblue"))
```
The background fill color is also adjusted by `poly_alpha` to
control the alpha transparency.
Values range from 0 (fully transparent) to 1 (fully opaque).
The default `poly_alpha=0.6` is mostly opaque.
Figure \@ref(fig:default-venn-set-colors-dark) shows the effect
of using low transparency, which allows some background colors to become dark.
Notice some text labels become white to maintain visual contrast.
(ref:default-venn-set-colors-dark) Venn diagram using opaque colors, which also causes some text labels to become white for visual contrast.
```{r default-venn-set-colors-dark, fig.cap="(ref:default-venn-set-colors-dark)", fig.alt="(ref:default-venn-set-colors-dark)"}
venndir(setlist,
set_colors=c("firebrick", "orange", "royalblue"),
poly_alpha=0.9)
```
:::: {.tipbox data-latex=""}
**Note:**
Some R graphics devices do not support alpha transparency,
which can be reviewed with `dev.capabilities()`. It should be run
when the specific graphics device is open, for example after using
`cairo_pdf()` to open a PDF file for output.
::::
Colors can also be customized in any section of the figure,
using techniques described in [Modify Venn Overlaps].
### Consistent Set Colors
When the argument `setlist` is passed to `venndir()`, and no custom
colors are defined with argument `set_colors`, categorical colors are assigned
to each set.
In a situation where `setlist` contains five sets, it is recommended to
provide all five sets even for a figure that uses only a few sets.
The following steps make sure colors are used consistently for each set name.
1. Supply `venndir()` with the complete `setlist`.
2. Use argument `sets` to indicate which entries in `setlist` should be used.
3. Optionally define `set_colors`, `setlist_labels`, and `legend_labels`
for all sets in `setlist`.
Figure \@ref(fig:consistent-colors-1) represents five sets
with specific categorical colors assigned to each set. When
making Venn diagrams with two sets, they should use consistent
colors.
(ref:consistent-colors-1) Five sets are depicted as circles, each with a specific categorical color.
```{r consistent-colors-1, echo=FALSE, fig.align="center", out.width="50%", fig.height=4, fig.width=5.8, fig.cap="(ref:consistent-colors-1)", fig.alt="(ref:consistent-colors-1)"}
setlist1 <- counts2setlist(c(A=5, B=4, C=6, D=3, E=5))
vs1 <- venndir(setlist1, proportional=TRUE, do_plot=FALSE)
jpbox <- bbox_JamPolygon(vs1@jps)
plot(vs1@jps,
xlim=expand_range(jpbox[1,],
expand_fraction=-0.3))
```
Figure \@ref(fig:consistent-colors-2) shows the effects in a Venn diagram
with sets A and E, using the respective colors.
(ref:consistent-colors-2) Venn diagram comparing sets A and E. The colors are taken from the five-color palette, using yellow for A, and blue for E.
```{r consistent-colors-2, fig.height=4, fig.width=5, out.width="70%", fig.cap="(ref:consistent-colors-2)", fig.alt="(ref:consistent-colors-2)"}
setlist <- make_venn_test(n_sets=5, n_items=70, set_names=LETTERS[1:5])
v2 <- venndir(setlist,
sets=c(1, 5))
```
Figure \@ref(fig:consistent-colors-5) shows the desired outcome,
several Venn diagrams using colors consistent for each set.
(ref:consistent-colors-5) Four Venn diagrams using consistent colors for each set.
```{r consistent-colors-5, fig.height=6, fig.width=8, echo=FALSE, out.width="80%", fig.cap="(ref:consistent-colors-5)", fig.alt="(ref:consistent-colors-5)"}
# correct usage
v2ce1 <- venndir(setlist, sets=c(1, 2), do_plot=FALSE)
v2ce2 <- venndir(setlist, sets=c(2, 4), do_plot=FALSE)
v2ce3 <- venndir(setlist, sets=c(1, 3), do_plot=FALSE)
v2ce4 <- venndir(setlist, sets=c(3, 4), do_plot=FALSE)
v2ce1_grobs <- plot(v2ce1, x_inset=grid::unit(-1, "lines"), do_draw=FALSE)
v2ce2_grobs <- plot(v2ce2, x_inset=grid::unit(-1, "lines"), do_draw=FALSE)
v2ce3_grobs <- plot(v2ce3, x_inset=grid::unit(-1, "lines"), do_draw=FALSE)
v2ce4_grobs <- plot(v2ce4, x_inset=grid::unit(-1, "lines"), do_draw=FALSE)
v2ce1_gtree <- attr(v2ce1_grobs, "gtree")
v2ce2_gtree <- attr(v2ce2_grobs, "gtree")
v2ce3_gtree <- attr(v2ce3_grobs, "gtree")
v2ce4_gtree <- attr(v2ce4_grobs, "gtree")
pw2 <- (wrap_elements(v2ce1_gtree) +
wrap_elements(v2ce2_gtree)) /
(wrap_elements(v2ce3_gtree) +
wrap_elements(v2ce4_gtree)) +
plot_layout(widths=c(1, 1));
pw2
```
Figure \@ref(fig:consistent-colors-4) shows the problem to avoid,
several Venn diagram with colors not consistent for each set.
(ref:consistent-colors-4) Four Venn diagrams using the default colors, illustrating the problem to avoid: Colors are not consistent for each set.
```{r consistent-colors-4, fig.height=6, fig.width=8, echo=FALSE, out.width="80%", fig.cap="(ref:consistent-colors-4)", fig.alt="(ref:consistent-colors-4)"}
# assemble panels with patchwork
v2be1 <- venndir(setlist[c(1, 2)], do_plot=FALSE)
v2be2 <- venndir(setlist[c(2, 4)], do_plot=FALSE)
v2be3 <- venndir(setlist[c(1, 3)], do_plot=FALSE)
v2be4 <- venndir(setlist[c(3, 4)], do_plot=FALSE)
v2be1_grobs <- plot(v2be1, x_inset=grid::unit(-1, "lines"), do_draw=FALSE)
v2be2_grobs <- plot(v2be2, x_inset=grid::unit(-1, "lines"), do_draw=FALSE)
v2be3_grobs <- plot(v2be3, x_inset=grid::unit(-1, "lines"), do_draw=FALSE)
v2be4_grobs <- plot(v2be4, x_inset=grid::unit(-1, "lines"), do_draw=FALSE)
suppressWarnings({
v2be1_gtree <- attr(v2be1_grobs, "gtree")
v2be2_gtree <- attr(v2be2_grobs, "gtree")
v2be3_gtree <- attr(v2be3_grobs, "gtree")
v2be4_gtree <- attr(v2be4_grobs, "gtree")
})
pw <- (wrap_elements(v2be1_gtree) +
wrap_elements(v2be2_gtree)) /
(wrap_elements(v2be3_gtree) +
wrap_elements(v2be4_gtree)) +
plot_layout(widths=c(1, 1));
pw
```
Other alternatives to avoid mis-using color:
* Supply argument `set_colors` to `venndir()` with specific colors per set.
* Create Venn with no color, and no background fill.
### Venndir Without Color
It is possible to create a Venndir without color.
For example, some publications require black-and-white output.
Figure \@ref(fig:bw-venn-1) demonstrates Venndir output with no color.
(ref:bw-venn-1) Venndir figure with no color.
```{r bw-venn-1, fig.cap="(ref:bw-venn-1)", fig.alt="(ref:bw-venn-1)"}
v <- venndir(make_venn_test(),
show_segments=FALSE,
poly_alpha=0,
border="black",
border.lwd=2,
legend_color_style=c("noborder", "nofill"))
```
:::: {.tipbox data-latex=""}
**Tip:**
Using `poly_alpha=0` also makes signed labels not colored.
To maintain signed colors, use `poly_alpha=0.01`.
For more control over signed label colors, see [Custom Sign Label Colors].
::::
## Venndir Labels
### Label Content
The term 'label' may refer to many elements of a Venndir figure.
Of course, the sets have labels, and Venn overlap counts have
labels. When using signed data, there are also signed count labels.
There are also optional labels such as percent overlap, and
even individual item labels. In future, there may be additional
labels such as Jaccard overlap, Kruskal's directional concordance,
or z-score of directionality.
There are so many types of labels, it became unwieldy to assign
a separate function argument for each label, which is why
labels are controlled by one argument: `show_labels`.
Table \@ref(tab:label-table) describes each type of label,
and the letter assigned to each.
(fig:label-table) Summary of recognized options for the argument show labels.
```{r label-table, echo=FALSE}
label_df <- data.frame(check.names=FALSE,
Label=c("Name",
"Count",
"Signed",
"Percent",
"Item"),
Letter=c("'N' or 'n'",
"'C' or 'c'",
"'S' or 's'",
"'P' or 'p'",
"'i'"),
Notes=c("The set name provided in 'setlist'.",
"The count of the number of items in each Venn region. This label is sometimes referred to as 'main count' or 'overlap count'.",
"The count of the number of items by each observed combination of signs.",
"The percent of items in each Venn region, related to the total items represented in the diagram.",
"The item labels represented in each Venn region. Items can only be displayed inside the Venn diagram."
))
# prevent kableExtra from loading 'tabu' which is deprecated
options("kableExtra.latex.load_packages"=FALSE)
label_kdf <- knitr::kable(label_df,
booktabs=TRUE,
caption="Summary of recognized options for the argument show labels.",
row.names=FALSE)
label_kdf <- label_kdf |>
kableExtra::column_spec(1:2, width=c("4em"),
extra_css="white-space: nowrap;") |>
kableExtra::column_spec(3, width=c("20em"))
label_kdf <- epub_kdf(label_kdf)
label_kdf
```
Including the respective letter will enable that label.
Using UPPERCASE places the label outside, and using
lowercase places the label inside the Venn diagram.
The default `show_labels="Ncs"` places the Name outside, then
counts and [signed counts](#signed-counts) inside.
:::: {.tipbox data-latex=""}
**Tip:**
A good default to include the percentage is `show_labels="Ncsp"`.
The percentage is always located with the [main counts](#main-counts),
unless [main counts](#main-counts) are not shown.
::::
Currently, item labels can only be placed inside.
It is possible to place item labels in a table outside the Venn
diagram, however it involves advanced techniques.
Figure \@ref(fig:label-info) illustrates common examples for `show_label`,
showing how the components of each label are grouped together.
(ref:label-info) Four examples of various Venndir labels, placed inside or outside each figure. The label components are grouped by location, then organized in a defined way.
```{r label-info, fig.height=4, fig.width=4, out.width="50%", echo=FALSE, fig.ncol=2, fig.cap="(ref:label-info)", fig.alt="(ref:label-info)", fig.subcap=c("show\\_label='Ncps'", "show\\_label='NCPs'", "show\\_label='ncps'", "show\\_label='NCPSi'")}
setlist1 <- make_venn_test(n_sets=2,
n_items=40,
set_names=c("Set Label", "B"),
do_signed=TRUE)
use_fontcex <- 1.2;
use_expand <- c(0.0,0.0,0.0,0.5);
use_xyratio <- 1.8;
label_style <- "lite box"
v <- venndir(setlist1, sets=1,
do_plot=FALSE,
show_labels="Ncsp",
set_colors=c("slateblue1", "gold"),
setlist_labels=c(`Set Label`="Set Name", B="B"),
label_style=label_style,
font_cex=c(1, 1, 0.7) * use_fontcex,
expand_fraction=c(1,1,1,1)*use_expand,
segment_distance=0.2)
v@label_df$text[1] <- "{.u c}ount\n{.u p}ercent"
v@label_df$text[2] <- paste(#v@label_df$text[2],
"\u2191 {.u s}igned")
v@label_df$text[3] <- paste(#v@label_df$text[3],
"\u2193 {.u s}igned")
v@label_df$venn_label <- "{.u N}ame"
vNcsp <- v;
# plot(vNcsp)
v <- venndir(setlist1, sets=1,
do_plot=FALSE,
show_labels="NCsp",
set_colors=c("slateblue1", "gold"),
setlist_labels=c(`Set Label`="Set Name", B="B"),
label_style=label_style,
font_cex=c(1, 1, 0.7) * use_fontcex,
expand_fraction=c(1,1,1,1)*use_expand,
segment_distance=0.2)
v@label_df$text[1] <- "{.u C}ount\n{.u P}ercent"
v@label_df$text[2] <- paste(#v@label_df$text[2],
"\u2191 {.u s}igned")
v@label_df$text[3] <- paste(#v@label_df$text[3],
"\u2193 {.u s}igned")
v@label_df$venn_label <- "{.u N}ame"
vNCPs <- v;
# plot(vNCPs)
v <- venndir(setlist1, sets=1,
do_plot=FALSE,
show_labels="ncsp",
set_colors=c("slateblue1", "gold"),
setlist_labels=c(`Set Label`="Set Name", B="B"),
label_style=label_style,
font_cex=c(1, 1, 0.7) * use_fontcex,
expand_fraction=c(1,1,1,1)*use_expand,
segment_distance=0.2)
v@label_df$text[1] <- "{.u c}ount\n{.u p}ercent"
v@label_df$text[2] <- paste(#v@label_df$text[2],
"\u2191 {.u s}igned")
v@label_df$text[3] <- paste(#v@label_df$text[3],
"\u2193 {.u s}igned")
v@label_df$venn_label <- "{.u n}ame"
vncps <- v;
# plot(vncps)
v <- venndir(setlist1, sets=1,
do_plot=FALSE,
show_labels="NCSPi",
set_colors=c("slateblue1", "gold"),
setlist_labels=c(`Set Label`="Set Name", B="B"),
label_style=label_style,
item_buffer=-0.01, item_cex=0.8, show_items="item", xyratio=2.5,
font_cex=c(1, 1, 0.7) * use_fontcex,
expand_fraction=c(1,1,1,1)*use_expand,
segment_distance=0.2)
v@label_df$text[1] <- "{.u C}ount\n{.u P}ercent"
v@label_df$text[2] <- paste(#v@label_df$text[2],
"\u2191 {.u S}igned")
v@label_df$text[3] <- paste(#v@label_df$text[3],
"\u2193 {.u S}igned")
v@label_df$venn_label <- "{.u N}ame"
vNCPSi <- v;
# plot(vNCPSi, xyratio=1.8)
rm(v)
plot(vNcsp,
expand_fraction=c(1,1,1,1)*use_expand,
draw_legend=FALSE)
plot(vNCPs,
expand_fraction=c(1,1,1,1)*use_expand,
draw_legend=FALSE)
plot(vncps,
expand_fraction=c(1,1,1,1)*use_expand,
draw_legend=FALSE)
plot(vNCPSi,
expand_fraction=c(1,1,1,1)*use_expand,
xyratio=use_xyratio,
draw_legend=FALSE)
```
### Overlap Type
The argument `overlap_type` provides different approaches to
summarize directional overlaps. This option determines which
count labels will be displayed in the Venndir figure.
Table \@ref(tab:overlap-type-table) summarizes the options for `overlap_type`.
(fig:overlap-type-table) Summary of recognized overlap types, with a description of the associated output in a Venndir figure.
```{r overlap-type-table, echo=FALSE}
oll <- list(
"'overlap'"=paste("Only the summary overlap counts are displayed."),
"'concordance'"=paste("The summary overlap counts are displayed.",
"Signed counts are tabulated for overlaps involving one direction.",
"All other signed counts are summarized with 'X' for discordance."),
"'each'"=paste("The summary overlap counts are displayed.",
"Signed counts are tabulated for each combination of signs observed."),
"'agreement'"=paste("The summary overlap counts are displayed.",
"Signed counts are tabulated based upon agreement or disagreement",
"of the directional sign.")
)
oldf <- data.frame("overlap_type"=names(oll),
"Description"=unlist(oll))
kdf <- knitr::kable(row.names=FALSE,
booktabs=TRUE,
caption="Summary of recognized overlap types, with a description of the associated output in a Venndir figure.",
oldf)
kdf <- kdf |>
kableExtra::column_spec(2, width="25em")
kdf <- epub_kdf(kdf)
kdf
```
* When the input `setlist` is not signed,
`overlap_type='overlap'` is the default.
* When the input `setlist` is signed,
`overlap_type='concordance'` is the default.
Figure \@ref(fig:overlap-type-1) illustrates the four values for
`overlap_type`.
(ref:overlap-type-1) Venn diagrams showing four overlap types recognized: 'overlap' (default for non-signed data), 'concordance' (default for signed data), 'each', and 'agreement'.
(ref:overlap-type-1a) overlap_type='overlap'
(ref:overlap-type-1b) overlap_type='concordance'
(ref:overlap-type-1c) overlap_type='each'
(ref:overlap-type-1d) overlap_type='agreement'
```{r overlap-type-1, echo=FALSE, out.width="50%", fig.height=6, fig.width=6, fig.cap="(ref:overlap-type-1)", fig.alt="(ref:overlap-type-1)", fig.ncol=2, fig.subcap=c("(ref:overlap-type-1a)", "(ref:overlap-type-1b)", "(ref:overlap-type-1c)", "(ref:overlap-type-1d)")}
setlist <- make_venn_test(do_signed=TRUE)
v1 <- venndir(setlist, overlap_type='overlap')
v2 <- venndir(setlist, overlap_type='concordance',
x_inset=grid::unit(-1, "lines"))
v3 <- venndir(setlist, overlap_type='each',
x_inset=grid::unit(-1, "lines"))
v4 <- venndir(setlist, overlap_type='agreement',
x_inset=grid::unit(-1, "lines"))
```
### Signed Label Placement
Signed count labels are placed beside main count labels by default,
however they can be placed below main count labels.
The argument `template` controls the signed label placement,
and there are two options, illustrated in Figure \@ref(fig:template-wide).
1. `template='wide'` (default) labels [signed counts](#signed-counts) beside
main overlap counts.
2. `template='tall'` labels [signed counts](#signed-counts) below
main overlap counts.
(ref:template-wide) Venn diagram with signed counts labeled beside overlap counts (left) using `template='wide'`; and below overlap counts (right) using `template='tall'`.
```{r template-wide, out.width="50%", fig.ncol=2, fig.height=6, fig.width=6, fig.cap="(ref:template-wide)", fig.alt="(ref:template-wide)", fig.subcap=c("template='wide'", "template='tall'")}
setlist3 <- make_venn_test(n_sets=3, do_signed=TRUE)
vt1 <- venndir(setlist3)
vt2 <- venndir(setlist3, template="tall")
```
### Visual Styles
By default, Venn count and set labels are drawn without any particular
color shading or border. The argument `label_style` is used to enable
background color fill, and optional border.
The recognized keywords for `label_style`:
* `"lite"` - light background
* `"shaded"` - semi-transparent shaded color background
* `"fill"` - full color background
* `"box"` - draw a box as a border around the label
A straightforward example is shown in Figure \@ref(fig:label-style-0)
using `label_style="lite box"` with [signed counts](#signed-counts).
(ref:label-style-0) Venn diagram showing signed counts, using label style 'lite box'.
```{r label-style-0, fig.height=8, fig.width=6, fig.alt="(ref:label-style-0)", fig.cap="(ref:label-style-0)"}
setlistS <- make_venn_test(do_signed=TRUE)
vS <- venndir(setlistS, label_style="lite box")
```
Figure \@ref(fig:label-style-1) illustrates several possible styles.
In general, when using a fill color with any of the options
'lite', 'shaded', 'fill', it may be preferred to add 'box' to include
a visual border. For example, try 'lite box' or 'shaded box'.
(ref:label-style-1) Venn diagrams showing the different label style options in each of four panels.
(ref:label-style-1a) label_style='lite'
(ref:label-style-1b) label_style='shaded'
(ref:label-style-1c) label_style='fill'
(ref:label-style-1d) label_style='lite box'
```{r label-style-1, fig.ncol=2, out.width="50%", fig.height=4, fig.width=5, echo=FALSE, fig.alt="(ref:label-style-1)", fig.cap="(ref:label-style-1)", fig.subcap=c("(ref:label-style-1a)", "(ref:label-style-1b)", "(ref:label-style-1c)", "(ref:label-style-1d)")}
setlist <- make_venn_test(n_sets=3)
v1 <- venndir(setlist, sets=c(1, 3), label_style="lite")
v2 <- venndir(setlist, sets=c(1, 3), label_style="shaded")
v3 <- venndir(setlist, sets=c(1, 3), label_style="fill")
v4 <- venndir(setlist, sets=c(1, 3), label_style="lite box")
```
### Nudge a Venn Label
To nudge, that is to reposition, one label in a Venndir diagram,
use `nudge_venndir_label()`.
Note that `venndir()` will define a label position for every Venn overlap,
and defines a position 'inside' and 'outside' the Venn region.
:::: {.tipbox data-latex=""}
**Exception:**
Sometimes a proportional Euler diagram cannot represent
every overlap, due to geometric constraints.
In this case, not every overlap will be assigned a label position.
For a discussion of that issue, see Section \@ref(hidden-overlaps)
[Hidden Overlaps].
::::
To nudge a label, define the label to nudge using these two arguments:
* `set` - the overlapping region to nudge
* `label_location` - the inside or outside label associated with that set
The label adjustment uses two coordinates. Units are proportional
to the overall Venndir plot region, where `1` is the full width
or height of the plot, whichever is larger.
* `x_offset`
* `y_offset`
The process requires an existing `Venndir` object.
In the example below, a simple 3-way Venn diagram is stored
in variable `v`.
The label for `'set_C'` is moved left (decreasing the x coordinate),
and up (increasing the y coordinate).
```{r nudge-label-1a, eval=FALSE}
# default Venn
setlist <- make_venn_test()
v <- venndir(setlist)
# nudge 'set_C' up-and-left
v2 <- nudge_venndir_label(v,
x_offset=-0.45, y_offset=0.25,
set="set_C",
label_location="outside")
plot(v2)
```
Figure \@ref(fig:nudge-label-1) shows the Venn with
default labels (left), and with the label `'set_C'` adjusted (right).
(ref:nudge-label-1) Default 3-way Venn diagram (left), with label `'set_C'` moved up and to the left (right).
(ref:nudge-label-1-1) Default Venn.
(ref:nudge-label-1-2) Adjusted set_C label.
```{r nudge-label-1, echo=FALSE, fig.ncol=2, fig.height=6, fig.width=6, out.width="50%", fig.cap="(ref:nudge-label-1)", fig.alt="(ref:nudge-label-1)", fig.subcap=c("(ref:nudge-label-1-1)", "(ref:nudge-label-1-2)")}
setlist <- make_venn_test()
v <- venndir(setlist, do_plot=FALSE)
plot(v)
v2 <- nudge_venndir_label(v,
x_offset=-0.45, y_offset=0.25,
set="set_C",
label_location="outside")
plot(v2)
```
Notice that after the label is adjusted, the new `Venndir` object `v2`
can be visualized using `plot()`.
### Label Placement Rules
For each Venndir figure, sets are each represented by a circle
or ellipse. It should be "easy" to label each circle, right?
Experience has shown this to be more difficult than expected.
(ref:label-rules) Two examples of with very challenging labels.
(ref:label-rules-1) Fully nested sets.
(ref:label-rules-2) Partially nested sets.
```{r label-rules, echo=FALSE, fig.ncol=2, fig.height=4, fig.width=5, out.width="50%", fig.cap="(ref:label-rules)", fig.alt="(ref:label-rules)", fig.subcap=c("(ref:label-rules-1)", "(ref:label-rules-2)")}
venndir(counts2setlist(c(A=7, "A&B"=3, "A&B&C"=1)),
proportional=TRUE,
innerborder="white", innerborder.lwd=0.7, outerborder=NA,
vector_method="label",
segment_buffer=-0.3,
legend_color_style="whiteborder",
circle_nudge=list(C=c(-0.5, -0.2), B=c(-0.7, -0.1)),
segment_method="vector")
venndir(counts2setlist(c(A=7, "A&B"=3, "A&B&C"=2, "A&C"=1)),
proportional=TRUE,
innerborder="white", innerborder.lwd=0.7, outerborder=NA,
vector_method="label",
segment_buffer=-0.3,
legend_color_style="whiteborder",
circle_nudge=list(C=c(-0.5, -0.7), B=c(-0.5, -0.7)), rotate_degrees=-40,
segment_method="vector")
```
In Figure \@ref(fig:label-rules), where should the Set label be placed?
Where should the line segments connect?
**Specific rules for placing labels in the Venn figure:**
1. **Set labels** are directed to the most specific region in the figure.
* For Venn diagrams, Set labels refer to the region
"unique to set_A", with no other overlapping sets.
* For Euler diagrams, Set labels refer to the region "unique to set_A"
*if it exists*. Otherwise the Set refers to a region
containing *"set_A"* with the fewest overlapping sets.
* In Figure \@ref(fig.label-rules) (left) Set **'C'** refers to
the region inside B and C.
* In Figure \@ref(fig.label-rules) (right) Sets **'B'** and **'C'**
each refer to regions as unique to B and C as possible, respectively.
2. **Count labels** will only be associated with the specific
overlap region.
* If the specific overlap region does not exist in the figure,
the count label is not shown. Even though it may seem obvious,
sometimes a count label cannot be shown. See [Hidden Overlaps].
* If a count label is placed outside, a line segment will
connect the label to the corresponding region.
* The argument `inside_percent_threshold` may be used to
place a label outside when the region is small.
3. **Set and Count labels** may be grouped together when both are inside
or outside.
* When placed inside, a Set label will be grouped with the count label
to which the Set label refers, as described in Rule 1.
* When placed outside, a Set label will only appear with a count label
that is specific to that Set.
4. **Percentage labels** are shown together with [main counts](#main-counts)
when [main counts](#main-counts) are shown.
* Percentages are calculated directly from [main counts](#main-counts),
and as a result it is most intuitive for them appear together.
* However, if [main counts](#main-counts) are hidden, the percentage
may be placed inside or outside.
5. **Items labels** are only displayed inside, see [Item Labels].
* Count labels are "moved outside" if they were enabled inside and items
are to be displayed.
* When the number of items exceeds `max_items` then items are hidden,
and the count labels may be displayed in their place.
* Set names may be displayed inside, together with item labels,
however no effort is made to prevent overlapping labels.
Set labels can be manually nudged, see [Nudge a Venn Label].
## Item Labels
To display items inside a Venn diagram, add `"i"` to the argument
`show_labels`. For example `show_labels="Ni"` will display the set Name
outside, and items inside.
I never thought I'd be able to place item labels in a Venn diagram, much
less expect it, and eventually *need* it.
Item labeling has become one of my favorite features.
Adding item labels also ticks the box, so to speak, of an important
[data visualization](#data-visualization) paradigm:
Answering the very next question. For a Venn diagram:
"What are those?"
Figure \@ref(fig:items-1) illustrates an example inspired by a
[published figure](https://www.researchgate.net/figure/Venn-diagram-of-genes-associated-with-susceptibility-to-PAD-CAD-and-stroke-identified_fig2_356459049)
[@Salybekov_2021].
(ref:items-1) Venn diagram depicting genes associated with susceptibility to two diseases. The diagram shows the number of genes in each region: 2, 3, and 2, but does not show the genes.
```{r items-1, echo=FALSE, out.width="80%", fig.cap="(ref:items-1)", fig.alt="(ref:items-1)"}
itemlist <- list(
Stroke=c("COL4A12", "CHRNA3", "PITX2", "HLA-B", "ABO"),
PAD=c("HLA-B", "IL-6", "F5", "CHRNA3", "ABO")
)
venndir(itemlist,
font_cex=1.3,
set_colors=c("#FFC700", "#12E0E6"), poly_alpha=0.8,
setlist_labels=c("{#33647A STROKE}", "{#33647A PAD}"),
alias=c(Stroke="Stroke", PAD="PAD"),
legend_labels=c("Stroke Susceptibility", "Peripheral Arterial Disease"),
fontfamily="Proxima Nova",
fontfaces=list(overlap="plain", count="plain", signed="plain"),
outerborder.lwd=5, innerborder.lwd=0,
outerborder="#F5F5F5BB")
```
*(What is the very next question?)*
**"Which genes are in each region?"**
(ref:items-2) The same Venn diagram now showing genes as item labels inside the diagram, answering *the very next question*.
```{r items-2, echo=FALSE, out.width="80%", fig.cap="(ref:items-2)", fig.alt="(ref:items-2)"}
venndir(itemlist,
font_cex=1.3,
show_labels="Ni",
item_cex=c(2, 2, 1.8), xyratio=1.5,
set_colors=c("#FFC700", "#12E0E6"), poly_alpha=0.8,
setlist_labels=c("{#33647A STROKE}", "{#33647A PAD}"),
fontfamily="Proxima Nova",
fontfaces=list(overlap="plain", count="plain", signed="plain"),
outerborder.lwd=5, innerborder.lwd=0,
outerborder="#F5F5F5BB")
```
Figure \@ref(fig:items-2) represents the first example thus far of