如何将Spearman相关p值与相关系数相加到ggpairs? [英] How to add the spearman correlation p value along with correlation coefficient to ggpairs?

查看:379
本文介绍了如何将Spearman相关p值与相关系数相加到ggpairs?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用以下代码在R中构造ggpairs图形。



df是一个数据帧,其中包含6个连续变量和一个 Group 变量

  ggpairs(df [,-1],列= 1:ncol(df [,-1]),
mapping = ggplot2 :: aes(colour = df $ Group),传说= T,axisLabels =显示,
上层=列表(连续= wrap( cor,方法=矛兵,大小= 2.5,高度= 0.7)))+
主题(面板。 grid.major = element_blank(),panel.grid.minor = element_blank(),
axis.line = element_line(colour = black))


通常,p值是使用 cor.test 通过 Spearman传递的方法



也知道StackOverFlow讨论中有类似的查询,但是我需要 ggpairs ,但解决方案不是加工。此外,先前的查询尚未解决。





我认为对于您自己的情节来说,文本间距可能不是最佳的,但这只是调整 my_fn 。


Constructing a ggpairs figure in R using the following code.

df is a dataframe containing 6 continuous variables and one Group variable

ggpairs(df[,-1],columns = 1:ncol(df[,-1]),
mapping=ggplot2::aes(colour = df$Group),legends = T,axisLabels = "show", 
upper = list(continuous = wrap("cor", method = "spearman", size = 2.5, hjust=0.7)))+ 
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
axis.line = element_line(colour = "black"))

I am trying to add the p-value of spearman correlation to the upper panel of the figure generated (i.e) appended to the Spearman correlation coefficient.

Generally, the p-value is computed using cor.test with method passed as "Spearman"

Also aware of the StackOverFlow post discussion a query similar to this, but I need for ggpairs, for which the solution is not working. Also, the previous query is not solved yet.

How to add p values for Spearman correlation coefficients plotted using pairs in R

解决方案

I have a feeling this is more than what you expected.. so you need to define a custom function like ggally_cor, so first we have a function that prints the correlation between 2 variables:

printVar = function(x,y){
      vals = cor.test(x,y,
      method="spearman")[c("estimate","p.value")]
      names(vals) = c("rho","p")
      paste(names(vals),signif(unlist(vals),2),collapse="\n")
}

Then we define a function that takes in the data for each pair, and calculates 1. overall correlation, 2. correlation by group, and pass it into a ggplot and basically only print this text:

my_fn <- function(data, mapping, ...){
  # takes in x and y for each panel
  xData <- eval_data_col(data, mapping$x)
  yData <- eval_data_col(data, mapping$y)
  colorData <- eval_data_col(data, mapping$colour)

# if you have colors, split according to color group and calculate cor

  byGroup =by(data.frame(xData,yData),colorData,function(i)printVar(i[,1],i[,2]))
  byGroup = data.frame(col=names(byGroup),label=as.character(byGroup))
  byGroup$x = 0.5
  byGroup$y = seq(0.8-0.3,0.2,length.out=nrow(byGroup))

#main correlation
mainCor = printVar(xData,yData)

p <- ggplot(data = data, mapping = mapping) +
annotate(x=0.5,y=0.8,label=mainCor,geom="text",size=3) +
geom_text(data=byGroup,inherit.aes=FALSE,
aes(x=x,y=y,col=col,label=label),size=3)+ 
theme_void() + ylim(c(0,1))
  p
}

Now I use mtcars, first column is a random Group:

df  =data.frame(
Group=sample(LETTERS[1:2],nrow(mtcars),replace=TRUE),
mtcars[,1:6]
)

And plot:

ggpairs(df[,-1],columns = 1:ncol(df[,-1]),
mapping=ggplot2::aes(colour = df$Group),
axisLabels = "show", 
upper = list(continuous = my_fn))+
theme(panel.grid.major = element_blank(), 
panel.grid.minor = element_blank(),
axis.line = element_line(colour = "black")) 

I think for your own plot, the spacing of the text might not be optimal, but it's just a matter of tweaking my_fn .

这篇关于如何将Spearman相关p值与相关系数相加到ggpairs?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