ggcorrplot2
ggcorrplot2 copied to clipboard
Problem with legend.position = "bottom"
Hi,
when I try using legend.position = "bottom", as shown in your vignette, the legend keeps staying vertical at the bottom with some weird dimension (image attached).
I'm using ggcorrplot2_0.1.1, ggplot2_3.3.5, RColorBrewer_1.1-2.
This example shows what happens:
library(ggcorrplot2)
data(mtcars)
library(psych)
ct <- corr.test(mtcars, adjust = "none")
corr <- ct$r
ggcorrplot(corr) +
theme( legend.position = "bottom")
I think I worked out the problem, in scale_fill_graidientn (and colour)
scale_fill_gradientn(colours = col2(200), limits = c(-1, 1),
guide = guide_colorbar(
title = "",
nbin = 1000,
ticks.colour = "black",
frame.colour = "black",
barwidth = 1.5,
barheight = 15))
barwidth and barheight are hindering any changes to the legend. Thus, when I try to flip it, it keeps barheight = 15 making that strange legend. Just removing those two lines fixed the problem.
Luca
Note that it is also possible to change the legend width and height within the theme() function:
theme(legend.position = ..., legend.key.height = .., legend.key.width =..)
@caijun would you be interested in adding an option 'legend.position' in the function ggcorrplot that addresses this? I tried it out on a local branch on my pc already, if you want I can upload that version somewhere next week.
LDSamson As the function ggcorrplot() returns a ggplot2 object, the legend can be customized by overriding the scale_fill_gradientn(). I think @lukelich has found the solution. I prefer not to exposure too many parameters in the function, which will increase the diffculity in usage. I am more interested in this issue.
I can understand that you want to keep the package as simple as possible, with not too many parameters in the function, I think that is a good design choice.
Just a thought: another option would be to leave out the barwidth and barheight parameters so that the ggcorrplot legend is a bit more flexible. The script would be less complex and adding theme(legend.position = "bottom/top/left/right") would behave as expected, and you can always add an option . But I also would understand if you would prefer the customized legend key size.
I will have a look at the other issue!