I have defined a function to set the background in ggpairs to match the level of correlation between two variables. However, I would also like to remove the gray background from the variable labels running along the outside of the plot, but I'm not able to do this without also removing the correlation colors.
library(GGally)
# Loads some data
mtcars <- dput(mtcars)[,1:6]
# Defines function to color according to correlation
cor_func <- function(data, mapping, method, symbol, ...){
x <- eval_data_col(data, mapping$x)
y <- eval_data_col(data, mapping$y)
corr <- cor(x, y, method=method, use='complete.obs')
colFn <- colorRampPalette(c("brown1", "white", "dodgerblue"),
interpolate ='spline')
fill <- colFn(100)[findInterval(corr, seq(-1, 1, length = 100))]
ggally_text(
label = paste(symbol, as.character(round(corr, 2))),
mapping = aes(),
xP = 0.5, yP = 0.5,
color = 'black',
...) +
theme_void() +
theme(panel.background = element_rect(fill = fill))
}
# Following the suggestion by @Jonni
pm <- ggpairs(mtcars,
upper = list(continuous = wrap(cor_func,
method = 'spearman', symbol = expression('\u03C1 ='))),
lower = list(continuous = function(data, mapping, ...) {
ggally_smooth_lm(data = data, mapping = mapping) +
theme(panel.background = element_blank())}),
diag = list(continuous = function(data, mapping, ...) {
ggally_densityDiag(data = data, mapping = mapping) +
theme(panel.background = element_blank())}
))
pm
# All of these methods looses the correlation color in addition
# to the background color of the labels
pm + theme(strip.background = element_rect(fill = "white"))
pm + theme(strip.background = element_rect(fill = NA))
pm + theme(strip.background = element_blank())
# This only looses the correlation colors
pm + theme(panel.grid.major = element_blank(), panel.grid.minor =
element_blank())
Here is the plot with color as resulting from the first call to plot (still has the gray label background):
::EDITED:: This worked for me to remove grey from facet labels. Took out theme_void() in function and specified personalized theme at the end.
mtcars <- dput(mtcars)[,1:6]
# Defines function to color according to correlation
cor_func <- function(data, mapping, method, symbol, ...){
x <- eval_data_col(data, mapping$x)
y <- eval_data_col(data, mapping$y)
corr <- cor(x, y, method=method, use='complete.obs')
colFn <- colorRampPalette(c("brown1", "white", "dodgerblue"),
interpolate ='spline')
fill <- colFn(100)[findInterval(corr, seq(-1, 1, length = 100))]
ggally_text(
label = paste(symbol, as.character(round(corr, 2))),
mapping = aes(),
xP = 0.5, yP = 0.5,
color = 'black',
...
) + #removed theme_void()
theme(panel.background = element_rect(fill = fill))
}
pm <- ggpairs(mtcars,
upper = list(continuous = wrap(cor_func,
method = 'spearman', symbol = expression('\u03C1 ='))),
lower = list(continuous = function(data, mapping, ...) {
ggally_smooth_lm(data = data, mapping = mapping) +
theme(panel.background = element_blank())}),
diag = list(continuous = function(data, mapping, ...) {
ggally_densityDiag(data = data, mapping = mapping) +
theme(panel.background = element_blank())}
))
mytheme = theme(strip.background = element_rect(fill = "white"),panel.grid.major = element_blank(), panel.grid.minor = element_blank())
pm + mytheme
probably don't need to define the theme but could be useful if you have to make multiple of these