I have a simple data set with two columns of data- K and SwStr.
K = c(.259, .215, .224, .223, .262, .233)
SwStr = c(.130, .117, .117, .114, .113, .111)
I plotted the data using:
plot(res$K, res$SwStr)
I want to plot the result of a linear model, using SwStr to predict K. I try to do that using:
graphic<-lm(K~SwStr-1, data=res)
P=predict(graphic)
plot(res$K, res$SwStr)
lines(P, lty="dashed", col="green", lwd=3)
But when I do this, I don’t get any line plotted. What am I doing wrong?
(1) You are inverting the axes of the original plot. If you want
SwStron the x axis andKon the y axis you needor
If you check the actual values of the plotted points on the graph, this might be obvious (especially if
KandSwStrhave different magnitudes) …For
lmfits you can also useabline(graphic,...)edit: (2) You also have to realize that
predictgives just the predicted y values, not the x values. So you want something like this:Depending on the situation, you may also want to use the
newdataargument topredictto specify a set of evenly spaced x values …