将列名传递给函数 [英] Pass column names to a function

查看:90
本文介绍了将列名传递给函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将 ggplot()调用转换为函数?我不知道如何使R识别要传递给函数的列名称。我遇到了几个类似的问题,但是在适应想法方面还没有成功。请参见此处以代替 ()

How can I turn this ggplot() call into a function? I can't figure out how to get R to recognize the column names I want to pass to the function. I've come across several similar sounding questions, but I've not had success adapting ideas. See here for substitute().

# setup
  library(dplyr)
  library(ggplot2)
  set.seed(205)
  dat = data.frame(t=rep(1:2, each=10), 
                   pairs=rep(1:10,2), 
                   value=rnorm(20))

# working example
  ggplot(dat %>% group_by(pairs) %>%
           mutate(slope = (value[t==2] - value[t==1])/(2-1)),
         aes(t, value, group=pairs, colour=slope > 0)) +
    geom_point() +
    geom_line() +
    stat_summary(fun.y=mean,geom="line",lwd=2,aes(group=1))

# attempt at turning into a function
    plotFun <- function(df, groupBy, dv, time) {
    groupBy2 <- substitute(groupBy)
    dv2 <- substitute(dv)
    time2 <- substitute(time)
    ggplot(df %>% group_by(groupBy2) %>%
             mutate(slope = (dv2[time2==2] - dv2[time2==1])/(2-1)),
           aes(time2, dv2, group=groupBy2, colour=slope > 0)) +
      geom_point() +
      geom_line() +
      stat_summary(fun.y=mean,geom="line",lwd=2,aes(group=1))
  }

# error time
  plotFun(dat, pairs, value, t)

更新

我接受@joran的建议来查看此答案,这是我想到的:

I took @joran's advice to look at this answer, and here's what I came up with:

library(dplyr)
library(ggplot2)
library(lazyeval)

plotFun <- function(df, groupBy, dv, time) {
    ggplot(df %>% group_by_(groupBy) %>%
             mutate_(slope = interp(~(dv2[time2==2] - dv2[time2==1])/(2-1),
                                    dv2=as.name(dv), 
                                    time2=as.name(time))),
           aes(time, dv, group=groupBy, colour=slope > 0)) +
      geom_point() +
      geom_line() +
      stat_summary(fun.y=mean,geom="line",lwd=2,aes(group=1))
  }

plotFun(dat, "pairs", "value", "t")

代码可以运行,但情节不正确:

The code runs but the plot is not correct:


geom_path:每个组仅包含一个观察值。您是否需要
调整小组的审美?

geom_path: Each group consists of only one observation. Do you need to adjust the group aesthetic?


推荐答案

这是可行的解决方案通知所有评论者:

Here's the working solution informed by all of the commenters:

# setup
  library(dplyr)
  library(ggplot2)
  library(lazyeval)
  set.seed(205)
  dat = data.frame(t=rep(1:2, each=10), 
                   pairs=rep(1:10,2), 
                   value=rnorm(20))


# function
  plotFun <- function(df, groupBy, dv, time) {
      ggplot(df %>% group_by_(groupBy) %>%
             mutate_(slope = interp(~(dv2[time2==2] - dv2[time2==1])/(2-1),
                                      dv2=as.name(dv), 
                                      time2=as.name(time))),
             aes_string(time, dv, group = groupBy, 
                        colour = 'slope > 0')) +
        geom_point() +
        geom_line() +
        stat_summary(fun.y=mean,geom="line",lwd=2,aes(group=1))
  }

# plot
  plotFun(dat, "pairs", "value", "t")

这篇关于将列名传递给函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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