R 在函数中指定分组变量

R 在函数中指定分组变量,r,function,ggplot2,grouping,correlation,R,Function,Ggplot2,Grouping,Correlation,我写了一个函数来简化我正在做的一系列关联的可视化。具体来说,我感兴趣的是在ggplot2面板中并排查看双变量关系,p值和rho值直接打印在图形上。我使用iris数据集编写了此函数: library(ggplot2) library(dplyr) grouped_cor_ <- function(data, x, y, group.col){ x <- lazyeval::as.lazy(x) y <- lazyeval::as.lazy(y) cor1 <-

我写了一个函数来简化我正在做的一系列关联的可视化。具体来说,我感兴趣的是在
ggplot2
面板中并排查看双变量关系,p值和rho值直接打印在图形上。我使用
iris
数据集编写了此函数:

library(ggplot2)
library(dplyr)

grouped_cor_ <- function(data, x, y, group.col){
  x <- lazyeval::as.lazy(x)
  y <- lazyeval::as.lazy(y)
  cor1 <- lazyeval::interp(~ cor.test(x, y,method="spearman",na.action = "na.exclude")$estimate, x = x, y = y)
  corp <- lazyeval::interp(~ cor.test(x, y,method="spearman", na.action = "na.exclude")$p.value, x = x, y = y)
  mnx <- lazyeval::interp(~ mean(x, na.rm=TRUE), x = x, y = y)
  mny <- lazyeval::interp(~ mean(y, na.rm=TRUE), x = x, y = y)

  summarise_(group_by(data, Species), rho=cor1, pval=corp, xcoord=mnx, ycoord=mny)
}
然后,这是调用绘图的函数:

corHighlight <- function(Data, x, y){
  cordf<-grouped_cor_(Data, x = substitute(x), y = substitute(y))
  cordf$prho <- paste("rho=",round(cordf$rho,3), "\n p-value=",round(cordf$pval,3), sep=" ")
  plt<-ggplot(Data, aes_q(x = substitute(x), y = substitute(y))) + 
    geom_text(data=cordf, aes_q(x=substitute(xcoord), 
                            y=substitute(ycoord), 
                            label=substitute(prho)), colour='red') + 
    geom_point(size=2, alpha=0.3) +
    facet_wrap(~Species)
  print(plt)
}


corHighlight(Data=iris, 
         x=Petal.Width, 
         y=Petal.Length)

corHighlight这将允许您向助手函数传递单个分组因子。确实需要使用
group\u by_uu
,因为我从公式中提取名称作为字符,然后强制将其恢复为名称:

grouped_cor_ <- function(data, x, y, form){
  x <- lazyeval::as.lazy(x)
  y <- lazyeval::as.lazy(y); fac <- as.name(as.character(form)[2])
  cor1 <- lazyeval::interp(~ cor.test(x, y,method="spearman",na.action = "na.exclude")$estimate, x = x, y = y)
  corp <- lazyeval::interp(~ cor.test(x, y,method="spearman", na.action = "na.exclude")$p.value, x = x, y = y)
  mnx <- lazyeval::interp(~ mean(x, na.rm=TRUE), x = x, y = y)
  mny <- lazyeval::interp(~ mean(y, na.rm=TRUE), x = x, y = y)

  summarise_( group_by_(data, fac), rho=cor1, pval=corp, xcoord=mnx, ycoord=mny)
}

grouped\u cor\uu难道你不能把一个公式参数传入
corHighlight
?如果你有两个因素,它可能是
form=~facA+facB
。我不太明白这是如何指定列的。可以详细说明吗?如果你想要一个经过测试的答案,那么就生成一个具有所需结构的数据集。我想我已经做到了。问题是我不知道如何在函数中指定分组变量。结果数据集是我想要的,但是创建它的函数是特定于iris数据集的。我希望函数中有一个参数指定分组列。我假设您希望指定两个因子,一个是行,另一个是列。这确实适用于
corHighlight
函数,但使用相同的逻辑,我似乎无法将其应用于
grouped\u cor
函数。有没有关于如何将它扩展到那里的想法?如果你只需要它接受一个分组名称,我可以很容易地做到。够了吗?
grouped_cor_ <- function(data, x, y, form){
  x <- lazyeval::as.lazy(x)
  y <- lazyeval::as.lazy(y); fac <- as.name(as.character(form)[2])
  cor1 <- lazyeval::interp(~ cor.test(x, y,method="spearman",na.action = "na.exclude")$estimate, x = x, y = y)
  corp <- lazyeval::interp(~ cor.test(x, y,method="spearman", na.action = "na.exclude")$p.value, x = x, y = y)
  mnx <- lazyeval::interp(~ mean(x, na.rm=TRUE), x = x, y = y)
  mny <- lazyeval::interp(~ mean(y, na.rm=TRUE), x = x, y = y)

  summarise_( group_by_(data, fac), rho=cor1, pval=corp, xcoord=mnx, ycoord=mny)
}
corHighlight <- function(Data, x, y, form){
  cordf<-grouped_cor_(Data, x = substitute(x), y = substitute(y), form=substitute(form))
  cordf$prho <- paste("rho=",round(cordf$rho,3), "\n p-value=",round(cordf$pval,3), sep=" ")
  plt<-ggplot(Data, aes_q(x = substitute(x), y = substitute(y))) + 
    geom_text(data=cordf, aes_q(x=substitute(xcoord), 
                            y=substitute(ycoord), 
                            label=substitute(prho)), colour='red') + 
    geom_point(size=2, alpha=0.3) +
    facet_wrap(form)
  print(plt)
}
corHighlight(Data=iris, 
         x=Petal.Width, 
         y=Petal.Length, form = ~Species)