ggplot frienndly corrlation plots with ggcorrplot
A co-worker ran into issues with corrplot::corrplot()
cutting off the title. A useful alternative is ggcorrplot
. It makes okay plots with ggplot2’s logic. Not as clean as the above but it’ll work with patchwork
and cowplot
. Unfortunately, scale_colour_stepsn
doesn’t override the scaling.
library(ggcorrplot)
<- cor_pmat(cor_df)
p.mat ggcorrplot(cor(cor_df, use = "pairwise.complete.obs"),
p.mat = p.mat,
insig = "blank",
type = "upper",
outline.col = "white",
colors = RColorBrewer::brewer.pal(n = 9, name = "PuOr")[c(1,5,9)]
+
)labs(title = "Brian_AP_Delayed")
ggcorrplot
appears to call internal functions which makes modifying it quickly impractical (one would probably be best forking the package and modifying that). I think I have a workaround that gets the same binning behavior:
After the significance matrix (p.mat
) is generated overwrite the correlation matrix with the middle value of each desired bin.
bkkca cav1 cav21.0000000 0.3452702 0.5603564
bkkca 0.3452702 1.0000000 0.7880727
cav1 0.5603564 0.7880727 1.0000000
cav2 > # bin the correlations so there are fewer colors used in the figure
> cor_bins <- seq(-1, 1, length.out = 9)
> for (i in 1:(length(cor_bins)-1)){
+ test[test > cor_bins[i] & test < cor_bins[i+1]] <- ((cor_bins[i] + cor_bins[i+1])/2)
+ }
> test
bkkca cav1 cav21.000 0.375 0.625
bkkca 0.375 1.000 0.875
cav1 0.625 0.875 1.000 cav2
Here this makes very slight changes to the plot. (legend dropped to not imply a continuous fill)
(2021-2-2) Last update, this is harder to read up will use the more extreme value to get closer to corrplot
<- seq(-1, 1, length.out = 5)+.0000001
test
test#-0.9999999 -0.4999999 0.0000001 0.5000001 1.0000001
for (i in 1:(length(cor_bins)-1)){
# test[test > cor_bins[i] & test < cor_bins[i+1]] <- (cor_bins[i] + cor_bins[i+1])
> cor_bins[i] & test < cor_bins[i+1]] <- cor_bins[c(i, (i+1))[which.max(abs(cor_bins[i:(i+1)]))]]
test[test
}
test#-1.00 -0.50 0.25 0.75 1.00