/
using-ggVennDiagram.Rmd
189 lines (127 loc) · 3.5 KB
/
using-ggVennDiagram.Rmd
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
---
title: "Tutorial: Using ggVennDiagram"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{Tutorial: Using ggVennDiagram}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.width = 7,
fig.asp = 0.618,
fig.align = "center"
)
```
'`ggVennDiagram`' enables fancy Venn plot with 2-7 sets and generates publication quality figure.
## Installation
You can install the released version of ggVennDiagram from [CRAN](https://CRAN.R-project.org) with:
``` r
install.packages("ggVennDiagram")
```
And the development version from [GitHub](https://github.com/) with:
``` r
# install.packages("devtools")
devtools::install_github("gaospecial/ggVennDiagram")
```
## Usage
Generate example data.
```{r}
genes <- paste0("gene",1:1000)
set.seed(20210302)
gene_list <- list(A = sample(genes,100),
B = sample(genes,200),
C = sample(genes,300),
D = sample(genes,200))
library(ggVennDiagram)
library(ggplot2)
```
## long category names
If you use long category names in Venn plot, labels may be cropped by plot borders.
To avoid this, just use a ggplot trick to expand x axis.
```{r}
p1 <- ggVennDiagram(gene_list,
category.names = c("a very long name","short name","name","another name"))
p1
# expand axis to show long set labels
p1 + scale_x_continuous(expand = expansion(mult = .2))
```
## Show intersection values
When intersection values only have several members, `ggVennDiagram` is efficient to show the values in places.
```{r}
set.seed(0)
small_list <- lapply(sample(6:10, size = 4), function(x){
sample(letters,x)
})
ggVennDiagram(small_list,
category.names = LETTERS[1:4],
show_intersect = TRUE)
```
## Setting set label
### color of set label
```{r}
ggVennDiagram(gene_list, set_color = c("blue","black","red","yellow"))
```
### size of set labels
```{r}
ggVennDiagram(gene_list, set_size = 8)
```
## Setting region label
### text content
```{r eval=FALSE}
ggVennDiagram(gene_list, label = "count")
ggVennDiagram(gene_list, label = "percent")
ggVennDiagram(gene_list, label = "both")
ggVennDiagram(gene_list, label = "none")
```
```{r fig.width=12, echo=FALSE}
plots = lapply(c("none","count","percent","both"), function(x){
ggVennDiagram(gene_list,label = x) +
labs(title = paste0('label = "', x, '"')) +
theme(legend.position = "none")
})
aplot::plot_list(gglist = plots, ncol = 2, labels = LETTERS[1:4])
```
### percentage digits
```{r}
ggVennDiagram(gene_list, label_percent_digit = 1, label = "percent")
```
### remove label background
- Method 1: set alpha to 0
```{r}
ggVennDiagram(gene_list, label_alpha = 0)
```
- Method 2: use `geom_text()`
```{r}
ggVennDiagram(gene_list, label_geom = "text")
```
### color and size
```{r}
ggVennDiagram(gene_list, label_color = "firebrick", label_size = 4)
```
## Setting set edges
```{r}
ggVennDiagram(gene_list, edge_lty = "dashed", edge_size = 1)
```
## Changing palette
- changing fill palette
```{r}
library(ggplot2)
p <- ggVennDiagram(gene_list)
# Red Blue
p + scale_fill_distiller(palette = "RdBu")
# Reds
p + scale_fill_distiller(palette = "Reds", direction = 1)
```
Some other palletes are:
```{r fig.asp=2, fig.width=4}
RColorBrewer::display.brewer.all()
```
## Adding note
```{r}
p + labs(title = "Fancy Venn Diagram of four sets",
subtitle = "Generated by `ggVennDiagram`",
caption = Sys.Date())
```