使用可选参数在R中编写ggplot函数

使用可选参数在R中编写ggplot函数,r,ggplot2,R,Ggplot2,我有一系列的ggplot图,我重复了一些小的变化。我想将这些Qplot及其选项包装到一个函数中,以避免代码中的大量重复 我的问题是,对于某些图,我使用+facet_wrap()选项,但对于其他图,我没有。也就是说,我需要facet wrap作为可选参数。当包含它时,代码需要使用facets参数中提供的变量调用+facet_wrap() 理想情况下,我的函数应该是这样的,facets是一个可选参数: $ qhist(variable, df, heading, facets) 我在谷歌上搜索过如

我有一系列的ggplot图,我重复了一些小的变化。我想将这些Qplot及其选项包装到一个函数中,以避免代码中的大量重复

我的问题是,对于某些图,我使用+facet_wrap()选项,但对于其他图,我没有。也就是说,我需要facet wrap作为可选参数。当包含它时,代码需要使用facets参数中提供的变量调用+facet_wrap()

理想情况下,我的函数应该是这样的,facets是一个可选参数:

$ qhist(variable, df, heading, facets)
我在谷歌上搜索过如何添加可选参数,他们建议要么传递默认值,要么使用带有missing()函数的if循环。我两个都没能去上班

这是我编写的函数,其中还包括可选facets参数所需的功能

$ qhist <- function(variable, df, heading, facets) {
      qplot(variable, data = df, geom = "histogram", binwidth = 2000, 
            xlab = "Salary", ylab = "Noms") + 
      theme_bw() +
      scale_x_continuous(limits=c(40000,250000), 
                 breaks=c(50000,100000,150000,200000,250000), 
                 labels=c("50k","100k","150k","200k","250k")) +
      opts(title = heading, plot.title = theme_text(face = "bold", 
           size = 14), strip.text.x = theme_text(size = 10, face = 'bold')) 
      # If facets argument supplied add the following, else do not add this code
      + facet_wrap(~ facets)

$qhist设置默认值的方法如下:

testFunction <- function( requiredParam, optionalParam=TRUE, alsoOptional=123 ) {
  print(requiredParam)
  if (optionalParam==TRUE) print("you kept the default for optionalParam")
  paste("for alsoOptional you entered", alsoOptional)
}
qhist <- function(variable, df, heading, facets) {
  ... insert @JD Longs' solution ...

  if (!missing(facets)) d <- d + facet_wrap(as.formula(paste("~", facets)))
  return(d)
}

testFunction注意,您还可以使用
missing(facets)
检查facets参数是否已指定。如果您使用@JD Long的解决方案,它将如下所示:

testFunction <- function( requiredParam, optionalParam=TRUE, alsoOptional=123 ) {
  print(requiredParam)
  if (optionalParam==TRUE) print("you kept the default for optionalParam")
  paste("for alsoOptional you entered", alsoOptional)
}
qhist <- function(variable, df, heading, facets) {
  ... insert @JD Longs' solution ...

  if (!missing(facets)) d <- d + facet_wrap(as.formula(paste("~", facets)))
  return(d)
}

qhist也许,最好的方法是停止使用这种不寻常的变量名,包括逗号或空格

作为解决方法,这里是@JDLong答案的扩展。诀窍是重命名facet变量

f <- function(dat, facet = NULL) {
    if(!missing(facet)) {
        names(dat)[which(names(dat) == facet)] <- ".facet."
        ff <- facet_wrap(~.facet.)
    } else {
        ff <- list()
    }
    qplot(x, y, data = dat) + ff
}

d <- data.frame(x = 1:10, y = 1:10, "o,o" = gl(2,5), check.names=F)

f(d, "o,o")
f(d)

f
facets
是否符合逻辑?还是别的什么?(面向方面的变量的字符向量?)不,它将是面向方面的变量。例如,我有工资数据柱状图,有时我想按行业划分。干杯,我想我明白了,我想我仍然不明白如何使用该结构在函数调用的代码中包含选项。我将更新我的问题以反映这一点。在函数体中,考虑到
qplot
变量的非标准计算,请尝试类似于
res的方法,您最好使用
ggplot
语法并使用
aes_string
d@JD Long好极了,我会尝试一下,看看是否能让它工作。@JD Long这很有效。唯一的问题似乎与发送到as.formula()函数的字符串有关。如果传递的文本中有逗号或空格,则会失败。当我在parse(text=x)中包含一个方面:
error:1:10:意外符号1:~Retail Trade^
很抱歉,“Retail Trade”实际上是变量中的字符串之一。变量本身名为“industry”。因此,它不会导致任何错误。也许在你的使用方法中有一些错误。你能附上一个可复制的例子吗?你说得对。我的问题是我没有将参数作为字符串封装。感谢您提供了另一个选项。我已经看到了这一点,但我不知道如何将其包含在我的特定函数中。