不能在函数

时间:2017-02-11 15:27:32

标签: r function ggplot2 facet facet-grid

我正在尝试在函数中使用ggplot,但我无法生成 情节。具体来说,我想确定是否有情节 将使用函数调用中的facet_grid()。这是我的数据:

mydf <- data.frame(
  group = rep(c("g1", "g2"), each = 16, times = 1), 
  cluster = rep(c("c1", "c2"), each = 8, times = 2), 
  score1 = c(rnorm(n = 16, mean = 10, sd = 10), rnorm(n = 16, mean = 18, sd = 10)), 
  score2 = c(rnorm(n = 16, mean = 50, sd = 10), rnorm(n = 16, mean = 33, sd = 10))
  )

这是功能:

myFunc <- function(data, group = NULL, group2, var1, var2) {

  # So we don't need quotation marks in function call
  arguments <- as.list(match.call())
  var1 = eval(arguments$var1, data)
  var2 = eval(arguments$var2, data)
  group2 = eval(arguments$cluster, data)
  grouping = eval(arguments$group, data)

  # Make this graph if no faceting needed
  if (length(grouping) == 0) {

  means <- aggregate(cbind(var1, var2) ~  group2, FUN = mean, data = data)

  ggplot(data, aes(x = var1, y = var2, color = group2, label = group2)) + 
    stat_ellipse(type = "norm", show.legend = FALSE, geom = "polygon", alpha = 0.1) +
    geom_text(alpha = 0.5, show.legend = FALSE) +
    geom_text(data = means, aes(x = var1, y = var2, color = group2)) 


  # Use faceting
  } else if (length(grouping) > 0) {

  means <- aggregate(cbind(var1, var2) ~ grouping + group2, FUN = mean, data = data)

  # Plot 
  ggplot(data, aes(x = var1, y = var2, color = group2, label = group2)) + 
    stat_ellipse(type = "norm", show.legend = FALSE, geom = "polygon", alpha = 0.1) +
    geom_text(alpha = 0.5, show.legend = FALSE) + 
    geom_text(data = means, aes(x = var1, y = var2, color = group2)) +
    facet_grid(. ~ grouping) 

  }

}

我正在调用函数:

myFunc(data = mydf, group = NULL, group2 = cluster, var1 = score1, var2 = score2)
myFunc(data = mydf, group = group, group2 = cluster, var1 = score1, var2 = score2)

两个调用分别给出以下错误:

# Error 1
Error: Aesthetics must be either length 1 or the same as the data (32): x, y, colour, label

# Error 2
Error in combine_vars(data, params$plot_env, cols, drop = params$drop) : 
At least one layer must contain all variables used for facetting

可以通过手动构建图表来获取预期输出:

means <- aggregate(cbind(score1, score2) ~ group + cluster, FUN = mean, data = mydf)

# without facet
ggplot(mydf, aes(x = score1, y = score2, color = cluster, label = cluster)) + 
  stat_ellipse(type = "norm", show.legend = FALSE, geom = "polygon", alpha = 0.1) +
  geom_text(alpha = 0.5, show.legend = FALSE) + 
  geom_text(data = means, aes(x = score1, y = score2, color = cluster)) 

# with facet
ggplot(mydf, aes(x = score1, y = score2, color = cluster, label = cluster)) + 
  stat_ellipse(type = "norm", show.legend = FALSE, geom = "polygon", alpha = 0.1) +
  geom_text(alpha = 0.5, show.legend = FALSE) + 
  geom_text(data = means, aes(x = score1, y = score2, color = cluster)) + 
  facet_grid(. ~ group)

2 个答案:

答案 0 :(得分:2)

以下是使用和不使用stat_ellipse的基本facet_grid图。我会让你添加褶边。此处列名称保留为字符串,因此使用aes_string而不是aes,并使用as.formula将公式传递给函数。

myFunc <- function(df, var1, var2, group2, group1 = NULL) {

  # Make this graph if no faceting needed
  if (is.null(group1)) {

  means_formula <- as.formula(paste(var1, "+", var2, "~", group2))
  means <- aggregate(means_formula, FUN = mean, data = df)

   p <- ggplot(df, 
       aes_string(x = var1, y = var2, color = group2, label = group2)) + 
       stat_ellipse(type = "norm", show.legend = FALSE, 
           geom = "polygon", alpha = 0.1)
    }else{

    means_formula <- as.formula(paste(var1,"+",var2,"~", group2,"+",group1))
    means <- aggregate(means_formula, FUN = mean, data = df)

    p <- ggplot(df, 
        aes_string(x = var1, y = var2, color = group2, label = group2)) + 
        stat_ellipse(type = "norm", show.legend = FALSE, 
            geom = "polygon", alpha = 0.1) + 
        facet_grid(as.formula(paste(".~ ",group1))) 
  }
  print(p)
}

myFunc(df = mydf, var1 = "score1", var2 = "score2", 
    group2 = "cluster", group1 = NULL)

myFunc(df = mydf, var1 = "score1", var2 = "score2", 
    group2 = "cluster", group1 = "group")

stat ellipse plot

答案 1 :(得分:2)

首先,您将group2分配给函数范围中不存在的变量cluster。将group2 = eval(arguments$cluster, data)替换为group2 = eval(arguments$group2, data)

其次,您需要一个动态facet_grid公式。目前您正在传递分组,这不是数据集中的实际字段。但是,由于您在函数参数中没有引号,因此您需要检索函数参数group的字符串文字,这可以通过deparse(substitute(...))返回"group"来实现。

考虑在其他功能变量的最近列表中添加:

grpname = deparse(substitute(group))

然后使用动态facet_grid字符串连接或as.formula替换reformulate

facet_grid(as.formula(paste0(". ~ ", grpname)))

facet_grid(reformulate(grpname))

当然,所有都可以使用引用函数args动态运行,如@ P-robot所示。