<p>Here is a solution using the <a href="https://cran.r-project.org/web/packages/pheatmap/index.html" rel="nofollow noreferrer">pheatmap</a> library to cluster and visualise the correlation matrix, then extract the groups from the cluster dendrograms:</p>

<pre><code>gen &lt;- read.csv(file = "TF_NORMAL_NEW_40.txt", header = TRUE, row.names = 1, sep = ",")
gendat &lt;- t(gen)
library(corrplot)
library(Hmisc)
macolor = colorRampPalette(c("navyblue", "white", "red"))(100)

cor_5 &lt;- rcorr(as.matrix(gendat))
M &lt;- cor_5<span class="math-container">$r
str(M)
p_mat &lt;- cor_5$</span>P
row_names &lt;- rownames(M)

library(pheatmap)
pdf("TF_NORMAL_NEW_40_gene.pdf",20,15,paper = "a4r")
plot_data_pheatmap &lt;- pheatmap(M, color = rev(macolor), clustering_method = "complete", fontsize_row = 0.8, fontsize_col = 0.8)
dev.off()
</code></pre>

<p><a href="https://i.sstatic.net/GSdVX.jpg" rel="nofollow noreferrer"><img src="https://i.sstatic.net/GSdVX.jpg" alt="jpeg version of pdf"></a></p>

<pre><code>tree_cut &lt;- cutree(plot_data_pheatmap<span class="math-container">$tree_row, h = 20)
tc &lt;- data.frame(tip = names(tree_cut), clust_membership = as.character(unname(tree_cut)))
row.names(tc) &lt;- tc$</span>tip
tc &lt;- tc['clust_membership']
head(tc)
str(tc)
class(tc$clust_membership)
pdf("TF_NORMAL_NEW_40_gene_clustcut.pdf",20,15,paper = "a4r")
pheatmap(M, color = rev(macolor), clustering_method = "complete", annotation_row = tc, annotation_col = tc, fontsize_row = 0.8, fontsize_col = 0.8)
dev.off()
</code></pre>

<p><a href="https://i.sstatic.net/PAT1v.jpg" rel="nofollow noreferrer"><img src="https://i.sstatic.net/PAT1v.jpg" alt="clusters at height 20 jpeg version"></a></p>

CC BY-SA 4.0
2018-12-19T04:40:15.900Z
okay! but how about the question i asked regarding high and low correlation? you saying i should be giving the corelation matrix as input to the pheatmap ?
kcm
CC BY-SA 4.0
2018-12-19T05:08:08.683Z
@krushnachChandra Yes.  Input the correlation matrix to pheatmap.  I have edited my answer.
user3479780
CC BY-SA 4.0
2018-12-19T06:19:16.343Z
okay i will give it a try so its the same thing as plotting in pheatmap there won't be any change i suppose..
kcm
CC BY-SA 4.0
2018-12-19T12:21:43.070Z
@krushnachChandra I have updated my answer to use your data and replicate what you have done to explain my method
user3479780
CC BY-SA 4.0
2018-12-19T03:57:30.973Z
2018-12-19T12:19:15.700Z
2018-12-19T12:19:15.700Z
user3479780
<p>I am doing a gene-gene pairwise correlation, I get a plot and I do see positive and negative correlation <a href="https://i.sstatic.net/56IzN.jpg" rel="nofollow noreferrer"><img src="https://i.sstatic.net/56IzN.jpg" alt="correlation plot "></a></p>

<p>My code:</p>

<pre><code>library(corrplot)
library(Hmisc)
gen = read.csv('NEW_RBP/CORPLOT/TF_NORMAL_NEW_40.txt',row.names = 1)
head(gen)
dim(gen)
gendat &lt;- t(gen)
#gendat &lt;- gen
head(gendat)
dim(gendat)


macolor = colorRampPalette(c("navyblue", "white", "red"))(100)

cor_5 &lt;- rcorr(as.matrix(gendat))
M &lt;- cor_5<span class="math-container">$r
p_mat &lt;- cor_5$</span>P
row_names &lt;- rownames(M)

