# 4.R

R: How to make professional and beautiful plots

## 1) lnstall R and R Studio

**R:** <https://www.r-project.org/>

**R Studio:** <https://www.rstudio.com/>

## 2) Reference cards for data analysis

* [Basics](https://cran.r-project.org/doc/contrib/Short-refcard.pdf)
* [Statistics](http://www.u.arizona.edu/~kuchi/Courses/MAT167/Files/R-refcard.pdf)
* [Data mining](https://cran.r-project.org/doc/contrib/YanchangZhao-refcard-data-mining.pdf)

## 3) R packages

**How to install R Packages:**

```r
> install.packages("PACKAGE")

# using bioconductor.org
> source("https://bioconductor.org/biocLite.R")
> biocLite("PACKAGE")
```

**Common used packages**

* To load data:

> - **RMySQL:** read in data from a database or Excel.
> - **xlsx:** read in data from a Excel.

* To manipulate data:

> - **stringr:** easy to learn tools for regular expressions and character strings.
> - **reshape2:** transform data between wide and long formats.

* To visualize data:

> - **gplots:** heatmap.2
> - **ggplot2:** plotting system for R, based on the grammar of graphics
> - **plotly:** R package for creating interactive web-based graphs

* To model data:

> - **lme4, nlme:** Linear and Non-linear mixed effects models
> - **randomForest:** Random forest methods from machine learning
> - **glmnet:** Lasso and elastic-net regression methods with cross validation
> - **NMF:** do non-negative matrix factorization

* To do bioinformatics:

> - **limma, DESeq2, edgeR:** do differential expression: [link](https://www.nature.com/articles/nprot.2013.099\))
> - **survival:** Tools for survival analysis
> - **motifRG, MotIV, MotifDb:** motif search and enrichment
> - **enrichR:** GO term enrichment

* To write your own R packages:

> - **devtools:** An essential suite of tools for turning your code into an R package.

* To color your figure:

> - **colorbrewer:** Creates nice looking color palettes especially for thematic maps <http://colorbrewer2.org/>

## 4) Homework

1. Plot one heatmap using function "[heatmap.2](https://www.rdocumentation.org/packages/gplots/versions/3.0.1/topics/heatmap.2)" in package "gplots" or package "[pheatmap](https://github.com/raivokolde/pheatmap)" . Here are some files to help you finish this job: [plotHeatmap.zip](https://github.com/lulab/training/blob/master/assets/files/plotHeatmap.zip) .
2. Make your own plots using other packages, like [plotly](https://plot.ly/r) or [ggvis](http://ggvis.rstudio.com).

## 5) Teaching video

### a.Basics

[@Youtube](https://youtu.be/A0YKZgxvpXM)

[@Bilibili](https://player.bilibili.com/player.html?aid=30590474\&cid=53392848\&page=1)

### b.Advanced (by Yang Eric Li)

[@Youtube](https://youtu.be/HeIAZ3pgsxQ)

[@Bilibili](https://player.bilibili.com/player.html?aid=30625984\&cid=53458279\&page=1)

## 6) References

<http://sape.inf.usi.ch/quick-reference/ggplot2>

<http://tutorials.iq.harvard.edu/R/Rgraphics/Rgraphics.html>

<https://www.analyticsvidhya.com/blog/2015/07/guide-data-visualization-r/>
