2

次のテストデータがあります。

structure(list(r = c(5.44702625911984, 6.3431860464319, 2.89023592667928, 
6.66260769449341, 7.5521021076857, 5.50645078944005, 6.70001850525037, 
7.39615449137166, 5.96032231142622, 7.88929821115731, 9.45119299499902, 
6.13534105776075, 7.79397401855071, 5.24488870603935, 4.53178061905952, 
5.80573244536445, 10.1194252475799, 12.5794215385996, 7.47503723957468, 
7.8682648760597, 15.7540766770233, 14.9800818974568, 14.5672865569748, 
9.5347507057429, 18.6791666362954, 10.4588651710497, 15.2076130678251, 
10.5052588219606, 13.1314628288852, 12.8384811800557, 10.9978569438483, 
10.0197995395016, 10.1479274794689, 12.5864754383382, 10.7985399338233, 
11.1100572430765, 10.756576992292, 9.17309427876051, 10.0441987112265, 
10.0652520950654), f1 = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L), .Label = c("H", "L"), class = "factor"), f2 = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L), class = "factor", .Label = c("Joe", 
"Sally"))), .Names = c("r", "f1", "f2"), row.names = c(NA, -40L
), class = "data.frame")

そして、ポイントを描画し(そうします)、各グループの手段を接続する(そうではありません)次の関数:

testFunc <- function(formula = NULL, data = NULL) {
    res <- as.character(formula[[2]])
    fac1 <- as.character(formula[[3]][2])
    fac2 <- as.character(formula[[3]][3])

    # Specify the data & aesthetics     
    p <- ggplot(data, aes_string(x = fac1, y = res, color = fac2,
        group = fac2))

    # Now add points
    p <- p + geom_point() # works fine if we stop here

    # Due to a bug in ggplot2_0.9.3, we must calc some quantities
    # and put them in a separate data frame for a new aesthetic
    avg <- aggregate(data[,res] ~ data[,fac1]*data[, fac2], data, FUN = mean)
    names(avg) <- c("factor1", "factor2", "mean")   
    p <- p + geom_line(aes_string(x = 'factor1', y = 'mean', group = 'factor2'), data = avg)
    }

私がそれを実行すると:

ex <- testFunc(formula = r ~ f1*f2, data = td)
print(ex)

次のエラーが表示されます。

Error in eval(expr, envir, enclos) : object 'f2' not found

私は常にこれらのスコーピングの問題を抱えているようですが、何かアドバイスはありますか? バグの適切な回避策に従っていると思いました。aesの代わりに使用aes_stringしたり、変数名を引用符で囲んだりしても、geom_line問題は解決しません。ありがとう。

4

2 に答える 2

1

継承された美学で問題が発生した場合、私はいつも戻って、ggplot()そこにある必要のないメイン コールからすべてを削除します。これを試して:

testFunc <- function(formula = NULL, data = NULL) {
    res <- as.character(formula[[2]])
    fac1 <- as.character(formula[[3]][2])
    fac2 <- as.character(formula[[3]][3])

    # Now add points
    p <- ggplot() + geom_point(data = data, aes_string(x = fac1, y = res, color = fac2,
        group = fac2)) # works fine if we stop here

    # Due to a bug in ggplot2_0.9.3, we must calc some quantities
    # and put them in a separate data frame for a new aesthetic
    avg <- aggregate(data[,res] ~ data[,fac1]*data[, fac2], data, FUN = mean)
    names(avg) <- c("factor1", "factor2", "mean")   
    p <- p + geom_line(aes_string(x = 'factor1', y = 'mean', group = 'factor2'), data = avg)
    }
于 2013-01-15T23:28:59.177 に答える
0

関数を機能させるには、2 つの変更を加える必要がありました。まず、ggplot2 0.9.3 (およびそれ以前) には、aes()デフォルトの評価環境がグローバル環境になるというバグがあります。回避策: 呼び出し環境 (関数の環境など) で評価するには、 を使用しますggplot(..., environment = environment()

もう 1 つの変更は、最後の行にありましたp <- p + geom_line(..., data=avg)ggplotここで重要なのは、メインコールからの美学の継承をオフにする必要があるということです。これは、データ セットに列avgがないためです。fac2これを行うには、 を使用しますgeom_line(..., inherit.aes=FALSE)

testFunc <- function(formula = NULL, data = NULL) {
  res <- as.character(formula[[2]])
  fac1 <- as.character(formula[[3]][2])
  fac2 <- as.character(formula[[3]][3])

  # Specify the data & aesthetics     
  p <- ggplot(data, aes_string(x = fac1, y = res, color = fac2, group = fac2),
    environment = environment())

  # Now add points
  p <- p + geom_point()

  # Due to a bug in ggplot2_0.9.3, we must calc some quantities
  # and put them in a separate data frame for a new aesthetic
  avg <- aggregate(data[,res] ~ data[,fac1]*data[, fac2], data, FUN = mean)
  names(avg) <- c("factor1", "factor2", "mean")   
  p + geom_line(aes_string(x = 'factor1', y = 'mean', group = 'factor2'), 
    inherit.aes = FALSE, data = avg)
}

ex <- testFunc(formula = r ~ f1*f2, data = td)
print(ex)
于 2013-01-24T06:27:34.783 に答える