I have tried with all options of colorbar in corrplot. However, when the matrix being plotted is very thin (low number of columns), the colorbar seems to step on top of the right margin. How to avoid this? Is there a parameter to control for spacing?
# Create the matrix
mat <- matrix(c(0.8, 0.6, 1, 0.2, 0, 0.2, 0.3, 0.4, 0.3,
-0.8, -0.6, -1, -0.2, 0, -0.2, -0.3, -0.4, -0.4),
ncol = 2, byrow = TRUE)
corrplot(mat, method = c("color"),
cl.ratio = 1,
# cl.offset = 0.5,
# cl.align.text = "l",
)
The way corrplot calculates the plot's width and height are hard coded and can not be adjusted by arguments.
A temporary work around can be modifying the corrplot package. This change will only last until the R session closes.
In the consol write:
trace(corrplot, edit=TRUE)
Variable mm
is used to allocate the plot's width and height. For thin matrices this value is very small, 1 in your case, so not enough space will be allocated for the plot.
On line 166 write:
mm = ifelse(mm < 6, 6, mm)
Save it. Now your plot looks like this (notice you have to reduce the cl.ratio to the recommended value):
library(corrplot)
# Create the matrix
mat <- matrix(c(0.8, 0.6, 1, 0.2, 0, 0.2, 0.3, 0.4, 0.3,
-0.8, -0.6, -1, -0.2, 0, -0.2, -0.3, -0.4, -0.4),
ncol = 2, byrow = TRUE)
corrplot(mat, method = c("color"), cl.ratio = 0.15)
You can play around with the value 6 in the code above. The larger this value the bigger the space between the plot and the bar.
For matrices with low number of rows, the nn
value should be modifyied similarly in line 163: nn = ifelse(nn < 6, 6, nn)
.
I'll commit these changes to the corrplot project.