删除列标签的背景颜色,同时保留绘图背景颜色 ggpairs
Removing background color for column labels while keeping plot background color ggpairs
我定义了一个函数来设置 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 后的颜色图(仍然有灰色标签背景):
::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
可能不需要定义主题,但如果您必须制作多个主题可能会很有用
我定义了一个函数来设置 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 后的颜色图(仍然有灰色标签背景):
::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
可能不需要定义主题,但如果您必须制作多个主题可能会很有用