代码之家  ›  专栏  ›  技术社区  ›  boshek

在函数中指定分组变量

  •  2
  • boshek  · 技术社区  · 10 年前

    我写了一个函数来简化我正在做的一系列相关性的可视化。具体来说,我对在 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)
    }
    

    这是我用来打印相关性统计数据的数据帧:

    grouped_cor_(data=iris, x=~Petal.Width, y=~Petal.Length)
    

    然后这是调用绘图的函数:

    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)
    

    这个功能虽然有点笨拙,但现在有一个小细节我似乎无法理解。我不知道如何为分组变量添加列规范。现在该函数与 虹膜 因为它只接受名为“species”的分组变量。我的问题是如何将这个函数从虹膜数据集中分离出来,并对分组变量进行广义化。

    有人能推荐一种有效的方法吗?很乐意接受任何改进功能的意见。

    1 回复  |  直到 10 年前
        1
  •  3
  •   IRTFM    10 年前

    这将允许您向助手函数传递单个分组因子。需要使用 group_by_ 因为我将公式中的名称提取为字符,然后强制返回为名称:

    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)
    }
    

    为了说明我在注释中所说的内容(允许函数接受一个可以由“facet_wrap”处理的公式:

    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)