ggplot具有不同斜率和截距的直线

时间:2016-08-06 15:51:34

标签: r ggplot2

嗨我想用ggplot包含一系列带有截距和斜率的直线。这是我的df,这是我的第一个情节:

df

                         x           y
1                  5.767382e-04 -0.0179316822
2                  5.382526e-04 -0.0064160490
3                  3.283479e-04  0.0118829440
4                  5.515362e-04 -0.0082115912
5                  6.214837e-04 -0.0171607214
6                  9.149512e-04  0.0162441461
7                  1.096518e-03  0.0096310500
8                  1.231807e-03  0.0039493755
9                  1.291489e-03 -0.0373589167
10                 1.290377e-03 -0.0047063656
11                 1.346938e-03  0.0236590046
12                 1.561922e-03  0.0063475614
13                 1.421441e-03 -0.0070059435
14                 1.546511e-03 -0.0169201470
15                -1.674733e-04  0.0015561601
16                 1.954316e-03  0.0249988252
17                 1.450279e-03 -0.0229110197
18                 3.647360e-03  0.0196769910
19                -9.657013e-04 -0.0232707201
20                 9.267196e-03 -0.0147603533
21                -2.330904e-03  0.0146431308
22                 1.690023e-03  0.0144978266
23                -1.899995e-03 -0.0079710747
24                -3.216321e-03  0.0127135418
25                 7.389830e-04  0.0251444785
26                 5.605426e-04 -0.0076547396
27                 7.745684e-04  0.0026117673
28                -4.348211e-04  0.0042256186
29                 8.814315e-04  0.0169941536
30                -2.446243e-03 -0.0273569406
31                 0.000000e+00  0.0074404985
32                 1.252793e-02 -0.0239050021
33                 1.859272e-03  0.0036494591
34                -3.281863e-05  0.0298095265
35                -5.894445e-03 -0.0118560733
36                -1.450642e-04 -0.0056893640
37                 8.910996e-04  0.0263777220
38                -6.954649e-03 -0.0104581718
39                 5.741444e-04  0.0193070220
40                -1.564921e-04  0.0458305555
41                -2.394386e-03 -0.0127560808
42                -4.719316e-03  0.0107511726
43                -4.819214e-03  0.0289323344
44                -8.151265e-04 -0.0160308497
45                -5.028173e-04  0.0180222018
46                -1.118739e-03  0.0240456897
47                -4.347277e-03 -0.0523667661
48                -6.275963e-04  0.0221451602
49                 5.550606e-04  0.0129487351
50                 4.902597e-04  0.0143767869
51                 4.406320e-04 -0.0063753077
52                -3.324976e-03  0.0071569408
53                -7.674161e-04  0.0100497676
54                 1.096383e-03 -0.0132797308
55                 1.722997e-03 -0.0175982918
56                 1.796998e-03 -0.0131551067
57                 1.674058e-03  0.0105408021
58                 2.762772e-04  0.0195710544
59                 4.198189e-03 -0.0203275722
60                 6.759233e-03  0.0293425895
61                 1.623984e-03 -0.0007776215
62                 2.687013e-04 -0.0067553441
63                 1.764196e-03 -0.0231203356
64                 2.067229e-03 -0.0040448936
65                 7.313363e-04 -0.0409024742
66                 1.827486e-03 -0.0245661039
67                -4.117068e-03  0.0098401969
68                 1.761924e-03 -0.0164702633
69                 1.468973e-02  0.0051605124
70                 6.409529e-03  0.0054452684
71                 2.160516e-03 -0.0340426755
72                 5.874911e-03 -0.0207890110
73                -4.931250e-05  0.0426363179
74                -3.308259e-04 -0.0035186580
75                 1.001841e-02 -0.0110782883
76                 6.817267e-03 -0.0247737276
77                 1.898683e-03 -0.0071688431
78                 1.879390e-03  0.0262043639
79                -3.978326e-03  0.0161207659
80                 2.038382e-03 -0.0491302989
81                 2.952938e-03 -0.0211728961
82                -5.091002e-05 -0.0133934387
83                 2.233294e-03 -0.0400734285
84                 7.845835e-03 -0.0166611856
85                 2.290868e-03 -0.0193531306
86                 2.576246e-03  0.0097141933
87                 8.855930e-03  0.0357254985
88                 8.331340e-04 -0.0333207907
89                 2.746399e-03  0.0109406292
90                 5.894602e-03 -0.0027239561
91                 2.760225e-03  0.0107796305
92                 5.507580e-03 -0.0058818392
93                 7.966446e-03 -0.0124912857
94                 2.269484e-03 -0.0423110888
95                 1.147931e-02 -0.0043869382
96                 1.343029e-03  0.0177541412
97                 3.332866e-03 -0.0207729389
98                 7.214568e-03  0.0189228345
99                 5.126573e-03 -0.0106763932
100                1.545817e-03  0.0468064583
101                6.923315e-04  0.0166643412
102               -5.572046e-03  0.0179240781
103                8.799100e-04 -0.0213355187
104                8.703026e-03  0.0165175080
105                5.011657e-03 -0.0006547804
106               -3.404681e-03  0.0227698214
107                7.859670e-03  0.0079283421
108                1.201645e-04 -0.0346891934
109                5.052620e-03  0.0147842433
110                6.510928e-03  0.0095857959
111               -6.805175e-03 -0.0468008862
112               -4.363319e-04 -0.0290832355
113               -8.097971e-03  0.0116832017
114                1.943374e-02 -0.0094214974
115                1.306860e-04 -0.0186188622
116               -2.380523e-03  0.0181519870
117               -4.110306e-04 -0.0208185537
118                2.305092e-03 -0.0308729855
119                8.793202e-03  0.0050425386
120               -2.153084e-04 -0.0001370091
121               -2.057392e-03 -0.0011947316
122               -2.924294e-03 -0.0008234326
123                5.883910e-05 -0.0194284696
124               -9.804020e-04  0.0109390697
125                1.857671e-03 -0.0141840698
126                3.811320e-04 -0.0127408263
127                4.298727e-03  0.0074098408
128                1.451233e-03 -0.0100142738
129                6.517445e-04 -0.0194408567
130                5.964974e-03  0.0102715503
131               -2.243700e-03  0.0323884648
132               -2.588121e-04  0.0013129366
133                1.569282e-03 -0.0006164677
134                1.709266e-03  0.0008549983
135                1.370448e-03  0.0035117342
136                6.829333e-03  0.0021959986
137                2.250341e-03 -0.0070402575
138                1.590869e-03  0.0046659934
139                3.164434e-03  0.0825476969
140                2.186351e-03  0.0230247530
141               -1.470575e-02  0.0981854863
142               -4.647970e-03  0.0516244670
143               -1.381099e-04  0.0650898115
144               -2.663386e-03 -0.0027354677
145                3.891808e-04  0.0113962573
146                1.282737e-04 -0.0055887228
147                1.006854e-02 -0.0332557676
148                3.609098e-03 -0.0845451850
149                9.674767e-05 -0.0210540954
150               -4.803852e-03 -0.0066589253
151                5.373557e-03  0.0170621188
152               -7.173718e-04  0.0188572127
153                3.751451e-03 -0.0301665839
154                1.813012e-04  0.0044078294
155               -4.622137e-03  0.0161162531
156                8.778474e-03  0.0077374492
157                2.243122e-04  0.0512243884
158                2.435534e-04  0.0036167504
159                4.313842e-03 -0.0090206012
160                2.140608e-03  0.0520850888
161                4.855610e-03 -0.0292653473
162               -6.378769e-03 -0.0457181372
163                1.409388e-04  0.0188185729
164               -6.677023e-03 -0.0249923532
165               -2.350696e-03 -0.0144915120
166                2.238066e-04  0.0262718350
167                2.985634e-03 -0.0053246849
168                1.733721e-02 -0.0227874598
169                1.110229e-02 -0.0153335154
170                1.218916e-02  0.0145804128
171               -1.636347e-03 -0.0397472603
172                4.168994e-04  0.0132717818
173                4.443091e-04  0.0249194075
174                2.261120e-03 -0.0222072628
175                3.282455e-04  0.0081088595
176                7.487101e-04  0.0442237830
177               -6.156654e-03 -0.0355019963
178                1.291197e-04  0.0259225094
179                7.723982e-03  0.0119600885
180                2.952351e-04 -0.0152999892
181                2.366808e-04 -0.0122095048
182                2.076462e-04 -0.0001917117
183                1.917117e-04 -0.0162039464
184                2.110784e-04  0.0535713964
185                1.539030e-04 -0.0188620346
186                7.841969e-05  0.0010503132
187                3.133568e-05 -0.0136614578
188                1.270608e-04 -0.0165115654
189                9.688358e-05  0.0023226192
190                9.665883e-05  0.0052383062
191                3.205231e-05 -0.0141548721
192                8.127108e-05  0.0249463151
193                1.585314e-04  0.0434959064
194                1.821383e-04 -0.0003188219
195                1.518326e-04 -0.0246884369
196                2.023110e-04  0.0262360490
197                2.122305e-04 -0.0003942620
198                2.123142e-04  0.0004245834
199                2.273813e-04 -0.0201528956
200                2.320096e-04 -0.0458005930
201                2.590716e-04  0.0079359095
202                2.248996e-04 -0.0193856392
203                2.293015e-04 -0.0110522023
204                1.987314e-04  0.0077707135
205                1.971933e-04 -0.0195328110
206                2.345923e-04  0.0052150485
207                2.167046e-04 -0.0127680943
208                2.363707e-04  0.0066644474
209                2.180312e-04 -0.0191872308
210                2.393490e-04  0.0126747424
211                2.532137e-04  0.0164598299
212                2.324770e-04  0.0241359061
213                2.593487e-04  0.0132217626
214                2.399482e-04  0.0213523358
215                1.722505e-04  0.0043441641
216                2.494504e-04  0.0374752187
217                1.802127e-04 -0.0285494293
218                2.317837e-04  0.0402110643
219                2.078076e-04 -0.0346015566
220                2.151232e-04  0.0070594834
221                2.136100e-04 -0.0301762391
222                2.044298e-04  0.0077859198
223                2.184462e-04  0.0145314016
224                1.537870e-04  0.0152173697
225                1.514647e-04 -0.0193785840
226                1.698697e-04  0.0256451675
227               -1.806522e-04  0.0210763020
228               -8.843818e-05  0.0383149856
229               -2.837040e-05  0.0177152551
230               -1.254312e-04 -0.0308801034
231               -8.624159e-05 -0.0190227662
232               -8.789792e-05  0.0064394355

