R:二次曲线正在打印数十行 [英] R: Quadratic curve is printing as dozens of lines

查看:123
本文介绍了R:二次曲线正在打印数十行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

R而不是打印漂亮的曲线,而是打印密集的线段。但是,那密密麻麻的混乱边缘看起来确实像我要打印的曲线。



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


X22<-X2 ^ 2


model2s<-lm(Y2〜X2 + X22)


图(X2,Y2)


lines(X2,fitted(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 0.81 -2.1256870
[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.588965 0.52543898 -2.40677026 -0.13610972 -1.31887725
[57] -1.56066767 -1.35457660 1.1651 1448 -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.37186​​380 -1.87455109 1.15276643 0.26130981 -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.830803.62.55882685768

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.27372436 $ b [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.75929998 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.46454987174 ] 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.13176176769[97] 3.86807106 5.50186659 2.51879877 0.82435797 1.56822937 -9.69863069 -7.75684415 3.61224550


解决方案

线将按照给出的顺序在点之间绘制线。

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

应该为您提供所需的内容


Instead of printing a nice curve, R is printing a dense mess of line segments. The edge of that dense mess looks exactly like the curve I'm trying to print, though.

I have datasets X2 and Y2 plotted and am trying to print a quadratic curve on the existing plot. Here's my code:

X22 <- X2^2

model2s <- lm(Y2 ~ X2 + X22)

plot(X2,Y2)

lines(X2,fitted(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

解决方案

lines will draw lines between the points in the order they are given. Something like

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

should give you what you're looking for.

这篇关于R:二次曲线正在打印数十行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