# Doing correlation in R using corrplot

#Creating a imaginary data frame

Lets say

>A <- c(2, 4, 6, 8, 12, 5, 7,8)
> B <- c(4,5,6,2,9,13,2, 6)
> C <- c(3,5,7,2,4,8,3,6)
>D <- c(4,6,2,5,7,4,6,9)

DD <- data.frame(A,B,C, D)

#correlation with package corrplot
> install.packages(“corrplot”)

library(“corrplot”)

#doing the correlation test and plotting the matrix
#we have assigned the result of the correlation to the variable CORT

#you can use any of the test from pearson, kendall, spearman, we are using pearson

CORT <- cor (DD, method = “pearson”)

```cor(x, y = NULL, use = "everything",
method = c("pearson", "kendall", "spearman"))```

#now plotting with corrplot, the default plot will give you a plot with circular points in a matrix with a key scale on the right
> corrplot(CORT)

*here, if you compare the right scale, higher the color intensity towards blue they are more correlated, while towards red are negatively correlated.

#you can easily change your plot , changing the following arguments

corrplot(CORT, method = “color”, order = “AOE”, tl.col = “black”, tl.cex = 0.8, addCoef.col = “black”)

#where, you can choose any of the following methods,

```method = c("circle", "square", "ellipse", "number", "shade",
"color", "pie") we choosed color```

or any of the order from,

 `order` Character, the ordering method of the correlation matrix. `"original"` for original order (default). `"AOE"` for the angular order of the eigenvectors. `"FPC"` for the first principal component order. `"hclust"` for the hierarchical clustering order. `"alphabet"` for alphabetical order.

tl. col = change the color of the label,

tl.cex= you can adjust the size of the font

The above corrplot is full, let say you only want the lower half or upper half, then what you have to do is,

corrplot(CORT, method = “color”, order = “AOE”, type = ‘lower’, tl.col = “black”, tl.cex = 0.8, addCoef.col = “black”)

add one more argument, type = “lower” if you want lower half or type = “upper” if you want upper half.

```corrplot(corr, method = c("circle", "square", "ellipse", "number", "shade",
"color", "pie"), type = c("full", "lower", "upper"), add = FALSE,
col = NULL, bg = "white", title = "", is.corr = TRUE, diag = TRUE,
outline = FALSE, mar = c(0, 0, 0, 0), addgrid.col = NULL,
addCoef.col = NULL, addCoefasPercent = FALSE, order = c("original",
"AOE", "FPC", "hclust", "alphabet"), hclust.method = c("complete", "ward",
"ward.D", "ward.D2", "single", "average", "mcquitty", "median", "centroid"),
addrect = NULL, rect.col = "black", rect.lwd = 2, tl.pos = NULL,
tl.cex = 1, tl.col = "red", tl.offset = 0.4, tl.srt = 90,
cl.pos = NULL, cl.lim = NULL, cl.length = NULL, cl.cex = 0.8,
cl.ratio = 0.15, cl.align.text = "c", cl.offset = 0.5, number.cex = 1,
number.font = 2, number.digits = NULL, addshade = c("negative",
"positive", "all"), shade.lwd = 1, shade.col = "white", p.mat = NULL,
sig.level = 0.05, insig = c("pch", "p-value", "blank", "n", "label_sig"),
pch = 4, pch.col = "black", pch.cex = 3, plotCI = c("n", "square",
"circle", "rect"), lowCI.mat = NULL, uppCI.mat = NULL, na.label = "?",
na.label.col = "black", win.asp = 1, ...)```

#You can play with the arguments, changing the parameter to change your plots accordingly.

corrplot(CORT, method = “pie”, order = “AOE”, type = ‘upper’, tl.col = “black”, tl.cex = 0.8, addCoef.col = “black”)

I hope this will help be useful. Please comment and share the post. if you have any question or suggestion, please comment.

Next time we will plot corrplot with p value or using significance.

Author : Saurav Das (https://twitter.com/Moutain_Soul or https://www.facebook.com/saurav12das)