gpt4 book ai didi

r - 如何在散点图矩阵中留下不显着值的空白单元格?

转载 作者:行者123 更新时间:2023-12-04 07:38:47 25 4
gpt4 key购买 nike

我有以下情节 enter image description here
我试图让这个图像这样(手动编辑)有不重要的单元格是空白的 enter image description here
我正在使用这些 r 代码;

leg <- grab_legend(ggplot(data=data1, aes(x=NA, y=NA, colour=Si)) +
geom_line(size=2) +
theme(legend.direction="horizontal",
#change legend title font size
legend.title = element_text(size=18),
#change legend text font size
legend.text = element_text(size=14),
#change legend key size
legend.key.size = unit(1, 'cm')))

my_fn <- function(data1, mapping, method="p", use="pairwise", ...){

# grab data
x <- eval_data_col(data1, mapping$x)
y <- eval_data_col(data1, mapping$y)

# calculate correlation
corr <- cor(x, y, method=method, use=use)

# calculate colour based on correlation value
# Here I have set a correlation of minus one to blue,
# zero to white, and one to red
# Change this to suit: possibly extend to add as an argument of `my_fn`
colFn <- colorRampPalette(c("blue", "white", "red"), interpolate ='spline')
fill <- colFn(100)[findInterval(corr, seq(-1, 1, length=100))]

ggally_cor(data=data1, size=5, mapping=mapping, ...) +
theme_void() +
theme(panel.background=element_rect(fill=fill))
}

ggpairs(
data1, columns=3:7, legend=leg,
upper = list(continuous=my_fn),
) +
theme(legend.position='top')+
theme(axis.text.x=element_text(angle=90, hjust=1, vjust=0.4, size=12),
axis.text.y=element_text(size=12),
strip.text = element_text(size = 14))
数据链接 https://docs.google.com/spreadsheets/d/1q2iJ1RsRDfZQiT0UskB8PhFDU-_qTDMh2DYv9paE3bM/edit?usp=sharing
我的问题是,
我怎样才能用那些空白的单元格绘制具有非显着值的图?谢谢

最佳答案

感谢 Barret Schloerke ( https://github.com/schloerke ),通过在 function(...) 中添加返回调用,我能够找到上述问题的答案。

my_fn <- function(.....) {
.....
if (abs(corr) < threshold) {
return(ggally_blank())
}
.....
}
定义 function(...)电话就是这样;
my_fn <- function(data1, mapping, threshold=0.34, method="p", use="pairwise", ...){

# grab data
x <- eval_data_col(data1, mapping$x)
y <- eval_data_col(data1, mapping$y)

# calculate correlation
corr <- cor(x, y, method=method, use=use)

# calculate colour based on correlation value
# Here I have set a correlation of minus one to blue,
# zero to white, and one to red
# Change this to suit: possibly extend to add as an argument of `my_fn`
colFn <- colorRampPalette(c("blue", "white", "red"), interpolate ='spline')
fill <- colFn(100)[findInterval(corr, seq(-1, 1, length=100))]

if (abs(corr) < threshold) {
return(ggally_blank())
}

ggally_cor(data=data1, digits=2, size=5, mapping=mapping, ...) +
theme_void() +
theme(panel.background=element_rect(fill=fill))
}


ggpairs(
data1, columns=3:7,
upper = list(continuous=my_fn), ) +
theme(legend.position='top')+
theme(axis.text.x=element_text(angle=90, hjust=1, vjust=0.4, size=12),
axis.text.y=element_text(size=12),
strip.text = element_text(size = 14))
enter image description here
我希望它会对其他人有所帮助。

关于r - 如何在散点图矩阵中留下不显着值的空白单元格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67602536/

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