This is a test version of Biostars. For the public version, visit https://www.biostars.org.
problem in generating correlation heatmap with significance level

Hi there!!! I want help regarding the correlation heatmap generation showing the significance level within the heatmap cells. I want to show the P-value <0.05 as "" and P-value <0.001 as "*". For that, I am following the script from here. You can find the script under the header "Stars instead of p values:" (also, I have added the codes at the end of this query). When I run with my data ("my_data") I am facing an error message

"Error in FUN(X[[i]], ...) : object 'Method' not found".

Can you please tell me how should I run the code and where am I doing a mistake? Please bear with me if it is a silly question as I am not an expert with R.

Thanks, dpc

CODE:

    library(Hmisc)
    library(car)



  mydf=df
    set.seed(12345)
    mydf[,2:5] = sapply(mydf[,2:5],jitter,amount=15)
    mydf=mydf[c(1:10,20:29,39:48,58:67),]

    # calculate r
    c = rcorr(as.matrix(mydf[sapply(mydf,is.numeric)]))$r

    # calculate p values
    p = rcorr(as.matrix(mydf[sapply(mydf,is.numeric)]))$P

    plots <- dlply(mydf, .(Method), function (x1) {
      ggplot(data.frame(subset(melt(rcorr(as.matrix(x1[sapply(x1,is.numeric)]))$r)[lower.tri(c),],Var1 != Var2),
                        pvalue=Recode(subset(melt(rcorr(as.matrix(x1[sapply(x1,is.numeric)]))$P)[lower.tri(p),],Var1 != Var2)$value , "lo:0.01 = '***'; 0.01:0.05 = '*'; else = ' ';")),
             aes(x=Var1,y=Var2,fill=value)) +
        geom_tile(aes(fill = value),colour = "white") +
        geom_text(aes(label = sprintf("%1.2f",value)), vjust = 0) + 
        geom_text(aes(label = pvalue), vjust = 1) +
        theme_bw() +
        scale_fill_gradient2(name="R^2",midpoint=0.25,low = "blue", high = "red") + 
        xlab(NULL) + 
        ylab(NULL) + 
        theme(axis.text.x=element_blank(),
              axis.text.y=element_blank(),
              axis.ticks=element_blank(),
              panel.border=element_blank()) + 
        ggtitle(x1$Method) + theme(plot.title = element_text(lineheight=1,face="bold")) + 
        geom_text(data = subset(melt(rcorr(as.matrix(x1[sapply(x1,is.numeric)]))$r),Var1==Var2),
                  aes(label=Var1),vjust=1 ) 
    })

    grid.arrange(plots$Single_ROI + theme(legend.position='none'), 
                 plots$Simple_2_ROI + theme(legend.position='none'),
                 plots$WIG_Method + theme(legend.position='none'), 
                 plots$WIG_drawn_bg + theme(legend.position='none'),
                 ncol=2, 
                 nrow=2)
heatmap

1 answer

Yes, corrplot is one option, mentioned by António.

Another is lattice::levelplot, and I wrote a wrapper function here:

Cor_Level_Plot1_1

h

Cor_Level_Plot2_1

Kevin

Thanks Kevin for your suggestion and help. I will definitely take a look on it and let you know. Actually I want only the stars to be reflected inside the cells of the heatmap, not the values. I want this because I have a correlation heatmap of 150 microbes X 150 microbes it is quite large and the cells are very small. And also, want only the lower or the upper triangle of the heatmap to avoid the redundancy.

Thanks, dpc

Log in to answer this question.