附加geom_point与ggplot组=变量 [英] additional geom_point with ggplot group = variable
问题描述
是否可以包含group = variable并在ggplot中添加另一个点。
Is it possible to include group = variable and add another point in ggplot.
例如:
For example:
require(MuMIn)
require(ggplot2)
data(Cement)
d <- data.frame(Cement)
dd <- melt(d,id.var = "y")
d2 <- runif(length(dd[,1]))
d2 <- data.frame(first = dd$y,
second = d2)
ggplot(dd, aes(x = y,y = value,group = variable)) +
geom_line() +
geom_point(data = d2,aes(x = first,y = second))
这会导致错误。最终我想将这里指定的点添加到线条图上。
This leads to an error. Eventually I would like to add the points specified here onto the line plot.
推荐答案
ggplot()
中,它们会自动传递到所有后续图层。
Mapped aesthetics in ggplot2 follow a sort of inheritance pattern. When you map aesthetics at the "top" level, in ggplot()
, they are automatically passed down to all subsequent layers.
这意味着 geom_point
正在寻找 d2 中的
变量
code>。它不会查找 y
或值
列,因为您明确地将x和y美学重新映射到 geom_point()
。
This means that geom_point
is looking for a variable
column in d2
. It won't look for y
or value
columns, since you explicitly re-mapped the x and y aesthetics in geom_point()
.
解决方案是将顶级审美映射从 ggplot ()
到 geom_line()
,或者 un-map group
geom_point 使用 group = NULL
。
The solution is either to move the top level aesthetic mappings from ggplot()
to geom_line()
, or to un-map group
in geom_point
using group = NULL
.
另外,Didzis在评论中指出了第三种解决方案,即将 inherit.aes = FALSE
添加到 geom_point
。
Additionally, Didzis pointed out a third solution in the comments, which is to add inherit.aes = FALSE
to geom_point
.
这篇关于附加geom_point与ggplot组=变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!