You have to change class
of group into factor
irrespective of what you want. Indeed interaction.plot()
is for two-way combinations of factors and your x
isn't factor. But if the one is continuous, interaction.plot()
gives some help. In your case, the output says "it is a foolish idea to consider an interaction with such data".
But if you want to do (I supposed you wanted a linear model) :
df <- data.frame(id = id, x = x, y = y, group = as.factor(group))
## Base plot
model <- lm(y ~ x * group, data = df)
xpara <- 20:80
plot(y ~ x, data = df, col=c(2:4)[group], pch=19)
for(i in 1:3) lines(xpara, predict(model, data.frame(x = xpara, group = as.factor(i-1))), col = i+1)
legend("topleft",paste(c("group0","group1","group2")), pch=19, lty=1, col=c(2:4))
## ggplot2 (I plotted lines and confidence intervals to interpret)
library(ggplot2)
ggplot(df, aes(x = x, y = y, colour = group)) +
geom_point(size = 4) +
geom_smooth(method = "lm", se = T, fullrange = T)

[Edited]
If class of the model supported by predict()
, the way is basically the same.
df2 <- data.frame(id = as.factor(id), x = x, y = y, group = as.factor(group))
library(nlme)
# first; make model
lme.mod <- lme(y ~ x * group, random = ~ 1|id, data = df2)
# second; get predicted values
xpara <- 20:80 # make a vector for an independent variable you use as x.
y.g1 <- predict(lme.mod, data.frame(x = xpara, group = "0", id = "1"), type="response")
y.g2 <- predict(lme.mod, data.frame(x = xpara, group = "1", id = "1"), type="response")
y.g3 <- predict(lme.mod, data.frame(x = xpara, group = "2", id = "1"), type="response")
# third; draw
plot(y ~ x, df2, col=c(2:4)[group], pch=19)
lines(xpara, y.g1, col=2)
lines(xpara, y.g2, col=3)
lines(xpara, y.g3, col=4)
## Simplified version
lev <- levels(df$group)
plot(y ~ x, data = df2, col=c(2:4)[group], pch=19, ylab="y (id = "1")")
for(i in seq.int(length(lev)))
lines(xpara, predict(lme.mod, data.frame(x = xpara, group = lev[i], id = 1)), col = i+1)
legend("topleft",paste(c("group0","group1","group2")), pch=19, lty=1, col=c(2:4))