我对相关矩阵使用了以下代码
library(ggplot2)
library(GGally)
ggpairs(CorrelationBINA, title="Correlation matrix of BINA dhan7",
upper = list(continuous= wrap("cor", size = 10)),
lower = list(continuous ="smooth"))
并得到以下相关矩阵。我想从矩阵的上三角形中删除单词";Corr";并且希望只保留相关值。
这采用一个用户定义的函数,计算相关性,四舍五入到两位小数,然后显示此文本,而不是默认的具有"Corr";术语:
#This function identifies correlation for each pair of variables that will go into ggpairs command written later
cor_func <- function(data, mapping, method, ...){
x <- eval_data_col(data, mapping$x)
y <- eval_data_col(data, mapping$y)
corr <- cor(x, y, method=method, use='complete.obs')
ggally_text(
label = as.character(round(corr, 2)),
mapping = aes(),
xP = 0.5, yP = 0.5,
color = 'black',
...
)
}
ggpairs(iris[-5],
upper = list(continuous = wrap(cor_func,
method = 'spearman')))