【问题标题】:ggtern + geom_interpolate_tern + expand.formula with unexpected outputggtern + geom_interpolate_tern + expand.formula 与意外输出
【发布时间】:2017-09-13 21:58:48
【问题描述】:

这是my previous question的后续报道,

所以给定以下数据,

> foo
Resp         A         B         C
1  1.629 0.3333333 0.3333333 0.3333333
2  1.734 0.1666667 0.6666667 0.1666667
3   1.957 0.0000000 1.0000000 0.0000000
4  1.778 1.0000000 0.0000000 0.0000000
5  1.682 0.6666667 0.1666667 0.1666667
6  1.407 0.1666667 0.1666667 0.6666667
7  1.589 0.0000000 0.5000000 0.5000000
8  1.251 0.0000000 0.0000000 1.0000000
9  1.774 0.5000000 0.5000000 0.0000000
10 1.940 0.5000000 0.0000000 0.5000000
>

我正在尝试从this article 复制图表(私人访问)。文章声称使用special cubic model

但是,当我尝试使用符号 value ~ (x + y + z)^3 -1 时,我得到了

object 'z' not found

我假设是因为z 线性依赖于xy

当我尝试仅使用 xy 重新创建特殊立方模型时,我尝试将 cubicSquad 函数与 expand.formula 一起使用,

      > expand.formula(Resp ~ cubicS(A,B) + quad(A,B))
       Resp ~ (A + B)^3 + I(A * B * (A - B)) + (A + B)^2 + I(A^2) + I(B^2)
      > 

但是,geom_interpolate_tern 会说我使用了太多预测变量,

    foo <-
      structure(
        list(
          Resp = c(1.629, 1.734, 1.957, 1.778, 1.682, 1.407,
                   1.589, 1.251, 1.774, 1.94),
          A = c(0.3333333, 0.1666667, 0, 1,
                0.6666667, 0.1666667, 0, 0, 0.5, 0.5),
          B = c(0.3333333, 0.6666667,
                1, 0, 0.1666667, 0.1666667, 0.5, 0, 0.5, 0),
          C = c(0.3333333,
                0.1666667, 0, 0, 0.1666667, 0.6666667, 0.5, 1, 0, 0.5)
        ),
        .Names = c("Resp",
                   "A", "B", "C"),
        class = "data.frame",
        row.names = c("1", "2",
                      "3", "4", "5", "6", "7", "8", "9", "10")
      )

    ggtern(data=foo,aes(y = A,x = B,z = C)) +
      geom_interpolate_tern(
        data = foo,
        mapping = aes(
          value = Resp,color=..level..
        ),
        formula = expand.formula(value ~ cubicS(x,y) + quad(x,y)),
        base = "identity"
      ) 

输出:

    Warning messages:
      1: In structure(c(), class = c(class(x), class(y))) :
      Calling 'structure(NULL, *)' is deprecated, as NULL cannot have attributes.
    Consider 'structure(list(), *)' instead.
    2: Computation failed in `stat_interpolate_tern()`:
      only 1-4 predictors are allowed 

【问题讨论】:

  • 默认使用loess,需要在interpolate tern函数中指定method=lm

标签: r interpolation ggtern


【解决方案1】:

默认情况下,插值方法是“黄土”,以与ggplot2 中的默认平滑方法保持一致,例如geom_smooth(...)。由于黄土回归的预测变量限制,此错误被抛出。

没关系,这很容易解决,改为指定method = lm。我添加了彩色点以查看模型如何与您的数据相匹配。

ggtern(data=foo,aes(y = A,x = B,z = C)) +
  geom_point(aes(color=Resp)) + 
  geom_interpolate_tern(
    data = foo,
    mapping = aes(
      value = Resp,color=..level..
    ),
    method=lm,   # <<<<<< SPECIFY METHOD HERE <<<<<<<
    formula = expand.formula(value ~ cubicS(x,y) + quad(x,y)),
    base = "identity"
)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-05-16
    • 2013-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-25
    • 2021-09-12
    相关资源
    最近更新 更多