【问题标题】:ggplot2 in R 3.2.3: layers broken?R 3.2.3 中的 ggplot2:图层损坏?
【发布时间】:2016-04-11 04:51:22
【问题描述】:

今天早上我升级到 R 3.2.3 (OSX 10.11.1)。我的以下类型的 ggplot2 脚本突然停止工作:

df <- data.frame(ind = c(20,35,45,55,70), dep = c(6,17,26,37,44))

语法 1

ggplot()+ layer(data=df, mapping=aes(x=ind, y=dep), geom="point")

"Error: Attempted to create layer with no stat."

ggplot2 本身并没有损坏,因为

语法 2:

ggplot(df, aes(x=ind, y=dep)) + geom_point()

产生预期的情节。语法 1 仍然在旧机器(R 2.15.3 OSX 10.5.8)上产生预期的情节。此外,我只是在今天早上 R 升级之前使用它。我已经删除并重新加载了 R3.2.3、ggplot2 和依赖项,但问题仍然存在。我喜欢语法 1,因为我喜欢添加来自不同数据集的图层。我将不胜感激。

【问题讨论】:

  • 机器是否有相同版本的ggplot2?
  • 机器没有相同版本的ggplot。可以想象我升级R版本的时候ggplot更新到v.2.0.0了。哈德利写道:“层现在对他们的论点更加严格——如果你提供的论点不是美学或参数,你会得到一个错误。这可能会导致一些短期的痛苦,但在长期——术语它将更容易发现拼写错误和其他错误 (#1293)。”我担心我已经遭受了痛苦。
  • 有趣。即使添加了stat="identity",position="identity",我也无法让它工作。它死于消息Error: is.logical(na.rm) is not TRUE。我认为这是一个错误。
  • 无论如何都不需要使用 layer(),您可以使用 geoms 轻松添加具有不同数据集的图层。
  • 这对我有用:ggplot()+ layer(data=df, mapping=aes(x=ind, y=dep), geom="point", stat = "identity", position = "identity", params = list(na.rm = FALSE))。似乎需要定义点几何图形na.rm。当您使用geom_point 时,此参数有一个默认值,但在使用layer 时似乎并非如此。我认为这是有道理的:layer 可以与任何几何图形一起使用,并且并非所有几何图形都可能具有参数 na.rm 或相同的默认值。

标签: r macos plot ggplot2


【解决方案1】:

当我们在函数层中应用 stat="identity",position_dodge(width=3) 时,大多数情况下它都可以正常工作

ggplot()+ layer(data=df, mapping=aes(x=ind, y=dep), geom="point",stat="identity",position_dodge(width=3))

#data

df <- data.frame(ind = c(20,35,45,55,70), dep = c(6,17,26,37,44))

【讨论】:

    猜你喜欢
    • 2011-03-11
    • 1970-01-01
    • 1970-01-01
    • 2017-11-16
    • 1970-01-01
    • 2019-10-16
    • 1970-01-01
    • 1970-01-01
    • 2015-03-14
    相关资源
    最近更新 更多