pdf("TF_NORMAL_NEW_40_gene.pdf",20,15,paper = "a4r")


corrplot(M, method = "color",is.corr = FALSE,
         tl.srt = 45,diag = TRUE,tl.pos='n'
         ,order = "hclust",hclust.method = c("complete"),tl.cex = 3,number.cex=1.5)

dev.off()
</code></pre>

<p>How do I extract those gene pairs? One way what I have read and done is:</p>

<pre><code>library(dplyr)
flat_cor_mat &lt;- function(cor_r, cor_p){
    #This function provides a simple formatting of a correlation matrix
    #into a table with 4 columns containing :
    # Column 1 : row names (variable 1 for the correlation test)
    # Column 2 : column names (variable 2 for the correlation test)
    # Column 3 : the correlation coefficients
    # Column 4 : the p-values of the correlations
    library(tidyr)
    library(tibble)
    cor_r &lt;- rownames_to_column(as.data.frame(cor_r), var = "row")
    cor_r &lt;- gather(cor_r, column, cor, -1)
    cor_p &lt;- rownames_to_column(as.data.frame(cor_p), var = "row")
    cor_p &lt;- gather(cor_p, column, p, -1)
    cor_p_matrix &lt;- left_join(cor_r, cor_p, by = c("row", "column"))
    cor_p_matrix
}


#cor_3 &lt;- rcorr(as.matrix(gendat))

my_cor_matrix &lt;- flat_cor_mat(cor_5<span class="math-container">$r, cor_5$</span>P)
head(my_cor_matrix)

dim(my_cor_matrix)

Delete.na &lt;- function(DF, n=0) {
      DF[rowSums(is.na(DF)) &lt;= n,]
    }

TF_NORMAL_CORR = Delete.na(my_cor_matrix)
dim(RBP_DISEASE_40_CORR)

TF_NORMAL_HIGH = TF_NORMAL_CORR[ TF_NORMAL_CORR<span class="math-container">$cor &gt; 0.9 &amp; RBP_DISEASE_40_CORR$</span>p &lt; 0.00000001, ]

TF_NORMAL_LOW = TF_NORMAL_CORR[TF_NORMAL_CORR<span class="math-container">$cor &lt; -0.9 &amp; TF_NORMAL_CORR$</span>p &lt; 0.00000001, ]
</code></pre>

<p>Now even if I do that sort of high cut-off filtering I still get considerably high number of correlation pairs like few thousands if positive correlation is considered.</p>

<p>So few of my issues which I would like to get input or help </p>

<ul>
<li>How do I extract the cluster of regions with positive correlation or negative correlation  ?</li>
<li>If I found certain gene pair/pairs how do I label them ? should I make a vector or map or any other way like if say more than 0.8 correlation I would like to put a label on those regions only </li>
<li>Considering label the gene order is important to be considered, what I have read and found is this how to find the order of the label</li>
</ul>

<blockquote>
<pre><code>tempCor &lt;- cor(gendat)
tempCor &lt;- data.frame(tempCor)
dim(tempCor)
names(tempCor) &lt;- c(1:741)

out &lt;- corrplot(t(tempCor),method = "color",is.corr = FALSE,
                 tl.srt = 45,diag = TRUE,tl.pos='n'
                 ,order = "hclust",hclust.method = c("complete"),tl.cex = 3,number.cex=1.5)

dimnames(out)
</code></pre>
</blockquote>

<p>Now with all these how can I extract those patches of high and low correlation pairs and add selected label or highlight them and add label </p>

<p>I m not sure if all these can be done with corrplot! like adding selected label if yes i would be really glad to use that..</p>

<p>My data file <a href="https://drive.google.com/open?id=1try36CdLgsBSCdwd52jWHLiLcDS8kkNe" rel="nofollow noreferrer">data used </a></p>

<p>"In which groups are you interested in? In the upper left region and the bottom right square? In which format do you want it ? A list for each group? BTW, how many samples do you have? If it is so high, or they are mostly similar or there are few samples."</p>

