-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathREADME.Rmd
142 lines (109 loc) · 3.85 KB
/
README.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
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, echo = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
# out.width = "100%",
# dpi = 300,
fig.path = "tools/README-",
fig.cap = "ggcorrplot: visualize correlation matrix using ggplot2"
)
```
[![R build status](https://github.com/kassambara/ggcorrplot/workflows/R-CMD-check/badge.svg)](https://github.com/kassambara/ggcorrplot/actions)
[![CRAN_Status_Badge](https://www.r-pkg.org/badges/version/ggcorrplot)](https://cran.r-project.org/package=ggcorrplot)
[![CRAN Checks](https://cranchecks.info/badges/summary/ggcorrplot)](https://cran.r-project.org/web/checks/check_results_ggcorrplot.html)
[![Downloads](https://cranlogs.r-pkg.org/badges/ggcorrplot)](https://cran.r-project.org/package=ggcorrplot)
[![Total Downloads](https://cranlogs.r-pkg.org/badges/grand-total/ggcorrplot?color=orange)](https://cranlogs.r-pkg.org/badges/grand-total/ggcorrplot)
# ggcorrplot: Visualization of a correlation matrix using ggplot2
The **ggcorrplot** package can be used to **visualize easily** a **correlation matrix** using **ggplot2**. It provides a solution for **reordering** the correlation matrix and displays the **significance level** on the correlogram. It includes also a function for computing a matrix of **correlation p-values**.
Find out more at http://www.sthda.com/english/wiki/ggcorrplot-visualization-of-a-correlation-matrix-using-ggplot2.
## Installation and loading
ggcorrplot can be installed from `CRAN` as follow:
```{r, eval = FALSE}
install.packages("ggcorrplot")
```
Or, install the latest version from GitHub:
```{r, eval = FALSE}
# Install
if(!require(devtools)) install.packages("devtools")
devtools::install_github("kassambara/ggcorrplot")
```
```{r, message = FALSE, warning = FALSE}
# Loading
library(ggcorrplot)
```
## Getting started
### Compute a correlation matrix
The *mtcars* data set will be used in the following R code. The function
**cor_pmat()** [in **ggcorrplot**] computes a matrix of correlation p-values.
```{r, fig.show = "asis"}
# Compute a correlation matrix
data(mtcars)
corr <- round(cor(mtcars), 1)
head(corr[, 1:6])
# Compute a matrix of correlation p-values
p.mat <- cor_pmat(mtcars)
head(p.mat[, 1:4])
```
## Correlation matrix visualization
```{r demo-ggcorrplot, fig.show = "asis", fig.width=5, fig.height=5}
# Visualize the correlation matrix
# --------------------------------
# method = "square" (default)
ggcorrplot(corr)
# method = "circle"
ggcorrplot(corr, method = "circle")
# Reordering the correlation matrix
# --------------------------------
# using hierarchical clustering
ggcorrplot(corr, hc.order = TRUE, outline.color = "white")
# Types of correlogram layout
# --------------------------------
# Get the lower triangle
ggcorrplot(corr,
hc.order = TRUE,
type = "lower",
outline.color = "white")
# Get the upper triangle
ggcorrplot(corr,
hc.order = TRUE,
type = "upper",
outline.color = "white")
# Change colors and theme
# --------------------------------
# Argument colors
ggcorrplot(
corr,
hc.order = TRUE,
type = "lower",
outline.color = "white",
ggtheme = ggplot2::theme_gray,
colors = c("#6D9EC1", "white", "#E46726")
)
# Add correlation coefficients
# --------------------------------
# argument lab = TRUE
ggcorrplot(corr,
hc.order = TRUE,
type = "lower",
lab = TRUE)
# Add correlation significance level
# --------------------------------
# Argument p.mat
# Barring the no significant coefficient
ggcorrplot(corr,
hc.order = TRUE,
type = "lower",
p.mat = p.mat)
# Leave blank on no significant coefficient
ggcorrplot(
corr,
p.mat = p.mat,
hc.order = TRUE,
type = "lower",
insig = "blank"
)
```