problem in generating correlation heatmap with significance level
1
0
Entering edit mode
4.2 years ago
dpc ▴ 250

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 • 2.4k views
ADD COMMENT
0
Entering edit mode

Hi,

The error is because you do not have the Method object. Try to use the R corrplot package: https://cran.r-project.org/web/packages/corrplot/vignettes/corrplot-intro.html

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

António

ADD REPLY
3
Entering edit mode
4.2 years ago

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

ADD COMMENT
0
Entering edit mode

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 REPLY
0
Entering edit mode

Hi, that is possible to do, but, if you choose to use CorLevelPlot, then you would have to edit the lines here: https://github.com/kevinblighe/CorLevelPlot/blob/master/R/CorLevelPlot.R#L118-L141

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

ADD REPLY

Login before adding your answer.

Traffic: 1932 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6