Quamena
Quamena

Reputation: 71

how to obtain blocks of correlated variables within corrplot heatmap in r

I have several variables (e.g., 2000) CPGs and I would want to obtain the block or cluster of variables that are correlated within corrplot/heatmap at a certain threshold, say 0.3. I have attached a correlation plot for a few of the CPGs. Is there a function in r to execute this? Is there any way to go about this in R? Thanks for your help! Renter image description here

Upvotes: 1

Views: 820

Answers (1)

Andre Wildberg
Andre Wildberg

Reputation: 19088

Here is a way to obtain a clustering of the correlations. Using mtcars as toy data. You can choose a different number of clusters to adjust their "sensitivity". The more clusters the less members the higher their "sensitivity".

library(gplots)

# select 4 clusters
ct <- cutree(hclust(dist(cor(mtcars))), 4)

hm <- heatmap.2(cor(mtcars), trace="none", 
  col=colorRampPalette(c("blue2","white","red2"))(40), 
  breaks=seq(-1,1,length.out=41), RowSideColors=as.character(ct))

heatmap of correlations

You can filter by values afterwards by simply selecting a range from the matrix hm$carpet

dat <- hm$carpet
dat[dat < .3] <- NA

image(dat, axes=F, col=colorRampPalette(c("blue2","white","red2"))(40),
  breaks=seq(-1,1,length.out=41))
axis(1, at=seq(0,1, length.out=11), labels=rownames(dat), las=2)

image of filtered values > .3

Upvotes: 4

Related Questions