删除列标签的背景色,同时保持绘图背景色ggpairs [英] Removing background color for column labels while keeping plot background color ggpairs

查看:138
本文介绍了删除列标签的背景色,同时保持绘图背景色ggpairs的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经定义了一个函数来设置ggpairs中的背景,以匹配两个变量之间的相关程度. 但是,我也想从沿图的外部延伸的变量标签中删除灰色背景,但是如果不删除相关颜色,就无法做到这一点.

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 ::这对我来说可以从构面标签上去除灰色.在函数中使用了theme_void(),并在最后指定了个性化主题.

::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

这篇关于删除列标签的背景色,同时保持绘图背景色ggpairs的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