Question: problem in generating correlation heatmap with significance level
gravatar for dpc
3 months ago by
dpc150 wrote:

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



    mydf[,2:5] = sapply(mydf[,2:5],jitter,amount=15)

    # 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) + 
              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'),
heatmap • 250 views
ADD COMMENTlink modified 3 months ago by Kevin Blighe69k • written 3 months ago by dpc150


The error is because you do not have the Method object. Try to use the R corrplot package:

I think it does what you want and it is fairly well documented.


ADD REPLYlink modified 3 months ago • written 3 months ago by antonioggsousa1.9k
gravatar for Kevin Blighe
3 months ago by
Kevin Blighe69k
Republic of Ireland
Kevin Blighe69k wrote:

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

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





ADD COMMENTlink modified 3 months ago • written 3 months ago by Kevin Blighe69k

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

ADD REPLYlink written 3 months ago by dpc150

Hi, that is possible to do, but, if you choose to use CorLevelPlot, then you would have to edit the lines here:

I think that it's also possible via corrplot, if you can check António's link.

ADD REPLYlink written 3 months ago by Kevin Blighe69k
Please log in to add an answer.


Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 1065 users visited in the last hour