将变量作为参数传递给plot_ly函数 [英] Pass variables as parameters to plot_ly function

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

问题描述

我想创建一个函数,该函数根据传递给它的参数创建不同种类的绘图.如果我创建以下数据

I would like to create a function that creates different kinds of plotly plots based on the parameters that are passed into it. If I create the following data

library(plotly)
#### test data
lead <- rep("Fred Smith", 30)
lead <- append(lead, rep("Terry Jones", 30))
lead <- append(lead, rep("Henry Sarduci", 30))
proj_date <- seq(as.Date('2017-11-01'), as.Date('2017-11-30'), by = 'day')
proj_date <- append(proj_date, rep(proj_date, 2))
set.seed(1237)
actHrs <- runif(90, 1, 100)
cummActHrs <- cumsum(actHrs)
forHrs <- runif(90, 1, 100)
cummForHrs <- cumsum(forHrs)
df <- data.frame(Lead = lead, date_seq = proj_date,
                 cActHrs = cummActHrs,
                 cForHrs = cummForHrs)

我可以使用以下方式绘制它:

I could plot it using:

plot_ly(data = df, x = ~date_seq, y = ~cActHrs, split = ~Lead)

如果我像下面显示的那样制作了一个 makePlot 函数,我将如何做这样的事情:

If I made a makePlot function like the one shown below, how would I make it do something like this:

makePlot <- function(plot_data = df, x_var = date_seq, y_var, split_var) {
    plot <- plot_ly(data = df, x = ~x_var, y = ~y_var, split = ~split_var)

    return(plot)
}

?

是否有一个函数可以包装x_var,y_var和split_var,以便可以将它们识别为x,y和split参数?

Is there a function I can wrap x_var, y_var, and split_var with so that plotly will recognize them as x, y, and split parameters?

推荐答案

最终解决了这个问题,并希望这个小小的后续工作对这些类型的任务有所神秘感.尽管这个问题的重点是绘图,但重要的是首先要了解各种R包(例如dplyr和plotly)中的函数如何评估表达式以及如何操纵这些表达式的评估方式. Hadley在dplyr 此处此处.

Eventually got around to figuring this out and hope this little follow up takes some of the mystery of these types of tasks. Although this question is focused on plotting, it's important to first build an understanding of how the functions in various R packages (e.g. dplyr and plotly) evaluate expressions and how to manipulate the way those expressions are evaluated. A great reference to build this understanding is Hadley's article on programming in dplyr here or alternatively here.

一旦这一切都在您的掌控之下,事实证明这很容易.技巧是像调用dplyr函数时一样简单地传递变量参数,并确保在绘图函数中引用这些参数.对于上述问题,此功能对我有用:

Once that's under your belt, this turns out to be pretty easy. The trick is to simply pass your variable arguments like you do when you call dplyr functions and be sure to quote those parameters inside your plotting function. For the question above, this function worked for me:

makePlot <- function(plot_data = df, x_var, y_var, split_var,
                     type_var="scatter",
                     mode_var="lines+markers") {
    quo_x <- enquo(x_var)
    quo_y <- enquo(y_var)
    quo_split <- enquo(split_var)

    # print(c(quo_x, quo_y, quo_split))

    plot <- plot_ly(data = df, x = quo_x, y = quo_y, split = quo_split,
                    type=type_var, mode=mode_var)

    return(plot)
}

# using df created in question, pass col's as args like dplyr functions
p1 <- makePlot2(df, date_seq, cActHrs, Lead)
p2 <- makePlot2(df, date_seq, cForHrs, Lead)

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

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