<p>Its symmetric so would it matter if i use upper left or bottom right? I guess anyone would do good.</p>

<p>Format - a list for each group if i understand if there is one gene with many positive correlation then that would be considered as group?is that you are saying ?</p>

<p>I have 16 samples in this case but here I using gene-gene. I'm bit curious with a naive question: Is it possible to do gene to sample correlation or if i know what genes are positively or negatively correlated can I trace back without have to plot another heatmap of found genes which are positively or negatively correlated to see their expression in sample ?</p>

<p>I have given the data file it might help you better with my context..</p>

CC BY-SA 4.0
2018-12-18T21:52:40.853Z
In which groups are you interested in? In the upper left region and the bottom right square? In which format do you want it ? A list for each group? BTW, how many samples do you have? If it is so high, or they are mostly similar or there are few samples.
llrs
CC BY-SA 4.0
2018-12-19T04:49:48.403Z
I have updated my question with your queries ..hope it helps
kcm
<r><visualization><correlation>
Extracting genes from corrplot and adding labels based on high and low corelation
# Extracting genes from corrplot and adding labels based on high and low corelation

(Asked by: username_0 on 2018-12-18)

I am doing a gene-gene pairwise correlation, I get a plot and I do see positive and negative correlation 
My code:
library(corrplot)
library(Hmisc)
gen = read.csv('NEW_RBP/CORPLOT/TF_NORMAL_NEW_40.txt',row.names = 1)
head(gen)
dim(gen)
gendat <- t(gen)
#gendat <- gen
head(gendat)
dim(gendat)


macolor = colorRampPalette(c("navyblue", "white", "red"))(100)

cor_5 <- rcorr(as.matrix(gendat))
M <- cor_5$r
p_mat <- cor_5$P
row_names <- rownames(M)

pdf("TF_NORMAL_NEW_40_gene.pdf",20,15,paper = "a4r")


corrplot(M, method = "color",is.corr = FALSE,
         tl.srt = 45,diag = TRUE,tl.pos='n'
         ,order = "hclust",hclust.method = c("complete"),tl.cex = 3,number.cex=1.5)

dev.off()

How do I extract those gene pairs? One way what I have read and done is:
library(dplyr)
flat_cor_mat <- function(cor_r, cor_p){
    #This function provides a simple formatting of a correlation matrix
    #into a table with 4 columns containing :
    # Column 1 : row names (variable 1 for the correlation test)
    # Column 2 : column names (variable 2 for the correlation test)
    # Column 3 : the correlation coefficients
    # Column 4 : the p-values of the correlations
    library(tidyr)
    library(tibble)
    cor_r <- rownames_to_column(as.data.frame(cor_r), var = "row")
    cor_r <- gather(cor_r, column, cor, -1)
    cor_p <- rownames_to_column(as.data.frame(cor_p), var = "row")
    cor_p <- gather(cor_p, column, p, -1)
    cor_p_matrix <- left_join(cor_r, cor_p, by = c("row", "column"))
    cor_p_matrix
}


#cor_3 <- rcorr(as.matrix(gendat))

my_cor_matrix <- flat_cor_mat(cor_5$r, cor_5$P)
head(my_cor_matrix)

dim(my_cor_matrix)

Delete.na <- function(DF, n=0) {
      DF[rowSums(is.na(DF)) <= n,]
    }

TF_NORMAL_CORR = Delete.na(my_cor_matrix)
dim(RBP_DISEASE_40_CORR)

TF_NORMAL_HIGH = TF_NORMAL_CORR[ TF_NORMAL_CORR$cor > 0.9 & RBP_DISEASE_40_CORR$p < 0.00000001, ]

TF_NORMAL_LOW = TF_NORMAL_CORR[TF_NORMAL_CORR$cor < -0.9 & TF_NORMAL_CORR$p < 0.00000001, ]

Now even if I do that sort of high cut-off filtering I still get considerably high number of correlation pairs like few thousands if positive correlation is considered.
So few of my issues which I would like to get input or help 

