在R中绘制二次函数

时间:2017-04-07 17:00:28

标签: r plot quadratic

这是我如何绘制二次曲线:

factor <- 1:7

data <- c(0.1375000,0.2500000,0.3416667,0.4583333,0.7250000,0.9166667,1.0000000)

plot(factor, fitted(lm(data~factor+I(factor^2))), type="l")

enter image description here

我尝试对我的另一个数据做同样的事情。

factor1<-c(2833,2500,2437,2124,1382,3736,2100,1844,2740,957,1614,1100,1550,3858,2430,2139,1812,1757,1847,945)

data1<-c(0.95,0.88,0.88,0.93,0.81,0.67,0.55,0.53,0.52,0.90,0.87,0.20,0.28,-0.16,0.23,0.11,0.26,0.08,0.73,0.76)

plot(factor1,fitted(lm(data1~factor1+I(factor1^2))), type="l")

enter image description here

我认为这是因为第二个数据集没有排序,但我认为R会在绘制它们之前自动对它们进行排序。

有人能告诉我如何在第二个图中绘制二次线。

1 个答案:

答案 0 :(得分:0)

您可以先订购

i<-order(factor1)
plot(factor1[i],fitted(lm(data1[i]~factor1[i]+I(factor1[i]^2))), type="l")