2

R 没有打印出漂亮的曲线,而是打印出密集的线段。不过,那个密集混乱的边缘看起来与我要打印的曲线一模一样。

错误图

我绘制了数据集 X2 和 Y2,并试图在现有图上打印二次曲线。这是我的代码:

X22 <- X2^2

model2s <- lm(Y2 ~ X2 + X22)

情节(X2,Y2)

线(X2,拟合(model2s))

X2:
[1] -2.69725933 -1.54282303 -1.91720835 -0.08528522 -2.57551112 -2.65955930  1.66190727  0.01135419
  [9] -1.67597429 -0.46931267  1.31551076  1.78942814 -0.54821881 -2.93750249 -0.63519111 -2.17234702
 [17]  2.26156660 -2.13808807 -0.74155513  2.65037057  2.44828088 -2.52896408 -2.02068505 -1.36222982
 [25]  1.97171562 -0.27897421 -2.12562749 -0.85870780  0.71198294  1.24482512  0.20295272 -1.58949497
 [33] -0.59396637  0.45486252  2.51659763  2.62181364  2.20407646  1.06466931 -1.43400604  0.01579675
 [41] -0.33513385 -0.05453015  1.96167436  1.28448541 -2.69429783 -1.08642394 -0.09400599  2.98775967
 [49]  2.05795131  1.58896527  0.67934349 -0.13352141 -0.52543898 -2.40677026 -0.13610972 -1.31887725
 [57] -1.56066767 -1.35457660  1.16511448 -2.55372404 -2.28185200 -0.19699659  1.84159785  1.24092476
 [65] -2.90374380  2.29220701  1.22968228  2.60137009  0.87307737  2.71556663  0.94467230  0.96922155
 [73]  1.89863312  1.64500729  1.37186380 -1.87455109  1.15276643  0.26130981 -1.84580809 -1.32085543
 [81] -2.41207641  0.19248616 -1.65741770  2.13950098 -1.69597327 -0.06976200  1.14711285  2.97132615
 [89]  0.71798324 -1.02838913  0.44070700  2.07600642 -0.21917452 -0.36556134  2.60091749 -1.41738042
 [97]  1.04864677  0.83080236  2.56432957 -0.72499588 -0.81415858 -2.49700816 -2.72860601  0.49777866

Y2:
  [1]  -9.00479135  -1.56827264  -3.85069478   3.80620694  -7.78195591  -6.21173824   4.24967581   3.39550072
  [9]  -2.51108153   1.71820705   5.44931613   4.97755290   2.66081793 -11.34941655   0.27113981  -5.27374362
 [17]   3.55191243  -3.64065638   1.27630806   4.20004221   5.53455823  -6.48854059  -4.17995733  -2.00651295
 [25]   3.72495467   0.68337096  -4.28579895  -1.37001146   4.87616860   6.06427661   1.70089898  -3.07543568
 [33]   2.90859968   4.12792739   2.76034855   3.87910950   4.14718875   4.73100437  -2.38820139   3.32093131
 [41]   1.86320165   3.27669364   2.46242358   4.92157619  -7.90548937  -0.75929903   2.94267998   1.74858185
 [49]   3.45587195   3.74016585   4.00274064   2.93845395   1.85504582  -4.30620277   3.40285048  -1.11881798
 [57]  -0.50718093  -0.43403754   2.54878083  -6.90253145  -5.37796863   3.25636120   3.41966211   3.40255742
 [65]  -8.59066220   1.82125444   3.20829746   2.46454987   4.09421369   1.79725157   5.61761174   4.55423983
 [73]   3.12240983   2.86139737   4.00807877  -4.19551852   4.63684416   4.82350596  -2.73656766  -1.69755051
 [81]  -4.16628941   2.60384722  -2.77361082   3.98215540  -2.73349536   1.61857480   4.05148933   3.57791895
 [89]   3.35775758   1.13832332   4.17317062   1.62551176   1.15076311   2.24591763   1.99284489  -2.35373088
 [97]   3.86807106   5.50186659   2.51879877   0.82435797   1.56822937  -9.69863069  -7.75684415   3.61224550
4

1 回答 1

4

lines将按照给定的顺序在点之间画线。就像是

plot(X2,Y2)
ox2 <- order(X2)
lines(X2[ox2],fitted(model2s)[ox2])

应该给你你正在寻找的东西。

于 2013-02-03T07:16:29.597 回答