我试图将单独的黄土线与散点图中的两组数据相匹配,这两组数据由一个因子区分。我的数据
mydf <- data.frame(var1 = sample(c('type1', 'type2'), 12, replace = T), var2=sample(1:10, 12, replace = TRUE), var3 =c(rnorm(12)))
分别绘制我的两组数据点:
par(mfrow=c(1,1), mar=c(4,4,0.5,0.5), mgp = c(1.5, 0.3, 0), tck = -0.01)
plot(var3 ~ var2, data=mydf[mydf$var1=="type1",], xlim = c(0, 12), ylim = c(-2, 2), xlab = "x axis", ylab = "y axis", pch = 21, col = "red")
points(var3 ~ var2, data=mydf[mydf$var1=="type2",], xlim = c(0, 12), ylim = c(-2, 2), xlab = "x axis", ylab = "y axis", pch = 21, col = "slateblue")
现在问题。不知道如何告诉R只使用var1 = type2
的点#?
loess_fit <- loess(var3 ~ var2, data=mydf[mydf$var1=="type2",])
lines(mydf$var2, predict(loess_fit), col = "blue")