gpt4 book ai didi

删除列标签的背景颜色,同时保留绘图背景颜色 ggpairs

转载 作者:行者123 更新时间:2023-12-02 08:52:19 25 4
gpt4 key购买 nike

我定义了一个函数来设置 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())

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

enter image description here

最佳答案

::编辑::这对我来说可以从面标签中删除灰色。函数中取出了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,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57450913/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com