【问题标题】:Why this command in ggplot is returning an error?为什么 ggplot 中的这个命令返回错误?
【发布时间】:2013-06-25 15:54:06
【问题描述】:

我正在学习 ggplot2,但我不明白为什么这不起作用:

p <- ggplot(diamonds, aes(x = carat))
p <- p + layer(
     geom = "point",
     stat = "identity"
)
p
Error in as.environment(where) : 'where' is missing

你知道为什么吗?

【问题讨论】:

    标签: r ggplot2 layer


    【解决方案1】:

    我认为问题在于您没有指定 y 值的用途。 ggplot2 没有与基本图形相同的默认值,用于根据索引值绘制点。要将 geom_point()stat="identity" 一起使用,您需要以下内容:

    p<-ggplot(diamonds, aes(x=carat, y=cut))
    p+layer(geom="point", stat="identity")
    

    或更常见

    p+geom_point(stat="identity")
    

    或者你想尝试绘制你的数据。

    【讨论】:

      【解决方案2】:

      通常您不会使用layer 来构建情节。相反,您使用geomstatp + geom_point() 将绘制您要查找的内容。我建议通过 gplot2 文档中的一些示例。

      【讨论】:

      • 对,但是通过使用层,我只是想了解 geom 和 stat 到底是什么以及它们如何交互
      • 将您的数据和美学移动到layer 调用中会给您一个原始对象,但我不知道如何处理它:layer(data=diamons, aes(x=caret), geom='point', stat='identity')。但是,stat='identity' 在这里显得有些多余。
      猜你喜欢
      • 2022-07-30
      • 2014-05-09
      • 2021-04-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-26
      相关资源
      最近更新 更多