---
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","green"))
```

### size of set labels

```{r}
ggVennDiagram(gene_list, set_size = 8)
```


## Setting region label

### text content

```{r}
ggVennDiagram(gene_list, label = "count")
ggVennDiagram(gene_list, label = "percent")
```

### 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)
```


- change edge colors

```{r}
p + scale_color_brewer(palette = "Set1")
```


Some other palletes are:

```{r fig.asp=2, fig.width=4}
RColorBrewer::display.brewer.all()
```


## Adding note

```{r}
p + labs(title = "Four sets Venn Diagram",
         subtitle = "generated by `ggVennDiagram`",
         caption = Sys.Date())
```




