I saw these posts GGally::ggpairs plot without gridlines when plotting correlation coefficient use ggpairs to create this plot
After reading I was able to implement
You can modify some parameters of GGally functions by using wrap()
as explained here. But not all parameters are named for wrap
to be useful. For example, if you try to change default palette with a manual colour scale within wrap
you may get an error like Error in wrap("cor",…) all parameters must be named arguments
. In that case you may build custom functions to generate any sort of ggplot object appropriate for the upper, lower or diagonal sections of the matrix plot.
However, there is a (safer) shortcut if you want to change some parameters (not named in GGally functions to be wrap
ped) without creating a custom function to design a ggplot object. You just call an already existing GGally function within a function call, adding the extra ggplot parameters. For example, to provide a manual scale colour for three categories (in the new column swiss$groups):
swiss$groups <- gl(n = 3, k = 1, length = nrow(swiss), labels = c("A", "B", "C"))
ggpairs(swiss, mapping = aes(colour = groups), columns = 1:6,
upper = list(continuous = function(data, mapping, ...) {
ggally_cor(data = data, mapping = mapping, size = 2) + scale_colour_manual(values = c("black", "dark green", "red"))}),
lower = list(continuous = function(data, mapping, ...) {
ggally_smooth(data = data, mapping = mapping, alpha = .2) + scale_colour_manual(values = c("black", "dark green", "red"))}),
diag = list(continuous = function(data, mapping, ...) {
ggally_barDiag(data = data, mapping = mapping, alpha = .5) + scale_fill_manual(values = c("black", "dark green", "red"))}))