Search code examples
rheatmapcorrelationr-corrplot

R: Corrplot() heatmap manipulation?


I have 2 data sets, one being 264 columns and the other being 19 columns.

After running a Pearson's correlation using cor() I try plotting the output in a heatmap using corrplot(), but it's unreadable.

How can I edit my code to fix this?

My code is below:

    library("corrplot")
    D1=VGT5
    D2=BFT5
    CorTest=cor(D1, y=D2, use = "everything", method = "pearson")
    CorGraph=corrplot(CorTest, method = "circle", col = colorRampPalette(c("blue","white","red"))(200), title = "Pearson's Correlation of High-Fat Sugar at 12 weeks", tl.cex = .75, tl.col = "Black",diag = TRUE, cl.ratio = 2.25)

Solution

  • Your correlation matrix has 19 rows and 264 columns. The graphical representation of this matrix is rather problematic. Here are two possibile solutions.
    (1) Plot the whole matrix

    library(ggcorrplot)
    png(file="CorrPlot1.png", height=10000, width=3000, res=600)
    ggcorrplot(CorTest, lab_size=.1)+
    theme(axis.text.y = element_text(size=6),
          axis.text.x = element_text(size=6, angle=90))
    dev.off()
    

    (2) Cut the matrix in two pieces

    library(gridExtra)
    nc <- ncol(CorTest)
    png(file="CorrPlot2.png", height=7000, width=7000, res=600)
    p1 <- ggcorrplot(CorTest[,1:(nc/2)], lab_size=.1)+
    theme(axis.text.y = element_text(size=6),
          axis.text.x = element_text(size=6, angle=90))
    p2 <- ggcorrplot(CorTest[,(nc/2+1):nc], lab_size=.1)+
    theme(axis.text.y = element_text(size=6),
          axis.text.x = element_text(size=6, angle=90))
    grid.arrange(p1, p2, ncol=2)
    dev.off()
    

    enter image description here