Search code examples
rplotr-corrplot

Color Legend has 0 width when making corrPlot with 1 column


I am trying to make a corrPlot with one column with one column like this

library(corrplot)
mc <- cor(mtcars,mtcars["mpg"])
corrplot(mc, method = "circle")

but when I do this the color legend has no width like this: 1columnCorrPlot

You can see the labels for the legend on the right side but the colorbar for the legend is just a line on the right border of the corrplot.

I tried using cl.ratio = to adjust this but i has no effect when there is only 1 column(it does have an effect when there are multiple columns).

Does anyone know a way to accomplish this?

Thanks,

Glenn


Solution

  • Looking at the code for corrplot which you can do by using fix(corrplot), you will notice cl.ratio is always multiplied by a variable mm to define xlim e.g. xlim <- c(m1 - 0.5 - xlabwidth, m2 + 0.5 + mm * cl.ratio * (cl.pos == "r")) and mm is calculated via:

    m2 <- max(Pos[, 1])
    m1 <- min(Pos[, 1])
    mm <- m2 - m1
    

    which results in mm=0 in the 1 column case. You can verify this by saving the corrplot function into another file (e.g. mycorrplot.r) and name the function mycorrplot(). Add these 3 lines following mm <- m2 - m1

    cat("Before:",mm,"\n")
    mm <- 1
    cat("After:",mm,"\n")
    

    Essentially, changing mm=0 to mm=1. Once you have done it, do:

    source("mycorrplot.r")
    mycorrplot(mc, method = "circle", cl.ratio=1)
    

    This will allow cl.ratio to modify the width of the legend. enter image description here

    This is just a hack, you should let the corrplot maintainer know of this issue. [https://github.com/taiyun/corrplot]

    Another option is to draw your own color legend: see how to place colorlegend (corrplot) in graphic