【问题标题】:Removing background color for column labels while keeping plot background color ggpairs删除列标签的背景颜色,同时保持绘图背景颜色 ggpairs
【发布时间】:2019-08-11 14:40:53
【问题描述】:

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

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())

这是第一次调用 plot 产生的带有颜色的图(仍然有灰色标签背景):

【问题讨论】:

    标签: r ggplot2 ggally ggpairs


    【解决方案1】:

    ::EDITED:: 这对我来说可以从构面标签中删除灰色。在函数中取出theme_void(),最后指定个性化主题。

    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
    

    可能不需要定义主题,但如果您必须制作多个主题,可能会很有用

    【讨论】:

    • 感谢您的建议,但这似乎不适合我。我已经用我得到的结果更新了问题。
    • 啊,我明白了。我知道你说你尝试了 strip.background 选项,但使用上面的代码对我有用
    • 哦——我还在你原来的函数定义中取出了你的 theme_void() 。也许这就是区别?
    • 就是这样!非常感谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-14
    • 2016-05-15
    • 1970-01-01
    相关资源
    最近更新 更多