washfaq
washfaq

Reputation: 322

How can I leave heatmap squares blank on no significant coefficient?

I want heatmap squares blank (without color) on no significance by using ggplot2. Here are the codes which I have used for heatmap.

data(mtcars)
cormat <- round(cor(mtcars), 2)

# Get upper triangle of the correlation matrix
get_upper_tri <- function(cormat){
  cormat[lower.tri(cormat)]<- NA
  return(cormat)
}

upper_tri <- get_upper_tri(cormat)

# Finished correlation matrix heatmap
library(reshape2)
melted_cormat <- melt(upper_tri, 
                      na.rm=TRUE) # Melt the correlation matrix

# Heatmap
library(ggplot2)

ggplot(data=melted_cormat, aes(Var2, Var1, fill=value))+
  geom_tile(color="white")+
  scale_fill_gradient2(low="blue", high="red", mid="white", 
                       midpoint=0, limit=c(-1,1), space="Lab", 
                       name="Pearson\nCorrelation")+
  ggtitle("Title")+
  xlab("V1")+
  ylab("V2")+
  theme_minimal()+ 
  theme(axis.text.x=element_text(angle=90, vjust=0.5, 
                                 size=10, hjust=1))+
  coord_fixed()

Here is the plot; enter image description here

I want plot like that enter image description here

Upvotes: 0

Views: 369

Answers (1)

Jon Spring
Jon Spring

Reputation: 66415

You could filter the incoming data frame like this, though the specific result will depend on your cutoff choice:

ggplot(data=melted_cormat[melted_cormat$value != 1 & abs(melted_cormat$value) > 0.3,], 
       aes(Var2, Var1, fill=value))+
...

enter image description here

Upvotes: 2

Related Questions