How do I extract the cluster of regions with positive correlation or negative correlation  ?
If I found certain gene pair/pairs how do I label them ? should I make a vector or map or any other way like if say more than 0.8 correlation I would like to put a label on those regions only 
Considering label the gene order is important to be considered, what I have read and found is this how to find the order of the label


tempCor <- cor(gendat)
tempCor <- data.frame(tempCor)
dim(tempCor)
names(tempCor) <- c(1:741)

out <- corrplot(t(tempCor),method = "color",is.corr = FALSE,
                 tl.srt = 45,diag = TRUE,tl.pos='n'
                 ,order = "hclust",hclust.method = c("complete"),tl.cex = 3,number.cex=1.5)

dimnames(out)


Now with all these how can I extract those patches of high and low correlation pairs and add selected label or highlight them and add label 
I m not sure if all these can be done with corrplot! like adding selected label if yes i would be really glad to use that..
My data file data used 
"In which groups are you interested in? In the upper left region and the bottom right square? In which format do you want it ? A list for each group? BTW, how many samples do you have? If it is so high, or they are mostly similar or there are few samples."
Its symmetric so would it matter if i use upper left or bottom right? I guess anyone would do good.
Format - a list for each group if i understand if there is one gene with many positive correlation then that would be considered as group?is that you are saying ?
I have 16 samples in this case but here I using gene-gene. I'm bit curious with a naive question: Is it possible to do gene to sample correlation or if i know what genes are positively or negatively correlated can I trace back without have to plot another heatmap of found genes which are positively or negatively correlated to see their expression in sample ?
I have given the data file it might help you better with my context..

**Tags:** r, visualization, correlation
> **Comments:**
> **username_1**: In which groups are you interested in? In the upper left region and the bottom right square? In which format do you want it ? A list for each group? BTW, how many samples do you have? If it is so high, or they are mostly similar or there are few samples.  
> **username_0**: I have updated my question with your queries ..hope it helps  

---

## 1 answers

### Answer by username_2 on 2018-12-19. Score: 4 | Accepted answer

Here is a solution using the pheatmap library to cluster and visualise the correlation matrix, then extract the groups from the cluster dendrograms:
gen <- read.csv(file = "TF_NORMAL_NEW_40.txt", header = TRUE, row.names = 1, sep = ",")
gendat <- t(gen)
library(corrplot)
library(Hmisc)
macolor = colorRampPalette(c("navyblue", "white", "red"))(100)

cor_5 <- rcorr(as.matrix(gendat))
M <- cor_5$r
str(M)
p_mat <- cor_5$P
row_names <- rownames(M)

library(pheatmap)
pdf("TF_NORMAL_NEW_40_gene.pdf",20,15,paper = "a4r")
plot_data_pheatmap <- pheatmap(M, color = rev(macolor), clustering_method = "complete", fontsize_row = 0.8, fontsize_col = 0.8)
dev.off()


tree_cut <- cutree(plot_data_pheatmap$tree_row, h = 20)
tc <- data.frame(tip = names(tree_cut), clust_membership = as.character(unname(tree_cut)))
row.names(tc) <- tc$tip
tc <- tc['clust_membership']
head(tc)
str(tc)
class(tc$clust_membership)
pdf("TF_NORMAL_NEW_40_gene_clustcut.pdf",20,15,paper = "a4r")
pheatmap(M, color = rev(macolor), clustering_method = "complete", annotation_row = tc, annotation_col = tc, fontsize_row = 0.8, fontsize_col = 0.8)
dev.off()

> **Comments:**
> **username_0**: okay! but how about the question i asked regarding high and low correlation? you saying i should be giving the corelation matrix as input to the pheatmap ?  
> **username_2**: @krushnachChandra Yes.  Input the correlation matrix to pheatmap.  I have edited my answer.  
> **username_0**: okay i will give it a try so its the same thing as plotting in pheatmap there won't be any change i suppose..  
> **username_2**: @krushnachChandra I have updated my answer to use your data and replicate what you have done to explain my method  

---

