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.
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.This means that
geom_point
is looking for avariable
column ind2
. It won't look fory
orvalue
columns, since you explicitly re-mapped the x and y aesthetics ingeom_point()
.The solution is either to move the top level aesthetic mappings from
ggplot()
togeom_line()
, or to un-mapgroup
ingeom_point
usinggroup = NULL
.Additionally, Didzis pointed out a third solution in the comments, which is to add
inherit.aes = FALSE
togeom_point
.