plot - R add loess line to subset of data in scatterplot -
i trying fit separate loess lines 2 sets of data in scatterplot distinguished factor. data
mydf <- data.frame(var1 = sample(c('type1', 'type2'), 12, replace = t), var2=sample(1:10, 12, replace = true), var3 =c(rnorm(12)))
scatterplot 2 groups of datapoints separately:
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")
now problem. not sure how tell r use points var1 = type2
#? loess_fit <- loess(var3 ~ var2, data=mydf[mydf$var1=="type2",]) lines(mydf$var2, predict(loess_fit), col = "blue")
Comments
Post a Comment