[R] How to plot linear, cubic and quadratic fitting curve in a figure?

Kenneth Frost kfrost at wisc.edu
Wed Jun 13 17:03:35 CEST 2012


Hi, Kristi-
Here is an option.
?curve...to add the polynomials to your graph
?text....for adding the R2 to you plot
Ken


On 06/13/12, Kristi Glover  wrote:
> 
> Hi R experts, 
> Could you please help me to fit a linear, cubic and quadratic curve in a figure? I was trying to show all these three fitting curves with different colour in one figure. 
> I spent substantial time to figure it out, but I could not. 
> 
> I have given here a example and what I did for linear, but no idea for cubic and quadratic fitting curve
> 
> 
> > dput(test)
> structure(list(sp = c(4L, 5L, 9L, 12L, 14L), env = c(12L, 18L, 
> 20L, 17L, 15L)), .Names = c("sp", "env"), class = "data.frame", row.names = c(NA, 
> -5L))
> > plot(test$sp~test$env, main = "S vs. temp", xlim=c(0,20), ylim=c(0,14), ylab="S",xlab="env")
> > linear<-lm(test$sp~test$env)
> > quadratic<-lm(test$sp~test$env+I(test$env^2))
> > #summary(quadratic)
> > cubic<-lm(test$sp~test$env+I(test$env^2)+I(test$env^3))
> > #summary(cubic)
> > #fitting curve
> > abline(linear)
> > 
> Here I did for linear, but now I don't how I can plot quadratic and cubic line in the figure with different colour. I further wanted to put r2 value on the top of the fitting line. 
> ON linear fitting curve, I can see the line originated from left to right (cover whole x axis). I could not plot the line only within the data set. 
> Would any one help me to figure it? I think it is not difficult but for me- it is really taking time. 
> 
> Thanks and waiting for your suggestions
> 
> sincerely, 
> Kristi Glover
> 
> 
> 
> 
> [[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list