ggplot(df, aes(x=df[,1], y=df[,2]))+ geom_point(shape=1) +  xlim(-0.015,0.021) +  ylim(-0.1,0.1)

现在我想直截了当地截取下面的斜坡和斜坡。也就是说,有19个拦截和19个斜坡,那么我将有19条线:

intercept_slope

             intercept        slope
tau= 0.05 -0.0382845875  0.89386498
tau= 0.10 -0.0289286773  0.35422172
tau= 0.15 -0.0212578889 -0.08822473
tau= 0.20 -0.0193409306 -0.19879373
tau= 0.25 -0.0163342536 -0.37221704
tau= 0.30 -0.0130620457 -0.20459475
tau= 0.35 -0.0088296144 -0.45186425
tau= 0.40 -0.0058704748 -0.51982456
tau= 0.45 -0.0002945895 -0.46964248
tau= 0.50  0.0018460162 -0.57979144
tau= 0.55  0.0050322871 -0.74374709
tau= 0.60  0.0074404985 -0.70151564
tau= 0.65  0.0106591381 -0.63874335
tau= 0.70  0.0133004843 -0.63371325
tau= 0.75  0.0160149735 -0.89770765
tau= 0.80  0.0194564185 -0.97319036
tau= 0.85  0.0244394584 -1.30573010
tau= 0.90  0.0275024680 -1.26220009
tau= 0.95  0.0438769348 -2.40348955

我正在尝试,但错了:

ggplot(df, aes(x=df[,1], y=df[,2]))+ geom_point(shape=1) +  xlim(-0.015,0.021) +  ylim(-0.1,0.1)+  
    for (i in 1:19) {
    geom_abline(intercept = intercept_slope[i,1], slope = intercept_slope[i,2])   
}

出错了什么?有什么帮助吗?

0 个答案:

没有答案