为什么ggplot中的这个命令会返回错误?

Wic*_*elo 8 r layer ggplot2

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

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

你知道为什么吗?

小智 7

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

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

或更常见的

p+geom_point(stat="identity")
Run Code Online (Sandbox Code Playgroud)

或者你想要尝试绘制数据.