---
title: "Fully Customed Venn Diagram"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Fully Customed Venn Diagram}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
```

```{r setup}
library(ggVennDiagram)
```

## Comprehensive customization by using helper functions

The main function `ggVennDiagram()` accepts a list input, and output a ggplot object. 
By measuring the length of input list, it automatically applies internal functions 
to build a plot in two steps: data preparation and visualization. 

Data preparation was packaged into one function `process_data()`. Its output
is a S4 `VennPlotData` class object, which contains three slots, `setEdge`, 
`setLabel` and `region`. These slot data then can be further plotted with `ggplot` 
functions. 

See below for a better understanding.

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

Then we can reproduce the plot of `ggVennDiagram()` with several lines.

```{r}
venn <- Venn(gene_list)
data <- process_data(venn)
ggplot() +
  # 1. region count layer
  geom_sf(aes(fill = count), data = venn_region(data)) +
  # 2. set edge layer
  geom_sf(aes(color = id), data = venn_setedge(data), show.legend = FALSE) +
  # 3. set label layer
  geom_sf_text(aes(label = name), data = venn_setlabel(data)) +
  # 4. region label layer
  geom_sf_label(aes(label = count), data = venn_region(data)) +
  theme_void()
```

The variable `data` is a S4 class object that has three slots.

```{r}
data
```


`ggVennDiagram` export functions to get these data, and they can be used 
for comprehensive customization in user-side.

- `Venn()`: Venn object constructor, use this to construct a Venn object from list.
- `process_data()`: process data with a Venn object
- `venn_region()`: get region data to plot
- `venn_setedge()`: get setedge data to plot
- `venn_setlabel()`: get setlabel data to plot


For example, you may change edge/fill/label properties as you will.

```{r}
ggplot() +
  # change mapping of color filling
  geom_sf(aes(fill = id), data = venn_region(data), show.legend = FALSE) +  
  # adjust edge size and color
  geom_sf(color="grey", size = 3, data = venn_setedge(data), show.legend = FALSE) +  
  # show set label in bold
  geom_sf_text(aes(label = name), fontface = "bold", data = venn_setlabel(data)) +  
  # add a alternative region name
  geom_sf_label(aes(label = name), data = venn_region(data), alpha = 0.5) +  
  theme_void()
```

## Stepwise `plotly`

This is for [issue #26](https://github.com/gaospecial/ggVennDiagram/issues/26).

```{r}
venn <- Venn(gene_list)
data <- process_data(venn)
items <- venn_region(data) %>%
  dplyr::rowwise() %>%
  dplyr::mutate(text = stringr::str_wrap(paste0(.data$item, collapse = " "),
                                         width = 40)) %>%
  sf::st_as_sf()
label_coord = sf::st_centroid(items$geometry) %>% sf::st_coordinates()
p <- ggplot(items) +
  geom_sf(aes_string(fill="count")) +
  geom_sf_text(aes_string(label = "name"),
               data = data@setLabel,
               inherit.aes = F) +
  geom_text(aes_string(label = "count", text = "text"),
            x = label_coord[,1],
            y = label_coord[,2],
            show.legend = FALSE) +
  theme_void() +
  scale_fill_distiller(palette = "RdBu")
ax <- list(
  showline = FALSE
)
plotly::ggplotly(p, tooltip = c("text")) %>%
  plotly::layout(xaxis = ax, yaxis = ax)
```

**Note**: Please be aware that due to the poor compatibility between plotly and ggplot, this feature is only experimental.
