---
title: "CitFuns"
output: rmarkdown::html_vignette
description: |
  How to use CitFuns functions.
vignette: >
  %\VignetteIndexEntry{CitFuns Package}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---


```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, message = F, warning = F, error = F)
```

## Installation

In order to install CitFuns package from Git reppository, you must install `devtools` package:

```{r, eval=F}
install.packages('devtools')
```

In order to install it, you will have to install (if not already done) [Rtools](https://cran.r-project.org/bin/windows/Rtools/rtools40.html).

You will also need [Git](https://git-scm.com/downloads) installed in your computer.

Now we are ready to install CitFuns package:
```{r, eval=F}
devtools::install_git("https://git.ratg.cat/marcelcosta/CitFuns.git")
```

## Update

Any time you want to update the package, you must *reinstall* it:

```{r, eval=F}
detach("package:CitFuns", unload = TRUE) # Only required if you have loaded the package in this session.
devtools::install_git("https://git.ratg.cat/marcelcosta/CitFuns.git")
```

## ggheatmap

**ggheatmap** generates ggplot heatmaps easily.

We start by loading required packages, including *CitFuns*

```{r}
library(tidyverse)
library(CitFuns)
```

Now we will create an example dataframe:

```{r}
df<-data.frame("pats"=paste0("PAT", 1:20), "CytA"=rnorm(20,5), "CytB"=rnorm(20,5),
"CytC"=c(rnorm(5,10),rnorm(5,5),rnorm(5,10),rnorm(5,5)),"CytD"=rnorm(20,5),
"CytE"=c(rnorm(5,10),rnorm(5,5),rnorm(5,10),rnorm(5,5)),"CytF"=rnorm(20,5),
"CytG"=c(rnorm(5,10),rnorm(5,5),rnorm(5,10),rnorm(5,5)))

df<-gather(df, Cyt, Value,-pats)
head(df)
```

Usually, this package works with the dataframes in **Long** format, as it is intended in ggplot workflow.

And now we will generate the heatmap.

```{r, fig.width=8}
ggheatmap(df)
```

As we can observe, X and Y axis are sorted by cluster detection (using *hclust*).

*ggheatmap* groups the results if more there is more than one observation for each X-Y coordinate. By default, it calculates the mean, but the median can also be used instead. To show this, we will create another variable, "Met", and we plot Cytokine expression *versus* Met status.

```{r, fig.width=8}
clinics<-data.frame("pats"=df$pats %>% unique, "Met"=rep(c("0","1"), 10))
df<-merge(df, clinics)

ggheatmap(df, x="Cyt",y="Met", grouping = "median")
```

By default, *other variables not used are eliminated* during the grouping process. However, if you want to further use them (for faceting, for example), you can use the *exclude_group* parameter to keep them in the data.frame.

```{r, fig.width=8}
ggheatmap(df, exclude_group = "Met")+facet_grid(.~Met, scales = "free")
```

Finally, you can scale the heatmap either by rows or by columns:

```{r, fig.width=8}
ggheatmap(df, scale="rows")
```

It is worth noticing that ggheatmap outputs a ggplot object, so you can further modify it as you are used to:

```{r, fig.width=8, fig.height=5}
ggheatmap(df)+
  scale_fill_gradient(low = "black", high = "yellow")+
  scale_y_discrete(position = "right")+
  theme(legend.position = "bottom")
```

## ggcorrplot

ggcorrplot generates a correlation matrix. Using the same example dataframe, you have to specify which variable and value columns will be used to test correlation.

```{r, fig.width=5, fig.height=4}
ggcorrplot(df, var = "Cyt", value = "Value")
```

You can specify a color for the tile lines, transparent by default:

```{r, fig.width=5, fig.height=4}
ggcorrplot(df, var = "Cyt", value = "Value", color = "white")
```

By default, ggcorrplot converts the p-value into *star* significance equivalence. You can show the pvalue or nothing ("none").

```{r, fig.width=5, fig.height=4}
ggcorrplot(df, var = "Cyt", value = "Value", color = "white", stat="pval")
```

Finally, you can show only the upper part or the lower part of the specular matrix.

```{r, fig.width=5, fig.height=4}
ggcorrplot(df, var = "Cyt", value = "Value", color = "black", tri="lower")
```