data visualization – How to build a trendline on a graph in R
since you had some data points missing, I took what you had provided: the six points.
edit – now the full example was available
A trendline is just a regression, and regressions are run most simple way like this:
a<-lm(outcome~predictor)— in this example the object a will hold your regression parameters. To get the values of your new trendline model, just use
predict(model_name), or in your case
Adding line to a plot is dead simple. Just say
lines(b), where b specifies the line you want to plot after you have used the
To wrap it up:
[![myds <- c(23.0415,13.1965,10.4110,12.2560,9.5910,10.7160,9.9665,8.5845,8.9855,8.8920,10.3425,9.3820,9.0860,9.6870,8.5635,9.0755,8.5960,7.9485,8.3235,8.1910) x <- (1:length(myds)) plot(myds) #make the main plot plot(x,myds,ylim=c(5,30),xlim=c(0,20)) #add linear trend lines(predict(lm(myds~x)),col=green) #one more trend lines(predict(lm(myds~log(x))),col=red)]]
Since you have not provided reproducible examples, Ill post some links, which I think might help you:
An example for a simple, linear trend line is here:
Furthermore, there has been a thread on this over at SO:
How do I add different trend lines in R?
Using ggplot would make it a bit easier, as you can use the smooth functions.