Plotting more than one linear regression line in ggplot - r

I am trying to plot, using ggplot, a series of scatter plots with regression lines for several datasets. I started with the following dataset, "onepectCO2MEDIAN". The data for this dataset is as follows:
onepctCO2MEDIAN
x y
layer.1 0.000000000 0.0000000
layer.2 0.006794447 4.9002490
layer.3 0.014288058 0.1608000
layer.4 0.022087920 6.6349133
layer.5 0.030797357 -1.2429506
layer.6 0.038451072 1.5643374
layer.7 0.048087904 -2.2659035
layer.8 0.058677729 2.2070045
layer.9 0.069261406 -2.3677001
layer.10 0.080524530 -1.0913506
layer.11 0.092760246 0.4099940
layer.12 0.103789609 -0.1259727
layer.13 0.116953168 -2.4138253
layer.14 0.129253298 7.0890257
layer.15 0.141710050 -0.7593539
layer.16 0.156002052 0.0454416
layer.17 0.170648172 -1.5349683
layer.18 0.185318425 6.5524201
layer.19 0.199463055 -0.8312563
layer.20 0.213513337 -2.5099183
layer.21 0.228839271 0.1365968
layer.22 0.246981293 -1.3719845
layer.23 0.263012767 -0.8712988
layer.24 0.278505564 0.6632584
layer.25 0.293658361 0.7938036
layer.26 0.310747266 3.4880637
layer.27 0.325990349 -4.4612208
layer.28 0.342517540 0.0871734
layer.29 0.362751633 -1.4171578
layer.30 0.380199537 -0.9956508
layer.31 0.394992948 0.3215526
layer.32 0.414373398 3.1403866
layer.33 0.430690214 -0.7376099
layer.34 0.449738145 -2.4860541
layer.35 0.470167458 -3.4235858
layer.36 0.489019871 0.4824748
layer.37 0.507242471 -0.9785386
layer.38 0.524314284 8.5359684
layer.39 0.543750525 5.4844742
layer.40 0.564234197 3.2149367
layer.41 0.583679616 3.9168916
layer.42 0.601459444 4.4907020
layer.43 0.619924664 6.5410410
layer.44 0.639932007 4.8068650
layer.45 0.661347181 8.1510170
layer.46 0.684117317 0.2697413
layer.47 0.704829752 -0.1807501
layer.48 0.725045770 9.7181249
layer.49 0.745165825 1.5406466
layer.50 0.765016139 -1.6476041
layer.51 0.783461511 4.8024603
layer.52 0.806382924 4.0421516
layer.53 0.829241335 9.3756512
layer.54 0.849924415 5.3305050
layer.55 0.871352434 7.5445803
layer.56 0.893632233 6.4679547
layer.57 0.916052133 2.8096065
layer.58 0.938579470 5.3921661
layer.59 0.959907651 7.2043689
layer.60 0.981643587 3.3350806
layer.61 1.004116774 8.8690707
layer.62 1.028363466 1.7861299
layer.63 1.054009140 6.2555038
layer.64 1.072440803 7.6079236
layer.65 1.094457805 7.6871483
layer.66 1.123176277 4.7787764
layer.67 1.149430871 12.7110502
layer.68 1.170912921 -0.7156284
layer.69 1.196743071 1.6490899
layer.70 1.218625903 3.0363024
layer.71 1.241868377 4.2974769
layer.72 1.267941594 1.9543778
layer.73 1.290708780 3.9986964
layer.74 1.313222289 4.5179472
layer.75 1.339045882 0.9337905
layer.76 1.362803459 3.3050770
layer.77 1.384450197 3.5422970
layer.78 1.409720302 5.9973660
layer.79 1.435851157 0.5081869
layer.80 1.455592215 7.9661630
layer.81 1.479495347 9.9460496
layer.82 1.506051958 3.7908372
layer.83 1.525728464 2.5735847
layer.84 1.549362063 10.1404974
layer.85 1.573440671 13.7408304
layer.86 1.600278735 0.9335771
layer.87 1.623879492 9.7588742
layer.88 1.650029302 1.2769395
layer.89 1.672362328 13.4970906
layer.90 1.700221121 10.2087502
layer.91 1.724793375 1.6811275
layer.92 1.751070559 6.1178992
layer.93 1.778022110 -0.1567626
layer.94 1.803022087 3.8237479
layer.95 1.830668867 4.4331468
layer.96 1.855736911 5.9790707
layer.97 1.882615030 11.3104333
layer.98 1.909218490 8.2142607
layer.99 1.938130021 15.3209674
layer.100 1.963727593 5.8178217
layer.101 1.993271947 9.6004907
layer.102 2.022548139 3.4063646
layer.103 2.050679922 4.7375010
layer.104 2.078064442 3.0133019
layer.105 2.104113460 5.5659522
layer.106 2.133597612 12.0346333
layer.107 2.164026260 -0.4028320
layer.108 2.194852829 10.5996780
layer.109 2.224257946 5.4479584
layer.110 2.252194643 4.7052374
layer.111 2.277335048 14.0962019
layer.112 2.304058313 5.7149016
layer.113 2.330930233 3.7780072
layer.114 2.357022762 4.4120620
layer.115 2.386489272 4.1866085
layer.116 2.417503953 6.9078802
layer.117 2.448524356 2.7825739
layer.118 2.478698969 7.6171786
layer.119 2.510175705 10.2410603
layer.120 2.539697886 8.1820711
layer.121 2.567915559 4.8275494
layer.122 2.597463250 19.1624883
layer.123 2.627518773 16.0677109
layer.124 2.658759236 12.5897081
layer.125 2.692401528 9.2907988
layer.126 2.721903205 7.4262502
layer.127 2.753021359 9.3902518
layer.128 2.786313415 12.6193550
layer.129 2.819564104 11.1121040
layer.130 2.850823164 15.7907100
layer.131 2.880394101 10.7425287
layer.132 2.911391258 7.7971430
layer.133 2.942965150 8.8060858
layer.134 2.974468350 17.5606266
layer.135 3.008983612 17.3088605
layer.136 3.040015221 13.4500543
layer.137 3.072668672 14.6377884
layer.138 3.105982423 8.0798552
dput(onepctCO2MEDIAN)
dput(onepctCO2MEDIAN)
structure(list(x = c(0, 0.00679444684647024, 0.014288058038801,
0.0220879195258021, 0.0307973567396402,0.0384510718286037,0.0480879042297602,
0.0586777292191982, 0.0692614056169987, 0.080524530261755,0.0927602462470531,
0.103789608925581, 0.116953168064356, 0.129253298044205, 0.141710050404072,
0.156002052128315, 0.170648172497749, 0.185318425297737, 0.199463054537773,
0.21351333707571, 0.22883927077055, 0.246981292963028, 0.263012766838074,
0.278505563735962, 0.29365836083889, 0.310747265815735, 0.325990349054337,
0.342517539858818, 0.362751632928848, 0.380199536681175, 0.39499294757843,
0.414373397827148, 0.430690214037895, 0.449738144874573, 0.470167458057404,
0.489019870758057, 0.507242470979691, 0.524314284324646, 0.543750524520874,
0.56423419713974, 0.583679616451263, 0.601459443569183, 0.619924664497375,
0.639932006597519, 0.661347180604935, 0.684117317199707, 0.704829752445221,
0.725045770406723, 0.745165824890137, 0.765016138553619, 0.783461511135101,
0.806382924318314, 0.829241335391998, 0.84992441534996, 0.871352434158325,
0.893632233142853, 0.916052132844925, 0.938579469919205, 0.959907650947571,
0.981643587350845, 1.00411677360535, 1.02836346626282, 1.05400913953781,
1.07244080305099, 1.09445780515671, 1.12317627668381, 1.14943087100983,
1.17091292142868, 1.19674307107925, 1.21862590312958, 1.24186837673187,
1.26794159412384, 1.2907087802887, 1.31322228908539, 1.33904588222504,
1.36280345916748, 1.38445019721985, 1.40972030162811, 1.43585115671158,
1.45559221506119, 1.47949534654617, 1.50605195760727, 1.52572846412659,
1.5493620634079, 1.5734406709671, 1.60027873516083, 1.62387949228287,
1.65002930164337, 1.67236232757568, 1.70022112131119, 1.72479337453842,
1.75107055902481, 1.77802211046219, 1.80302208662033, 1.83066886663437,
1.85573691129684, 1.88261502981186, 1.90921849012375, 1.93813002109528,
1.96372759342194, 1.99327194690704, 2.02254813909531, 2.05067992210388,
2.07806444168091, 2.1041134595871, 2.13359761238098, 2.16402626037598,
2.19485282897949, 2.2242579460144, 2.25219464302063, 2.27733504772186,
2.30405831336975, 2.33093023300171, 2.35702276229858, 2.38648927211761,
2.41750395298004, 2.44852435588837, 2.47869896888733, 2.51017570495605,
2.53969788551331, 2.567915558815, 2.59746325016022, 2.62751877307892,
2.65875923633575, 2.69240152835846, 2.72190320491791, 2.75302135944366,
2.78631341457367, 2.8195641040802, 2.85082316398621, 2.88039410114288,
2.91139125823975, 2.94296514987946, 2.97446835041046, 3.00898361206055,
3.04001522064209, 3.07266867160797, 3.10598242282867), y = c(0,
4.90024901723162, 0.160799993152722, 6.63491326258641, -1.24295055804536,
1.56433744259162, -2.26590352245208, 2.20700446463354, -2.36770012911069,
-1.09135061899174, 0.409993989292701, -0.125972681525582, -2.41382533818026,
7.08902570153028, -0.759353880417294, 0.0454415959640926, -1.53496826259972,
6.55242014096194, -0.831256280861552, -2.50991825629084, 0.136596820654013,
-1.37198445498419, -0.871298832596736, 0.663258363762466, 0.793803634291308,
3.48806373666998, -4.46122081238949, 0.0871733966938564, -1.41715777257774,
-0.995650815648318, 0.32155262317503, 3.14038657369241, -0.737609879885404,
-2.48605406511292, -3.423585843908, 0.482474753780281, -0.978538630093809,
8.53596837794201, 5.48447420320695, 3.21493665820644, 3.91689160157513,
4.49070195980797, 6.54104103157039, 4.80686500146557, 8.15101701282067,
0.26974132191657, -0.180750068063062, 9.71812491230244, 1.54064657400204,
-1.64760408795688, 4.80246028991894, 4.04215159914344, 9.37565121768513,
5.33050496938428, 7.54458026088508, 6.46795470819342, 2.80960651433971,
5.39216613235986, 7.20436888038562, 3.3350806460997, 8.86907069895943,
1.78612988613659, 6.25550382050395, 7.60792364896564, 7.68714830528144,
4.77877638957615, 12.7110501777314, -0.715628443181046, 1.64908991824022,
3.03630240714679, 4.29747688442346, 1.95437780501881, 3.99869636910933,
4.51794724689848, 0.933790484492299, 3.30507700050003, 3.5422970157433,
5.99736597322524, 0.508186860060022, 7.96616300581067, 9.94604963036295,
3.79083717222623, 2.57358468532258, 10.1404974171776, 13.7408303595752,
0.933577123801399, 9.75887417074129, 1.27693947132921, 13.4970905965787,
10.2087501765735, 1.68112753028756, 6.1178991508927, -0.156762622680077,
3.82374791691426, 4.43314678736265, 5.97907067167507, 11.3104332518482,
8.21426074201525, 15.320967360602, 5.81782169471483, 9.6004907412354,
3.40636455909704, 4.73750103921864, 3.0133019468806, 5.56595224859066,
12.0346332527215, -0.40283199827104, 10.5996779538754, 5.44795836991128,
4.70523736412729, 14.096201892183, 5.71490161813391, 3.77800720810782,
4.41206200639436, 4.18660847858423, 6.90788020044911, 2.78257393345915,
7.61717857379431, 10.2410602647684, 8.18207106836167, 4.82754943871433,
19.1624882857155, 16.0677109398509, 12.589708067017, 9.29079879799404,
7.42625019725314, 9.39025179806185, 12.6193550331438, 11.1121039747257,
15.7907099734986, 10.7425286789233, 7.79714300307344, 8.80608578166101,
17.5606266346039, 17.3088604929222, 13.4500543478523, 14.6377884248645,
8.07985518296064)), class = "data.frame", row.names = c("layer.1",
"layer.2", "layer.3", "layer.4", "layer.5", "layer.6", "layer.7",
"layer.8", "layer.9", "layer.10", "layer.11", "layer.12", "layer.13",
"layer.14", "layer.15", "layer.16", "layer.17", "layer.18", "layer.19",
"layer.20", "layer.21", "layer.22", "layer.23", "layer.24", "layer.25",
"layer.26", "layer.27", "layer.28", "layer.29", "layer.30", "layer.31",
"layer.32", "layer.33", "layer.34", "layer.35", "layer.36", "layer.37",
"layer.38", "layer.39", "layer.40", "layer.41", "layer.42", "layer.43",
"layer.44", "layer.45", "layer.46", "layer.47", "layer.48", "layer.49",
"layer.50", "layer.51", "layer.52", "layer.53", "layer.54", "layer.55",
"layer.56", "layer.57", "layer.58", "layer.59", "layer.60", "layer.61",
"layer.62", "layer.63", "layer.64", "layer.65", "layer.66", "layer.67",
"layer.68", "layer.69", "layer.70", "layer.71", "layer.72", "layer.73",
"layer.74", "layer.75", "layer.76", "layer.77", "layer.78", "layer.79",
"layer.80", "layer.81", "layer.82", "layer.83", "layer.84", "layer.85",
"layer.86", "layer.87", "layer.88", "layer.89", "layer.90", "layer.91",
"layer.92", "layer.93", "layer.94", "layer.95", "layer.96", "layer.97",
"layer.98", "layer.99", "layer.100", "layer.101", "layer.102",
"layer.103", "layer.104", "layer.105", "layer.106", "layer.107",
"layer.108", "layer.109", "layer.110", "layer.111", "layer.112",
"layer.113", "layer.114", "layer.115", "layer.116", "layer.117",
"layer.118", "layer.119", "layer.120", "layer.121", "layer.122",
"layer.123", "layer.124", "layer.125", "layer.126", "layer.127",
"layer.128", "layer.129", "layer.130", "layer.131", "layer.132",
"layer.133", "layer.134", "layer.135", "layer.136", "layer.137",
"layer.138"))
I started with the following to generate the first regression line and scatter plot:
lm<-ggplot(onepctCO2MEDIAN) +
geom_jitter(aes(RCP1pctCO2cumulativeMedian[1:138], departurea),
colour="blue") + geom_smooth(aes(RCP1pctCO2cumulativeMedian[1:138],
departurea), method=lm)
But I receive this error:
Warning message:
Computation failed in `stat_smooth()`:
'what' must be a function or character string
A blue scatter plot is successfully generated, but the problem is that the regression line does not appear, presumably related to the above warning.
Is there a reason for this? I would appreciate any assistance!

You should be able to get a scatter plot with a regression line by doing the following:
library(tidyverse)
#> Registered S3 methods overwritten by 'ggplot2':
#> method from
#> [.quosures rlang
#> c.quosures rlang
#> print.quosures rlang
onepctCO2MEDIAN <- structure(list(x = c(0, 0.00679444684647024, 0.014288058038801,
0.0220879195258021, 0.0307973567396402,0.0384510718286037,0.0480879042297602,
0.0586777292191982, 0.0692614056169987, 0.080524530261755,0.0927602462470531,
0.103789608925581, 0.116953168064356, 0.129253298044205, 0.141710050404072,
0.156002052128315, 0.170648172497749, 0.185318425297737, 0.199463054537773,
0.21351333707571, 0.22883927077055, 0.246981292963028, 0.263012766838074,
0.278505563735962, 0.29365836083889, 0.310747265815735, 0.325990349054337,
0.342517539858818, 0.362751632928848, 0.380199536681175, 0.39499294757843,
0.414373397827148, 0.430690214037895, 0.449738144874573, 0.470167458057404,
0.489019870758057, 0.507242470979691, 0.524314284324646, 0.543750524520874,
0.56423419713974, 0.583679616451263, 0.601459443569183, 0.619924664497375,
0.639932006597519, 0.661347180604935, 0.684117317199707, 0.704829752445221,
0.725045770406723, 0.745165824890137, 0.765016138553619, 0.783461511135101,
0.806382924318314, 0.829241335391998, 0.84992441534996, 0.871352434158325,
0.893632233142853, 0.916052132844925, 0.938579469919205, 0.959907650947571,
0.981643587350845, 1.00411677360535, 1.02836346626282, 1.05400913953781,
1.07244080305099, 1.09445780515671, 1.12317627668381, 1.14943087100983,
1.17091292142868, 1.19674307107925, 1.21862590312958, 1.24186837673187,
1.26794159412384, 1.2907087802887, 1.31322228908539, 1.33904588222504,
1.36280345916748, 1.38445019721985, 1.40972030162811, 1.43585115671158,
1.45559221506119, 1.47949534654617, 1.50605195760727, 1.52572846412659,
1.5493620634079, 1.5734406709671, 1.60027873516083, 1.62387949228287,
1.65002930164337, 1.67236232757568, 1.70022112131119, 1.72479337453842,
1.75107055902481, 1.77802211046219, 1.80302208662033, 1.83066886663437,
1.85573691129684, 1.88261502981186, 1.90921849012375, 1.93813002109528,
1.96372759342194, 1.99327194690704, 2.02254813909531, 2.05067992210388,
2.07806444168091, 2.1041134595871, 2.13359761238098, 2.16402626037598,
2.19485282897949, 2.2242579460144, 2.25219464302063, 2.27733504772186,
2.30405831336975, 2.33093023300171, 2.35702276229858, 2.38648927211761,
2.41750395298004, 2.44852435588837, 2.47869896888733, 2.51017570495605,
2.53969788551331, 2.567915558815, 2.59746325016022, 2.62751877307892,
2.65875923633575, 2.69240152835846, 2.72190320491791, 2.75302135944366,
2.78631341457367, 2.8195641040802, 2.85082316398621, 2.88039410114288,
2.91139125823975, 2.94296514987946, 2.97446835041046, 3.00898361206055,
3.04001522064209, 3.07266867160797, 3.10598242282867),
y = c(0,
4.90024901723162, 0.160799993152722, 6.63491326258641, -1.24295055804536,
1.56433744259162, -2.26590352245208, 2.20700446463354, -2.36770012911069,
-1.09135061899174, 0.409993989292701, -0.125972681525582, -2.41382533818026,
7.08902570153028, -0.759353880417294, 0.0454415959640926, -1.53496826259972,
6.55242014096194, -0.831256280861552, -2.50991825629084, 0.136596820654013,
-1.37198445498419, -0.871298832596736, 0.663258363762466, 0.793803634291308,
3.48806373666998, -4.46122081238949, 0.0871733966938564, -1.41715777257774,
-0.995650815648318, 0.32155262317503, 3.14038657369241, -0.737609879885404,
-2.48605406511292, -3.423585843908, 0.482474753780281, -0.978538630093809,
8.53596837794201, 5.48447420320695, 3.21493665820644, 3.91689160157513,
4.49070195980797, 6.54104103157039, 4.80686500146557, 8.15101701282067,
0.26974132191657, -0.180750068063062, 9.71812491230244, 1.54064657400204,
-1.64760408795688, 4.80246028991894, 4.04215159914344, 9.37565121768513,
5.33050496938428, 7.54458026088508, 6.46795470819342, 2.80960651433971,
5.39216613235986, 7.20436888038562, 3.3350806460997, 8.86907069895943,
1.78612988613659, 6.25550382050395, 7.60792364896564, 7.68714830528144,
4.77877638957615, 12.7110501777314, -0.715628443181046, 1.64908991824022,
3.03630240714679, 4.29747688442346, 1.95437780501881, 3.99869636910933,
4.51794724689848, 0.933790484492299, 3.30507700050003, 3.5422970157433,
5.99736597322524, 0.508186860060022, 7.96616300581067, 9.94604963036295,
3.79083717222623, 2.57358468532258, 10.1404974171776, 13.7408303595752,
0.933577123801399, 9.75887417074129, 1.27693947132921, 13.4970905965787,
10.2087501765735, 1.68112753028756, 6.1178991508927, -0.156762622680077,
3.82374791691426, 4.43314678736265, 5.97907067167507, 11.3104332518482,
8.21426074201525, 15.320967360602, 5.81782169471483, 9.6004907412354,
3.40636455909704, 4.73750103921864, 3.0133019468806, 5.56595224859066,
12.0346332527215, -0.40283199827104, 10.5996779538754, 5.44795836991128,
4.70523736412729, 14.096201892183, 5.71490161813391, 3.77800720810782,
4.41206200639436, 4.18660847858423, 6.90788020044911, 2.78257393345915,
7.61717857379431, 10.2410602647684, 8.18207106836167, 4.82754943871433,
19.1624882857155, 16.0677109398509, 12.589708067017, 9.29079879799404,
7.42625019725314, 9.39025179806185, 12.6193550331438, 11.1121039747257,
15.7907099734986, 10.7425286789233, 7.79714300307344, 8.80608578166101,
17.5606266346039, 17.3088604929222, 13.4500543478523, 14.6377884248645,
8.07985518296064)),
class = "data.frame", row.names = c("layer.1",
"layer.2", "layer.3", "layer.4", "layer.5", "layer.6", "layer.7",
"layer.8", "layer.9", "layer.10", "layer.11", "layer.12", "layer.13",
"layer.14", "layer.15", "layer.16", "layer.17", "layer.18", "layer.19",
"layer.20", "layer.21", "layer.22", "layer.23", "layer.24", "layer.25",
"layer.26", "layer.27", "layer.28", "layer.29", "layer.30", "layer.31",
"layer.32", "layer.33", "layer.34", "layer.35", "layer.36", "layer.37",
"layer.38", "layer.39", "layer.40", "layer.41", "layer.42", "layer.43",
"layer.44", "layer.45", "layer.46", "layer.47", "layer.48", "layer.49",
"layer.50", "layer.51", "layer.52", "layer.53", "layer.54", "layer.55",
"layer.56", "layer.57", "layer.58", "layer.59", "layer.60", "layer.61",
"layer.62", "layer.63", "layer.64", "layer.65", "layer.66", "layer.67",
"layer.68", "layer.69", "layer.70", "layer.71", "layer.72", "layer.73",
"layer.74", "layer.75", "layer.76", "layer.77", "layer.78", "layer.79",
"layer.80", "layer.81", "layer.82", "layer.83", "layer.84", "layer.85",
"layer.86", "layer.87", "layer.88", "layer.89", "layer.90", "layer.91",
"layer.92", "layer.93", "layer.94", "layer.95", "layer.96", "layer.97",
"layer.98", "layer.99", "layer.100", "layer.101", "layer.102",
"layer.103", "layer.104", "layer.105", "layer.106", "layer.107",
"layer.108", "layer.109", "layer.110", "layer.111", "layer.112",
"layer.113", "layer.114", "layer.115", "layer.116", "layer.117",
"layer.118", "layer.119", "layer.120", "layer.121", "layer.122",
"layer.123", "layer.124", "layer.125", "layer.126", "layer.127",
"layer.128", "layer.129", "layer.130", "layer.131", "layer.132",
"layer.133", "layer.134", "layer.135", "layer.136", "layer.137",
"layer.138"))
# Create scatterplot from dataframe "onepctCO2MEDIAN" with "x" and "y" variables and add "lm"
onepctCO2MEDIAN %>%
ggplot(aes(x = x, y = y)) +
geom_point() +
xlab("x") +
ylab("y") +
geom_smooth(method = "lm")
Created on 2019-06-07 by the reprex package (v0.3.0)

Related

Calculating polygon intersecting area with gArea and gIntersection error in R

I have two polygons
coords1[,1]
-15.99719 -16.01133 -16.04000 -16.08308 -16.14041 -16.21175 -16.29681 -16.39525 -16.50668 -16.63064 -16.76664 -16.91413 -17.07252 -17.24116 -17.41939 -17.60647 -17.80166 -18.00417 -18.21319 -18.42788 -18.64736 -18.87076 -19.09718 -19.32570 -19.55541 -19.78538 -20.01468 -20.24240 -20.46760 -20.68940 -20.90688 -21.11919 -21.32546 -21.52486 -21.71659 -21.89988 -22.07399 -22.23822 -22.39191 -22.53443 -22.66522 -22.78374 -22.88953 -22.98215 -23.06124 -23.12646 -23.17757 -23.21435 -23.23666 -23.24441 -23.23756 -23.21615 -23.18025 -23.13002 -23.06566 -22.98742 -22.89562 -22.79063 -22.67288 -22.54283 -22.40101 -22.24800 -22.08440 -21.91088 -21.72813 -21.53690 -21.33795 -21.13208-20.92012 -20.70293 -20.48137 -20.25635 -20.02876 -19.79953 -19.56958 -19.33983 -19.11120 -18.88463 -18.66102 -18.44127 -18.22626 -18.01687 -17.81393 -17.61826 -17.43066 -17.25187 -17.08262 -16.92358 -16.77540 -16.63868 -16.51396 -16.40174 -16.30249 -16.21659 -16.14440 -16.08620 -16.04224 -16.01268 -15.99764 -15.99719
coords1[,2]
6.225418 6.262789 6.300443 6.338229 6.375996 6.413591 6.450863 6.487662 6.523839 6.559250 6.593750 6.627202 6.659471 6.690426 6.719943 6.747904 6.774196 6.798712 6.821354 6.842031 6.860660 6.877166 6.891481 6.903550 6.913322 6.920758 6.925830 6.928515 6.928804 6.926696 6.922198 6.915329 6.906116 6.894597 6.880818 6.864834 6.846711 6.826520 6.804343 6.780269 6.754396 6.726828 6.697675 6.667055 6.635091 6.601912 6.567652 6.532448 6.496442 6.459780 6.422608 6.385077 6.347338 6.309542 6.271842 6.234389 6.197335 6.160829 6.125017 6.090044 6.056051 6.023174 5.991546 5.961295 5.932541 5.905401 5.879985 5.856394 5.834723 5.815060 5.797484 5.782066 5.768867 5.757941 5.749333 5.743075 5.739195 5.737707 5.738617 5.741922 5.747609 5.755654 5.766025 5.778680 5.793569 5.810631 5.829798 5.850993 5.874129 5.899115 5.925849 5.954224 5.984126 6.015434 6.048021 6.081758 6.116508 6.152130 6.188483 6.225418
coords1.sys[,1]
-17.27076 -17.28943 -17.32370 -17.37346 -17.43849 -17.51853 -17.61326 -17.72229 -17.84520 -17.98148 -18.13059 -18.29192 -18.46483 -18.64862 -18.84254 -19.04583 -19.25765 -19.47717 -19.70348 -19.93569 -20.17285 -20.41402 -20.65821 -20.90445 -21.15175 -21.39911 -21.64554 -21.89003 -22.13161 -22.36931 -22.60216 -22.82924 -23.04962 -23.26241 -23.46677 -23.66187 -23.84692 -24.02117 -24.18394 -24.33455 -24.47241 -24.59695 -24.70769 -24.80416 -24.88599 -24.95284 -25.00445 -25.04060 -25.06116 -25.06603 -25.05520 -25.02871 -24.98667 -24.92926 -24.85668 -24.76926 -24.66732 -24.55129 -24.42163 -24.27886 -24.12357 -23.95636 -23.77792 -23.58897 -23.39027 -23.18261 -22.96683 -22.74380 -22.51443 -22.27962 -22.04034 -21.79754 -21.55219 -21.30530 -21.05785 -20.81083 -20.56525 -20.32209 -20.08233 -19.84693 -19.61685 -19.39301 -19.17632 -18.96763 -18.76781 -18.57764 -18.39789 -18.22930 -18.07253 -17.92822
-17.79696 -17.67926 -17.57560 -17.48640 -17.41203 -17.35277 -17.30887 -17.28050 -17.26778 -17.27076
coords1.sys[,2]
6.465179 6.538004 6.611223 6.684541 6.757663 6.830295 6.902143 6.972919 7.042338 7.110120 7.175992 7.239689 7.300955 7.359542 7.415215 7.467750 7.516935 7.562573 7.604478 7.642483 7.676435 7.706197 7.731648 7.752688 7.769230 7.781208 7.788574 7.791299 7.789371 7.782798 7.771606 7.755841 7.735567 7.710864 7.681832 7.648589 7.611267 7.570018 7.525007 7.476416 7.424440 7.369288 7.311183 7.250359 7.187060 7.121542 7.054068 6.984910 6.914346 6.842661 6.770143 6.697084 6.623779 6.550522 6.477608 6.405332 6.333984 6.263851 6.195216 6.128355 6.063538 6.001025 5.941069 5.883910 5.829778 5.778892 5.731457 5.687664 5.647688 5.611692 5.579819 5.552199 5.528941 5.510141 5.495874 5.486197 5.481149 5.480751 5.485003 5.493890 5.507375 5.525404 5.547904 5.574784 5.605938 5.641238 5.680543 5.723695 5.770520 5.820829 5.874419 5.931076 5.990571 6.052663 6.117104 6.183634 6.251984 6.321880 6.393041 6.465179
plot(1,1,ylim=c(0,15),xlim=c(-30,-10), t="n", xlab="", ylab="")
polygon(coords1[,1], coords1[,2], border=2,col=alpha("red",0.5))
polygon(coords1.sys[,1], coords1.sys[,2], border=2,col=alpha("black",0.5))
I am trying to calculate the percent overlap, as shown using gArea and gIntersection here.
kt.small.g=data.frame(coords1[,1], coords1[,2])
names(kt.small.g)=c("x","y")
summary(kt.small.g)
kt.small.s=data.frame(coords1.sys[,1], coords1.sys[,2])
names(kt.small.s)=c("x","y")
kt.small.g_pol <- Polygons(list(Polygon(kt.small.g)), "small.g")
kt.small.s_pol <- Polygons(list(Polygon(kt.small.s)), "small.s")
kt.small.g_area <- gArea(shape["small.g"])
kt.small.intersections <- gIntersection(shape["small.g"], shape["small.s"])
but after running kt.small.g_area <- gArea(shape["small.g"]) or kt.small.intersections <- gIntersection(shape["small.g"], shape["small.s"]) I get the error:
Error in h(simpleError(msg, call)) :
error in evaluating the argument 'obj' in selecting a method for function 'is.projected': NAs not permitted in row index
I have run through the code in the linked example without error. I have checked my data for NAs, parallel structure to the example, and tried to change the names in shape[] to smaller strings or strings without "." and I still get the same error.
Thank you for your help.
Not sure what percentage you're after, but all of the areas are listed below for you to do the math on.
The following solution uses the sf library to work with the spatial objects.
If you're working with sp objects (SpatialPolygonsDataframes, etc), they can usually be changed to sf using as(x, 'sf').
library(dplyr)
library(ggplot2)
library(sf)
# Cleaning your data pasted from SO
poly1_x <- as.numeric(c("-15.99719", "-16.01133", "-16.04000", "-16.08308", "-16.14041", "-16.21175", "-16.29681", "-16.39525", "-16.50668", "-16.63064", "-16.76664", "-16.91413", "-17.07252", "-17.24116", "-17.41939", "-17.60647", "-17.80166", "-18.00417", "-18.21319", "-18.42788", "-18.64736", "-18.87076", "-19.09718", "-19.32570", "-19.55541", "-19.78538", "-20.01468", "-20.24240", "-20.46760", "-20.68940", "-20.90688", "-21.11919", "-21.32546", "-21.52486", "-21.71659", "-21.89988", "-22.07399", "-22.23822", "-22.39191", "-22.53443", "-22.66522", "-22.78374", "-22.88953", "-22.98215", "-23.06124", "-23.12646", "-23.17757", "-23.21435", "-23.23666", "-23.24441", "-23.23756", "-23.21615", "-23.18025", "-23.13002", "-23.06566", "-22.98742", "-22.89562", "-22.79063", "-22.67288", "-22.54283", "-22.40101", "-22.24800", "-22.08440", "-21.91088", "-21.72813", "-21.53690", "-21.33795", "-21.13208","-20.92012", "-20.70293", "-20.48137", "-20.25635", "-20.02876", "-19.79953", "-19.56958", "-19.33983", "-19.11120", "-18.88463", "-18.66102", "-18.44127", "-18.22626", "-18.01687", "-17.81393", "-17.61826", "-17.43066", "-17.25187", "-17.08262", "-16.92358", "-16.77540", "-16.63868", "-16.51396", "-16.40174", "-16.30249", "-16.21659", "-16.14440", "-16.08620", "-16.04224", "-16.01268", "-15.99764", "-15.99719"))
poly1_y <- c(6.225418, 6.262789, 6.300443, 6.338229, 6.375996, 6.413591, 6.450863, 6.487662, 6.523839, 6.55925, 6.59375, 6.627202, 6.659471, 6.690426, 6.719943, 6.747904, 6.774196, 6.798712, 6.821354, 6.842031, 6.86066, 6.877166, 6.891481, 6.90355, 6.913322, 6.920758, 6.92583, 6.928515, 6.928804, 6.926696, 6.922198, 6.915329, 6.906116, 6.894597, 6.880818, 6.864834, 6.846711, 6.82652, 6.804343, 6.780269, 6.754396, 6.726828, 6.697675, 6.667055, 6.635091, 6.601912, 6.567652, 6.532448, 6.496442, 6.45978, 6.422608, 6.385077, 6.347338, 6.309542, 6.271842, 6.234389, 6.197335, 6.160829, 6.125017, 6.090044, 6.056051, 6.023174, 5.991546, 5.961295, 5.932541, 5.905401, 5.879985, 5.856394, 5.834723, 5.81506, 5.797484, 5.782066, 5.768867, 5.757941, 5.749333, 5.743075, 5.739195, 5.737707, 5.738617, 5.741922, 5.747609, 5.755654, 5.766025, 5.77868, 5.793569, 5.810631, 5.829798, 5.850993, 5.874129, 5.899115, 5.925849, 5.954224, 5.984126, 6.015434, 6.048021, 6.081758, 6.116508, 6.15213, 6.188483, 6.225418)
poly2_x <- c("-17.27076 -17.28943 -17.32370 -17.37346 -17.43849 -17.51853 -17.61326 -17.72229 -17.84520 -17.98148 -18.13059 -18.29192 -18.46483 -18.64862 -18.84254 -19.04583 -19.25765 -19.47717 -19.70348 -19.93569 -20.17285 -20.41402 -20.65821 -20.90445 -21.15175 -21.39911 -21.64554 -21.89003 -22.13161 -22.36931 -22.60216 -22.82924 -23.04962 -23.26241 -23.46677 -23.66187 -23.84692 -24.02117 -24.18394 -24.33455 -24.47241 -24.59695 -24.70769 -24.80416 -24.88599 -24.95284 -25.00445 -25.04060 -25.06116 -25.06603 -25.05520 -25.02871 -24.98667 -24.92926 -24.85668 -24.76926 -24.66732 -24.55129 -24.42163 -24.27886 -24.12357 -23.95636 -23.77792 -23.58897 -23.39027 -23.18261 -22.96683 -22.74380 -22.51443 -22.27962 -22.04034 -21.79754 -21.55219 -21.30530 -21.05785 -20.81083 -20.56525 -20.32209 -20.08233 -19.84693 -19.61685 -19.39301 -19.17632 -18.96763 -18.76781 -18.57764 -18.39789 -18.22930 -18.07253 -17.92822 -17.79696 -17.67926 -17.57560 -17.48640 -17.41203 -17.35277 -17.30887 -17.28050 -17.26778 -17.27076")
poly2_x <- stringr::str_split(poly2_x, pattern = ' ') %>% unlist()
poly2_y <- c(6.465179, 6.538004, 6.611223, 6.684541, 6.757663, 6.830295, 6.902143, 6.972919, 7.042338, 7.11012, 7.175992, 7.239689, 7.300955, 7.359542, 7.415215, 7.46775, 7.516935, 7.562573, 7.604478, 7.642483, 7.676435, 7.706197, 7.731648, 7.752688, 7.76923, 7.781208, 7.788574, 7.791299, 7.789371, 7.782798, 7.771606, 7.755841, 7.735567, 7.710864, 7.681832, 7.648589, 7.611267, 7.570018, 7.525007, 7.476416, 7.42444, 7.369288, 7.311183, 7.250359, 7.18706, 7.121542, 7.054068, 6.98491, 6.914346, 6.842661, 6.770143, 6.697084, 6.623779, 6.550522, 6.477608, 6.405332, 6.333984, 6.263851, 6.195216, 6.128355, 6.063538, 6.001025, 5.941069, 5.88391, 5.829778, 5.778892, 5.731457, 5.687664, 5.647688, 5.611692, 5.579819, 5.552199, 5.528941, 5.510141, 5.495874, 5.486197, 5.481149, 5.480751, 5.485003, 5.49389, 5.507375, 5.525404, 5.547904, 5.574784, 5.605938, 5.641238, 5.680543, 5.723695, 5.77052, 5.820829, 5.874419, 5.931076, 5.990571, 6.052663, 6.117104, 6.183634, 6.251984, 6.32188, 6.393041, 6.465179)
# Make polygons as sf objects
poly1 <- data.frame(x = poly1_x, y = poly1_y) %>%
st_as_sf(coords = c('x', 'y')) %>%
st_combine() %>%
st_cast('POLYGON')
poly2 <- data.frame(x = poly2_x, y = poly2_y) %>%
st_as_sf(coords = c('x', 'y')) %>%
st_combine() %>%
st_cast('POLYGON')
# Plot
ggplot() +
geom_sf(data = poly1, fill = 'red', alpha = .5) +
geom_sf(data = poly2, fill = 'blue', alpha = .5)
# Area of polygon1: (red)
st_area(poly1)
#> [1] 6.62603
# Area of polygon2: (blue)
st_area(poly2)
#> [1] 13.88645
# Area covered only by BOTH polygons (purple)
st_intersection(poly1, poly2) %>%
st_area
#> [1] 5.678321
Created on 2020-12-30 by the reprex package (v0.3.0)

Stratigraphic plot of geochemical data using ggplot2 and tidypaleo

I am trying to create a stratigraphic plot of geochemical element data which should be possible using package tidypaleo.
I want multiple plots of the different element data with Depth (cm) downcore set as the y axis. The data look as follows.
Image of data
I am using this code:
ggplot(wapITRAX, aes(x =BrTi , y = wapITRAX$Depth))+
labs(y = "Depth (cm)")+
geom_lineh()+
theme_classic()+
scale_y_reverse()
However, this only plots one element and I am trying to achieve a plot like this Image of plot
> dput(head(wapITRAX))
structure(list(Depth = 0:5, IncCoh = c(6.049230907, 5.975282432,
5.736199822, 5.658584418, 5.659008377, 5.597103404), BrTi =
c(50.50197628,
22.09236453, 23.48370927, 18.62638581, 14.36924414, 17.48777896
), AlIncCOh = c(16.69633736, 8.200449193, 23.70907643, 20.32310407,
28.62692352, 26.44224866), BrCl = c(8.04090623, 4.306048968,
3.417836951, 3.156895904, 2.787628518, 2.059316731), FeTi =
c(332.715415,
235.9371921, 372.726817, 390.7871397, 396.986099, 495.2624867
), CaTi = c(4.071146245, 3.27955665, 4.395989975, 3.677383592,
3.028670721, 4.523910733), ZrRb = structure(c(363L, 447L, 407L,
395L, 450L, 410L), .Label = c("#DIV/0!", "0.447638604",
"0.478169284",
"0.54554134", "0.548501778", "0.561420163", "0.579454254",
"0.579498861",
"0.580801291", "0.589758019", "0.590194076", "0.590277778",
"0.591357754",
"0.592870544", "0.593851133", "0.598519653", "0.599931082",
"0.600979737",
"0.601426307", "0.611710677", "0.617065868", "0.618499405",
"0.621310093",
"0.627720871", "0.63775246", "0.64005168", "0.643958869",
"0.644371941",
"0.645605974", "0.645661658", "0.646672915", "0.647348952",
"0.651578947",
"0.652401176", "0.656186383", "0.657906264", "0.658835905",
"0.662074554",
"0.662361624", "0.669589393", "0.67103429", "0.671371769",
"0.674335863",
"0.674781688", "0.676097561", "0.676639083", "0.677849462",
"0.680497925",
"0.680610514", "0.680725971", "0.683906537", "0.68855859",
"0.689067202",
"0.692353115", "0.692732291", "0.695411392", "0.696067091",
"0.696794872",
"0.699376436", "0.701762744", "0.702015197", "0.702432938",
"0.70361991",
"0.705235754", "0.705426357", "0.708084164", "0.708258528",
"0.708925221",
"0.715226656", "0.715263314", "0.717828827", "0.718975706",
"0.719799305",
"0.720363636", "0.72476489", "0.725426857", "0.725461098",
"0.726030739",
"0.7267645", "0.726998188", "0.727170554", "0.727533265",
"0.730362368",
"0.731182796", "0.735042735", "0.735849057", "0.736184046",
"0.737214792",
"0.738692342", "0.742223591", "0.742639327", "0.742714724",
"0.744126167",
"0.745790081", "0.746207701", "0.746606335", "0.747041077",
"0.749019608",
"0.751204307", "0.751326495", "0.752443737", "0.752994012",
"0.753398969",
"0.754587869", "0.755151515", "0.755466053", "0.756316411",
"0.757107679",
"0.759175941", "0.76070965", "0.761635833", "0.763760166",
"0.763861189",
"0.764076577", "0.765001936", "0.765509391", "0.765795207",
"0.76598579",
"0.767080745", "0.767225748", "0.767262192", "0.770291777",
"0.771194699",
"0.772540984", "0.774533358", "0.77486376", "0.777252364",
"0.778319123",
"0.778445883", "0.779076739", "0.779310345", "0.782186577",
"0.78238342",
"0.782541093", "0.785497178", "0.787594824", "0.788230584",
"0.788916736",
"0.789033068", "0.789052737", "0.789177605", "0.789596048",
"0.790439133",
"0.791078234", "0.792993631", "0.794899978", "0.795265235",
"0.795432921",
"0.798130469", "0.79861957", "0.798894446", "0.799410029",
"0.799679487",
"0.800096223", "0.800119868", "0.800280899", "0.800947004",
"0.80293448",
"0.804118993", "0.804120112", "0.805092452", "0.806161301",
"0.81631016",
"0.816337149", "0.81638756", "0.81827622", "0.818899466",
"0.81993865",
"0.821925431", "0.825329202", "0.825608077", "0.826135414",
"0.826325411",
"0.827108292", "0.829798658", "0.832460733", "0.832814584",
"0.833845295",
"0.833908046", "0.833980583", "0.83741705", "0.837604457",
"0.838292367",
"0.838860231", "0.839741935", "0.846485664", "0.846740374",
"0.84679304
1",
"0.84734599", "0.850027518", "0.850077882", "0.854863613",
"0.85565883",
"0.85591192", "0.857095047", "0.858967536", "0.85897779",
"0.859192457",
"0.859239235", "0.860408412", "0.860505166", "0.8609918",
"0.862821134",
"0.867779204", "0.872207328", "0.872918493", "0.873888706",
"0.874140666",
"0.877452229", "0.879856851", "0.88003663", "0.880155093",
"0.881013197",
"0.882190156", "0.882319978", "0.883029342", "0.883316008",
"0.884908053",
"0.885431692", "0.885849846", "0.889082969", "0.89763062",
"0.898384089",
"0.898527865", "0.9", "0.900719424", "0.904636261", "0.904913848",
"0.905223479", "0.908485273", "0.911212059", "0.916470588",
"0.917162698",
"0.917470525", "0.917952884", "0.918054256", "0.918781726",
"0.922268448",
"0.925987182", "0.926287744", "0.933958724", "0.939701616",
"0.940987438",
"0.941196465", "0.943041526", "0.946832078", "0.947419907",
"0.948080043",
"0.949085366", "0.950099404", "0.950502912", "0.950704225",
"0.953596288",
"0.954868709", "0.95505992", "0.955361596", "0.956422018",
"0.95671509",
"0.959064327", "0.96165309", "0.964232489", "0.965528048",
"0.965874467",
"0.968717195", "0.970270821", "0.971573209", "0.973415133",
"0.974608081",
"0.974986972", "0.978223496", "0.97935022", "0.980142566",
"0.981730052",
"0.982819606", "0.987483236", "0.989587207", "0.991002571",
"0.991932655",
"0.995306859", "0.997555012", "1.001706485", "1.002357873",
"1.005146199",
"1.010493827", "1.011544012", "1.013038906", "1.013469577",
"1.015357613",
"1.01541976", "1.015544041", "1.015937059", "1.016162826",
"1.019393939",
"1.019529957", "1.019994873", "1.028932491", "1.029518072",
"1.030101225",
"1.032310705", "1.03344968", "1.036769138", "1.037322515",
"1.041150092",
"1.041459782", "1.041961577", "1.042347697", "1.047411444",
"1.048390581",
"1.057811121", "1.060218978", "1.065395654", "1.06921167",
"1.071509648",
"1.073730469", "1.076014602", "1.077266637", "1.079983072",
"1.083003953",
"1.086196504", "1.086445657", "1.0867266", "1.0905", "1.092152628",
"1.094730942", "1.095692666", "1.1074142", "1.109534807",
"1.111154446",
"1.111188325", "1.113985094", "1.114045618", "1.128810226",
"1.129375525",
"1.132705479", "1.154833837", "1.155201819", "1.158940397",
"1.161348047",
"1.162953533", "1.175788329", "1.178036097", "1.179280397",
"1.18116463",
"1.198506534", "1.203134068", "1.207592892", "1.208610568",
"1.212525667",
"1.212841855", "1.214403157", "1.216756112", "1.228462377",
"1.23305986",
"1.241758242", "1.258212878", "1.265571914", "1.265707797",
"1.272666444",
"1.279325988", "1.289559543", "1.291191103", "1.296541003",
"1.305111821",
"1.315201411", "1.342263532", "1.344827586", "1.347944377",
"1.352831595",
"1.377717391", "1.37771934", "1.385440181", "1.403358682",
"1.411889597",
"1.412269549", "1.412544902", "1.433843384", "1.444088526",
"1.473706353",
"1.50172117", "1.508521601", "1.53030303", "1.532658694",
"1.537642783",
"1.54340949", "1.558746736", "1.595342983", "1.596774194",
"1.596969697",
"1.630653266", "1.702558635", "1.70468948", "1.718568102",
"1.740819711",
"1.760157274", "1.775510204", "1.804859813", "1.814081408",
"1.816513761",
"1.849050827", "1.870188679", "1.880195599", "1.887061404",
"1.91858679",
"1.929152149", "1.944140197", "1.946996466", "1.986547085",
"2.007683864",
"2.070983811", "2.099778271", "2.156359393", "2.159613059",
"2.163963964", "2.25951087", "2.261603376", "2.329896907",
"2.402555911", "2.414500684",
"2.444075305", "2.450268817", "2.484520124", "2.51119403",
"2.515064562",
"2.526086957", "2.554", "2.609715243", "2.61965812", "2.643854749",
"2.704166667", "2.883275261", "3.013186813", "3.02739726",
"3.206896552",
"3.320930233", "3.411627907", "3.688931298", "3.709677419",
"3.748267898",
"3.878865979", "3.936440678", "3.994230769", "33.15909091",
"4.095854922",
"4.29330254", "4.390957447", "4.514634146", "4.6367713",
"4.847665848",
"5.284023669", "5.387755102", "6.171339564", "6.183908046",
"6.36121673",
"6.847826087", "7.003496503", "7.193220339", "8.160550459",
"8.751879699"
), class = "factor"), MnFe = c(0.012176723, 0.010329834,
0.009460859,
0.004488071, 0.0033725, 0.003435313), MnIncCoh = c(169.4430276,
331.1977339, 490.5686845, 279.5752228, 272.3091921, 286.0408118
), CuRb = c(0.392971246, 1.484304933, 0.735426009, 0.491651206,
1.142857143, 0.4345898)), row.names = c(NA, 6L), class =
"data.frame")
Using your posted data. This should approximate the desired design.
First step, Transform the data from a wide format to a long format using the pivot_longer function from tidyr.
Then plot the data using "depth" as the independent variable and the parameters' values as the dependent variables.
Then use facet_wrap() to separate the plots. coord_flip() will make the independent variable (Depth) appear on the y-axis.
#fixed 1 column of data.
originaldata$ZrRb <- as.numeric(as.character(originaldata$ZrRb))
library(tidyr)
#Make wide
wapITRAX<-pivot_longer(originaldata, -1, names_to="parameter", values_to = "value")
library(ggplot2)
ggplot(wapITRAX, aes(x = Depth , y = value))+
labs(x = "Depth (cm)")+
geom_line() +
theme_classic() +
coord_flip() +
scale_x_reverse() +
facet_wrap(vars(parameter), nrow=1, scales = "free_x")

How to plot r squared for regression model for multiple group in r

o<-read.csv("old.csv')
Callosal_FA CST_FA SLF_FA Area_SMA_A
0.556566554 0.539971971 0.482016736 -0.007984
0.586793895 0.554954237 0.487595985 0.05567
0.613107046 0.597039029 0.467378312 0.136
0.59241945 0.58101919 0.460784717 0.03253
0.586344082 0.555524562 0.479480255 -0.01629
0.607088378 0.56048251 0.478998182 0.07981
0.595145661 0.571902322 0.461452732 0.07882
0.591501695 0.581156582 0.51408736 0.1143
0.587255765 0.566562088 0.462376015 0.1717
0.583943048 0.571209263 0.46400787 -0.01861
0.603512157 0.587332337 0.477376739 0.05672
0.582126533 0.565946603 0.459743433 0.002831
0.570966197 0.556258709 0.470341615 -0.003823
0.570307147 0.542675924 0.504833121 0.01764
0.579498276 0.569837284 0.475364742 -0.000387
0.570543729 0.542095809 0.468923119 0.117
0.613672747 0.572339549 0.486481493 0.1264
0.570649037 0.554125163 0.522845609 0.04696
0.580601176 0.558799894 0.504017998 0.1056
0.576166024 0.542110191 0.476548484 0.05783
0.579598762 0.546776236 0.491528835 0.08022
0.604775228 0.576144869 0.506060596 0.1515
0.555354582 0.556518053 0.492985322 -0.01114
0.580857907 0.556575944 0.484096309 0.03578
I wanted to plot the regression model result (r squared) for multiple group.
I have tried this one and managed to do for one group.
y <- lm(o$Area_SMA_A ~ o$CST_FA + o$SLF_FA + o$Callosal_FA)
then by using the following code:
library(ggplot2)
ggplot(y$model, aes_string(x = names(y$model)[2], y = names(y$model)[1])) +
geom_point() +
stat_smooth(method = "lm", col = "lightblue") +
labs(title = paste("Adj R2 = ",signif(summary(y)$adj.r.squared, 5),
"Intercept =",signif(y$coef[[1]],5 ),
" Slope =",signif(y$coef[[2]], 5),
" P =",signif(summary(y)$coef[2,4], 5)))
I produced this image, which what I wanted.
For I have three groups, I want to add another regression model for the two groups into this plot. Is there a way to do it please? Thank you.
m <- read.csv("middle.csv')
Callosal_FA CST_FA SLF_FA Area_SMA_A
0.599350895 0.59082334 0.518316923 0.04286
0.591540991 0.585592011 0.517415822 0.1291
0.62120411 0.613751115 0.456966929 0.05915
0.59344635 0.571179365 0.500941682 0.01122
0.621645795 0.599144316 0.487736421 0.0471
0.611521291 0.596407776 0.508636999 -0.08177
0.561589532 0.549150165 0.509993364 -0.002053
0.608089072 0.581477369 0.496346462 0.1157
0.583942196 0.576979247 0.505747697 0.01913
0.614675486 0.584447311 0.513085904 0.006673
0.599312499 0.585156336 0.475447955 0.05582
0.591977354 0.578031977 0.505042846 0.08293
0.602347244 0.582916321 0.504538196 -0.07645
0.628674145 0.595462642 0.469785878 0.04787
0.595963981 0.547983665 0.497874226 0.1132
0.604934306 0.586583356 0.502788492 0.08803
0.599656344 0.580235613 0.471793292 0.0118
0.587288357 0.559298093 0.535857414 0.06225
0.586031623 0.582565008 0.475876222 0.282
0.58277546 0.555852007 0.497386116 0.05266
y <- read.csv("young.csv')
Callosal_FA CST_FA SLF_FA Area_SMA_A
0.641939581 0.610050256 0.497039292 -0.05461
0.600969207 0.581011925 0.486918544 0.03801
0.597728695 0.569094851 0.522076721 0.08515
0.605851215 0.575788238 0.522207993 0.001711
0.615141198 0.586422768 0.49536629 0.08908
0.664600517 0.636086957 0.50723616 0.04712
0.617076761 0.577625164 0.50950881 0.02169
0.612482041 0.569112478 0.512551218 0.04043
0.627284885 0.597122461 0.541768958 0.003275
0.627408656 0.607896037 0.505038914 0.06681
0.609205487 0.577178474 0.508818934 -0.04759
0.606824376 0.593485569 0.530833127 0.05503
0.608929339 0.583816742 0.506553103 0.08804
0.623125338 0.599054187 0.518118823 0.04499
0.606161965 0.578010045 0.491883074 0.1487
0.605391626 0.585302201 0.488368677 0.1316
0.640007128 0.599344654 0.503622583 0.1909
0.598483618 0.588507596 0.508622188 0.2013
0.625079582 0.597286968 0.510829857 0.09116
0.620938861 0.577980188 0.52410613 0.02284
0.615765316 0.577922653 0.542867003 0.08179
0.606476852 0.571277288 0.486362068 0.2072
0.607761045 0.585516175 0.509739355 0.075
0.633673687 0.615854958 0.470963903 0.02209
0.641553411 0.621000635 0.492999164 0.101
0.588310547 0.57312727 0.490874808 0.07214
0.588535558 0.571499503 -0.08068 -0.03153

kriging spatial data, error var1.pred NA

hey i have a dataset that contains a z value of several sampling points, i want to interpolate them to an extent which i had created and gridded earlier, but i keep getting NA for my var1.pred value.
Error in seq.default(min(z[valid]), max(z[valid]), length = cuts + 1) :
'from' must be a finite number
In addition: Warning messages:
1: In min(z[valid]) : no non-missing arguments to min; returning Inf
2: In max(z[valid]) : no non-missing arguments to max; returning -Inf
here's the sample of my code.
#convert to spdf
coordinates(VELOCITY2)<- ~ X + Y
wg<-gstat(id='V', formula = V~1, data=VELOCITY2 )
#variogram
vv<-variogram(wg, cutoff=2000, width=100)
print(plot(vv,plot.numbers=T))
#initial variogram model
vm<-vgm(psill=0.02, range=550, model="Wav", nugget=0.005)
print(plot(vv, model=vm))
#fit variogram
vmf<-fit.variogram(vv,vm)
print(plot(vv, model=vmf))
# points to be interpolated
xx<-seq(from=488300, to=493200, by=100)
yy<-seq(from=9839400, to=9843200, by=100)
xy<-expand.grid(x=xx,y=yy)
coordinates(xy)<- ~ x+y
#ordinary kriging
kk<-krige(V~1, VELOCITY2, newdata=grid, model=vmf)
#plot
spplot(kk, "var1.pred")
my dput
structure(list(X = c(491810.00721276, 492328.97433014, 490911.89327549,
491615.51887818, 491615.51887818, 492894.620021, 492151.9979203,
492572.42983547, 492347.61141516, 491363.11382593, 491353.29751125,
492072.93297881, 490716.22127832, 492001.85406044, 492018.98815452,
492077.66253205, 492050.13933868, 490972.9839127, 491655.7778789,
491576.10196219, 492355.7400162, 492297.13259429, 492673.39998931,
492505.61098796, 492938.89101704, 492796.38161778, 492663.73140998,
492766.00027009, 492765.78359527, 491933.88466968, 492333.89864649,
492720.51113432, 492666.86568757, 492667.84225007, 492522.21401529,
492831.86513802, 492878.85586085, 493201.72451331, 492638.4094498,
492819.43215484, 492796.76931284, 492747.65005034, 492788.05373669,
492762.22524575, 492244.6870987, 492264.07015541, 493008.44619315,
492815.19350734, 492290.80611714, 492277.18886875, 493134.10402992,
493079.00698391, 493078.7734634, 491865.03127595, 492051.82525174,
492670.54384925, 492371.64034572, 491846.41277618, 492775.84682769,
492411.87697905, 492638.79940336, 492638.87569731, 492495.60555568,
490338.97585592, 490362.31496736, 490377.77810208, 489826.99294604,
489853.05092929, 489834.89614387, 490306.53786769, 490308.6412613,
489636.52553863, 489814.82046046, 489667.51345374, 490203.52431788,
491203.81484533, 491077.79256743, 491073.15243053, 491084.72133938,
490958.56246482, 490941.8523819, 491050.5864517, 490764.25539687,
490188.41994785, 490419.69106592, 490424.64284814, 490735.1435195,
490387.98623199, 489297.98195959, 489300.76955718, 490397.404689,
489661.53774563, 490894.97121725, 491108.0970718, 491121.07516021,
490612.02505037, 490615.90676419, 490585.43667157, 490603.01613921,
490591.97121732, 490535.56215982, 489776.74367816, 489958.02334144,
490066.17495271, 490031.0577653, 490190.24001607, 490211.20925445,
490214.70901031, 490003.5550795, 490604.9470475, 491123.82247476,
489847.73806302, 489976.32595346, 491294.31450964, 491007.18618313,
490369.37771158, 490354.14687653, 489273.8003189, 493001.10122237,
489539.64309234, 489012.00149085, 489142.87160796, 488409.5044205,
489078.66848285, 488976.293483, 489208.90896154, 489567.98757469,
490259.16079274, 490199.06081707, 490548.30691066, 489506.84939121,
489507.3984635, 489021.03298499, 488958.65896146, 490198.91372226,
489139.35891258, 488770.78518218, 488912.45339509, 488981.85549448,
488921.30666655, 489881.9584, 489672.654689, 489896.83645177,
489617.50564114, 489919.08864898, 489379.67238921, 491965.57000272,
491962.99825592, 492682.55996244), Y = c(9840083.34991902, 9839461.79230204,
9839986.04132536, 9839995.68878658, 9839995.68878658, 9839486.28400126,
9839853.49933327, 9839540.02081761, 9840596.63605199, 9840110.85284895,
9840123.81671612, 9839970.95343491, 9840217.59113002, 9840621.88702863,
9840939.65655981, 9840492.69562223, 9840364.30304425, 9840527.17804416,
9840153.15948959, 9841067.64777074, 9840733.71222385, 9840674.12726294,
9841985.09015356, 9841070.55597392, 9841095.72784893, 9841691.20050513,
9841720.81866902, 9841676.02960665, 9841673.8772629, 9841246.79718484,
9841422.27277072, 9841223.11749737, 9841990.83136454, 9841994.02765366,
9841585.87726301, 9841625.27960673, 9841777.00030985, 9841315.42609103,
9841973.23663798, 9841953.58624746, 9841942.04523162, 9841975.5950365,
9841921.55109098, 9841756.52179438, 9841803.37823974, 9841535.90655976,
9841695.74445044, 9841543.94562221, 9842174.64777079, 9842182.19659889,
9841376.54230196, 9841406.85773171, 9841406.8294114, 9841667.93488034,
9841949.92609129, 9841973.67413798, 9841897.64874747, 9842246.51398172,
9842171.76691117, 9841921.16437243, 9842138.53937229, 9842136.97003635,
9842380.89386459, 9839975.91827856, 9840042.95343491, 9840089.22394263,
9839589.44562232, 9839978.97296608, 9839972.67706767, 9840000.61749732,
9840046.62823938, 9840384.73566135, 9840302.14484109, 9840018.78351297,
9842250.19659889, 9842008.34698953, 9842209.1907394, 9842203.61261452,
9842217.50519284, 9841646.07648174, 9841683.64679432, 9842192.50519265,
9842183.09796603, 9842277.04034879, 9841973.30402079, 9841966.2620286,
9842110.55011467, 9842137.70929444, 9841800.60968466, 9841802.06769271,
9842097.74445065, 9841639.30499731, 9842195.63995834, 9842542.10675523,
9842116.55011473, 9841963.44464579, 9841829.30792699, 9841930.18585674,
9841916.66632539, 9842076.43488016, 9842082.75030963, 9842131.36456766,
9842160.68292699, 9842132.72687234, 9842139.580388, 9842277.19952862,
9842102.06183342, 9842101.63605201, 9842143.56085666, 9842095.04718486,
9841791.23859127, 9842670.9856614, 9843067.83527079, 9841903.34601296,
9841830.02277074, 9843129.77081764, 9843097.71808326, 9842604.47394268,
9840407.78644266, 9841952.95148171, 9842186.80011454, 9842038.18195044,
9841950.33234116, 9841538.57843486, 9842189.03839584, 9842814.41534891,
9842339.88898166, 9841739.39484119, 9841739.58624731, 9841750.17023162,
9842428.79816146, 9842966.83429408, 9841471.2981614, 9842028.5803879,
9841446.35480217, 9841755.92120829, 9842498.21027067, 9842371.99152076,
9842484.98761453, 9842385.0296069, 9840908.95441133, 9840831.87628639,
9842363.49738009, 9842105.66730203, 9842228.79132547, 9842178.55792694,
9840714.60968473, 9841214.43976303, 9842612.580388), V = c(6.8575591273834,
6.9091981717661, 6.81861092653243, 6.83544315031305, 6.83544315031305,
6.86928044706404, 6.87308158923757, 6.90102887370632, 6.93656422063757,
6.82756100389376, 6.8279728230811, 6.86916707425107, 6.88723098934826,
6.80869108780012, 6.81041873685147, 6.84851460419374, 6.83998564798438,
6.95597672440102, 6.84830429624437, 6.83571668853121, 6.81554716876882,
6.81653691949333, 6.86570172920796, 8.4183926299318, 6.82540315375968,
6.83694453186651, 6.83616001733514, 6.83366932709608, 6.83281519168562,
6.80657805425162, 6.79252081867902, 6.82804093704906, 6.86560659062316,
6.86560443487979, 6.81210486339519, 6.83024981887722, 6.85348365590436,
6.82737621767244, 6.86002774823981, 6.87890401083686, 6.87433304662824,
6.87310828011749, 6.87227288578899, 6.8406970455203, 6.79788666568234,
6.79289704147439, 6.84212179617635, 6.82201406577053, 6.85818255251501,
6.85882147766148, 6.81441839578817, 6.81109679654478, 6.81075562992338,
6.7695290140362, 6.78453986430733, 6.86470017777305, 6.82941505329464,
6.80201545646814, 6.89940111534327, 6.83386524913627, 6.88022016259413,
6.88000932400808, 6.89652613707228, 6.80951701420202, 6.81047007954082,
6.91318917468862, 6.81620742405261, 6.81609592365983, 6.81555114576811,
6.82359364040975, 6.82243031847043, 6.74848377545697, 6.77432742350875,
6.82113095074504, 6.73035636876622, 6.73157311534001, 6.68948620878327,
6.68804994926179, 6.68991058622174, 6.82298306298922, 6.79655085098109,
6.67806113435277, 6.67880893125878, 6.67462932793097, 6.76784233910405,
6.76791677619622, 6.68328242293505, 6.62279457979837, 6.80333643693172,
6.8017836170837, 6.74582126343433, 6.89705699013895, 6.67169623224357,
6.71095156872004, 6.70398418262631, 6.74214143831379, 6.77269747758851,
6.75839168452443, 6.75917136037271, 6.72567955117309, 6.72476400985706,
6.7940929187327, 6.76465011146018, 6.76185819997855, 6.76483265829331,
6.73217398985043, 6.7558472215621, 6.75644688026535, 6.76569985187991,
6.72096268409918, 6.79914751724536, 6.73903734858573, 6.76387478912845,
6.75057145302792, 6.78523214680787, 6.80514090870779, 6.79610181104104,
6.94632505323771, 6.87151617920864, 6.73501760825965, 6.8043110280055,
6.76237168672284, 6.87393143300434, 6.77223974414773, 6.81073018103125,
6.87291116726081, 6.85121557850258, 6.84429643254038, 6.71871340520001,
6.80737642861958, 6.87505551354325, 6.78810343307566, 6.77795244044547,
6.80445829734336, 6.96781526094347, 6.85340711647284, 6.86963847089039,
6.81734050576888, 6.79958184646059, 6.81761012476222, 7.11505624553277,
7.10405850051035, 6.74511681211045, 6.82787357725889, 6.75689662271503,
6.75616848941266, 6.82089585076081, 6.81664705027901, 6.91617186341967
)), .Names = c("X", "Y", "V"), row.names = c(NA, -149L), class = c("tbl_df",
"tbl", "data.frame"))

Plotting Curves from Data Frame Columns

i am facing a problem in plot ols estimations in a scatterplot:
I have this data frame: With 9 columns and 99 rows:
structure(list(Y = c(-0.145442175, 0.291096141, 0.489923112,
-2.038363166, 1.180430664, 0.188114666, 0.850922634, 1.172142766,
-3.980837975, 0.285762444, 2.497040646, 0.658010994, -0.925171981,
0.37076995, -1.108211119, -0.409242669, -1.234583525, -0.385841816,
0.016744771, -0.584406288, 1.17224811, -0.746804388, -0.625028046,
0.257871468, -2.735845346, 2.619304857, -0.406825232, 0.323665151,
2.218951363, -0.821029648, -0.872854889, -2.663306158, -0.121976044,
0.881566376, -1.972706678, -3.855576256, 2.927421113, 1.314753531,
0.234296206, 0.828464757, -0.909318569, 0.616134903, -0.567630403,
0.624571064, -0.414112923, 0.642200314, -0.309421266, 0.195312598,
-0.519988256, 0, 0.081070175, 0.032446432, -0.534025032, -0.426783307,
-0.38495511, -0.207900219, -1.953789746, -0.616924355, -0.783222881,
-1.935420969, 0.638445535, 1.080925923, -1.598076681, 0.25063631,
-0.697183766, 0.188971653, -0.415267389, -4.154506044, 1.163226552,
0.036569698, -0.547147074, 1.11937374, 0.383311682, -0.875037781,
-0.372684863, 0.306816004, -1.250561544, -1.042237738, -1.757788446,
0.021079982, 1.844023775, 1.674645753, -0.428546132, -0.527705597,
0.542202572, -0.621479123, -0.050415867, -0.122332943, 0.468553764,
0.216998274, 3.088480781, 0.434099931, 2.114916704, -2.407018936,
-0.127060127, 0.546756422, 0.263207486, 0.63453915, 0.76832746
), X = c(0.009476137, -0.0236354, 0.0094081, 0.11715252, 0.032324021,
0.0461193, 0.050794971, 0.032372819, 0.202121874, 0.390821859,
-0.124492596, -0.127305193, -0.22233597, -0.081113713, 0.09952616,
0.22494711, 0.226621495, 0.411607624, 0.089200478, -0.013454832,
-0.013547165, -0.232366214, 0.03140992, -0.026798837, -0.084556341,
-0.091993172, -0.303730207, -0.236679148, -0.284235285, -0.355253166,
-0.179645537, -0.01381843, -0.022950244, -0.050065976, -0.032018504,
-0.087168055, -0.081865767, -0.253991077, -0.242882759, -0.150225053,
-0.16596575, -0.156887247, -0.071795146, -0.100408802, -0.067307731,
0.024006869, -0.019250912, -0.02399429, 0.038421097, 0.062320065,
0.07187025, 0.024019462, 0.038421097, 0.033539309, 0.014351457,
-0.009575137, 0.014343968, 0.028561284, 0.0404213, 0.026065697,
-0.004700435, -0.072739794, -0.042217496, -0.05889531, -0.130522139,
-0.136291869, -0.120099035, -0.091418565, -0.122040844, -0.124609029,
-0.096255449, -0.190338762, -0.11611752, -0.055598423, -0.065293448,
-0.038746326, -0.029090518, -0.067627348, -0.082097445, -0.215845836,
-0.389993696, -0.264371785, -0.126530291, -0.111840985, -0.094952196,
-0.136700196, -0.190968195, -0.156564122, -0.181077278, -0.15381292,
-0.122020692, -0.107867301, -0.068642333, -0.034348677, -0.073289926,
-0.063314884, -0.092537576, -0.165375956, -0.15042398), Null = c(-0.036795117836493,
0.0120555676565338, -0.0366906491623935, -0.22323992930528, -0.0728300398338213,
-0.0955073599141197, -0.103350601084975, -0.0729090354522075,
-0.400153521158964, -0.887015257107641, 0.1362666683468, 0.13919994231771,
0.221388292373518, 0.087380368104602, -0.189831042487278, -0.452154909992189,
-0.456044210600938, -0.948567833126862, -0.170785020294756, -0.00253939338337472,
-0.00240533038312774, 0.228145471304061, -0.0713518661553421,
0.0165138860659871, 0.0915102566139487, 0.100284493544177, 0.265652059802101,
0.230938443729295, 0.257246215885006, 0.281209408151878, 0.188533028671265,
-0.00201164134414489, 0.0110851592192505, 0.0481858583559124,
0.0237904823161768, 0.094614581053392, 0.0882862377341187, 0.241468070168396,
0.234837060900023, 0.162029971029324, 0.176601607696189, 0.168307425791361,
0.0759851164110966, 0.109970788582389, 0.0703849242291975, -0.059492586621119,
0.00581616568295407, 0.0125631925046972, -0.0827672867080164,
-0.123023227393077, -0.139691063870559, -0.0595125909296922,
-0.0827672867080164, -0.074799966578053, -0.044324863847201,
-0.00820062690976645, -0.0443132308515717, -0.0667648997869916,
-0.0860567642206439, -0.0627706942069095, -0.0153914247452083,
0.0771546773236518, 0.0377224646820258, 0.0596889425617937, 0.1425196179012,
0.148379247725525, 0.13162698340227, 0.0996137276510431, 0.133686233062275,
0.136388667637584, 0.105222539655097, 0.197385328960716, 0.127361748973716,
0.0554268640818151, 0.0678473149754353, 0.0330232883757411, 0.0197208677278167,
0.0707862239701058, 0.0885648870712001, 0.216820906265572, 0.286245951224793,
0.247258814186372, 0.138394666330137, 0.122716205945161, 0.103719679674083,
0.148789344619283, 0.197893429730301, 0.168006688568371, 0.189742414352596,
0.165430712615822, 0.133664933948451, 0.11833998959919, 0.0720581343490991,
0.0270069004188009, 0.077834296346802, 0.0653403280475977, 0.100918894574441,
0.176071877748707, 0.162219750035618), OLS_1 = c(-2.97674658085357,
-2.95792547866683, -2.97674412477729, -2.7937460366665, -2.96913739819288,
-2.95639989365184, -2.95069150171007, -2.96910314906723, -2.3856485268894,
-0.647452287114872, -2.68293610049662, -2.670570393744, -2.10297963546522,
-2.84137496711892, -2.84927190111917, -2.23638642750757, -2.22477621905134,
-0.385841816000001, -2.87715002139054, -2.96747293407547, -2.96740133507642,
-2.02609643038743, -2.9697648045679, -2.95427875550959, -2.8310157181346,
-2.80733412921436, -1.38551048535346, -1.99204069101103, -1.57679230211392,
-0.821029648, -2.39395151432173, -2.96718943992586, -2.95867282134313,
-2.9175506236826, -2.94755679517459, -2.82290206987746, -2.83914454134393,
-1.84931168689084, -1.94200482386918, -2.56030139156351, -2.4747687889082,
-2.52507434784403, -2.86749990988846, -2.77838660436577, -2.87908253396987,
-2.97385415360498, -2.96244666805069, -2.95752797222193, -2.96426392038595,
-2.93361303993881, -2.91621877029975, -2.97384869333029, -2.96426392038595,
-2.96826157356433, -2.97653443074828, -2.97023260580068, -2.97653534550966,
-2.9715473503959, -2.96240424133875, -2.97289412424858, -2.9730125951007,
-2.86497897723402, -2.93188917574701, -2.89904800305061, -2.6561144854951,
-2.62935195635151, -2.70174255054932, -2.80922741244202, -2.69350740105694,
-2.68242924921473, -2.79295820376613, -2.32657978700299, -2.718248099245,
-2.90625073580661, -2.88407071600265, -2.93759776247538, -2.95143559806685,
-2.87827902655775, -2.83845377816351, -2.15100018436527, -0.392139380784325,
-1.7590965971582, -2.67400272569948, -2.73540774982849, -2.79741598960129,
-2.62741730304073, -2.322499279269, -2.52681590220219, -2.38514457172383,
-2.541507865502, -2.6935934995898, -2.75082409521646, -2.87570553083222,
-2.94427256930162, -2.86349763526591, -2.88884317216564, -2.80553055841713,
-2.47811758528604, -2.55927025907886), OLS_2 = c(-2.83865555876367,
-2.82203271957637, -2.83865550287755, -2.66277932892391, -2.83073328950317,
-2.8182826854432, -2.81275284604234, -2.83069942358793, -2.27571536741022,
-0.632851535784811, -2.56646067709365, -2.55491098827374, -2.02364579120999,
-2.71420058960775, -2.71564453925406, -2.13442002502496, -2.12343285482248,
-0.385841816, -2.74223576659719, -2.83068449367348, -2.83062014186059,
-1.95158880862936, -2.83135434505306, -2.81870405841395, -2.70456098525177,
-2.68251016192609, -1.35080974869909, -1.91966655284606, -1.53026524143009,
-0.821029648, -2.29619548286091, -2.83042962848176, -2.82271365766308,
-2.78489427206998, -2.81254809712918, -2.69700817487578, -2.71212546804251,
-1.78585373408616, -1.87276085874404, -2.45184700668681, -2.37183555552258,
-2.41889982491589, -2.73848954857785, -2.65553364194069, -2.74924637290594,
-2.8354502300085, -2.82614423798244, -2.82167034953476, -2.82594242161564,
-2.7962902949221, -2.77959589724382, -2.83544467118397, -2.82594242161564,
-2.82986834510621, -2.83829410413293, -2.83315419155684, -2.83829521382395,
-2.83312719078141, -2.82412509152621, -2.83447802392599, -2.83561001727694,
-2.73614728712302, -2.79813447119318, -2.76776591170989, -2.54140667394362,
-2.5163996858597, -2.58402223424852, -2.68427373122372, -2.57633280462435,
-2.56598731123967, -2.66911582708562, -2.23311605677819, -2.59943103595799,
-2.7744383205277, -2.75387620457868, -2.80339428073398, -2.81610308322424,
-2.74850042856033, -2.71148276169435, -2.06864445166113, -0.418358709691658,
-1.7012556906544, -2.558117011201, -2.61544592452239, -2.67326984561107,
-2.5145916492569, -2.22929491666958, -2.42052887445801, -2.28795076147412,
-2.43427089501948, -2.57641320261571, -2.62982944259216, -2.74611100908034,
-2.80953310903525, -2.73477077084888, -2.75830410348864, -2.68083005992821,
-2.37496906485549, -2.4508827380889), OLS_3 = c(-2.58083646581942,
-2.5683178338716, -2.58084089114316, -2.41826149362172, -2.57232965672457,
-2.56041470241702, -2.55521822468909, -2.57229650627193, -2.0704676472292,
-0.605591599496051, -2.34899840070827, -2.33897223601076, -1.87552769159633,
-2.47676312148376, -2.46615920192222, -1.94404642215785, -1.9342224786085,
-0.385841816000001, -2.49034777076914, -2.57529735049815, -2.57524652934739,
-1.81248137667339, -2.57293885513887, -2.56558300171966, -2.46846711008925,
-2.44946096338359, -1.28602268062379, -1.78454238349805, -1.4433981562183,
-0.821029648, -2.11368273887782, -2.57509593622485, -2.56887479307252,
-2.53722183306237, -2.56048377359198, -2.46196139684977, -2.47497795642607,
-1.66737628649693, -1.7434807939705, -2.24936019247138, -2.17965685727221,
-2.22066956504207, -2.49762425675709, -2.42616435450559, -2.50683929408026,
-2.57704694280319, -2.57166448720316, -2.56802106429762, -2.56769302344379,
-2.53990559282486, -2.52451787208599, -2.57704119998386, -2.56769302344379,
-2.57148502596854, -2.58019625622877, -2.57722566059429, -2.58019772985789,
-2.57469359055957, -2.56595475982599, -2.57605200249485, -2.57907626550515,
-2.49561557851369, -2.54841138215235, -2.52265924802504, -2.32724456926626,
-2.30551521644622, -2.36423571438323, -2.45098235381054, -2.35756515622,
-2.3485875529132, -2.43789928063234, -2.05861713726078, -2.37759686441414,
-2.52834152993493, -2.51080007744427, -2.55283331443161, -2.56343418632904,
-2.50620082129485, -2.47442497328161, -1.91488441727801, -0.467310795744689,
-1.59326539683083, -2.34175573481226, -2.39147445613669, -2.44148615865099,
-2.30394357612981, -2.05528024243402, -2.22208856552246, -2.10648769733616,
-2.23405702128991, -2.35763491117015, -2.40392966200837, -2.50415507637054,
-2.55797145858227, -2.49443477420494, -2.51458468009137, -2.44801138045477,
-2.18238842077399, -2.24852076027753), OLS_4 = c(-2.4289478285331,
-2.41681903415288, -2.42895104301202, -2.27867081965274, -2.4213161496905,
-2.41038194422522, -2.40559515788832, -2.42128586809391, -1.95522949388955,
-0.590647453749078, -2.21077815389366, -2.20138321248198, -1.76758669368012,
-2.33060054299992, -2.32313500877883, -1.83755181381677, -1.82840597739465,
-0.385841816, -2.34557046847711, -2.42346978407977, -2.42342111188123,
-1.70861264386732, -2.42187239429871, -2.41422413566286, -2.32281181955877,
-2.30497392699143, -1.21632553238408, -1.68248005204524, -1.36346128591018,
-0.781669317752002, -1.99042352676657, -2.42327691796255, -2.41734804581689,
-2.38744248609079, -2.40939495374384, -2.31670510436427, -2.32892438647688,
-1.57289978140148, -1.64407512538075, -2.11744278294415, -2.05217911016675,
-2.09057710272701, -2.35019495754122, -2.28311871426765, -2.35885543710246,
-2.42560672084754, -2.42000135641999, -2.4165372393818, -2.41707097497419,
-2.39145946177805, -2.3772271125231, -2.42560153071694, -2.41707097497419,
-2.420544236609, -2.42841594588832, -2.4253216199613, -2.42841722040367,
-2.42347169955882, -2.41547562547196, -2.42470587973943, -2.4271143253132,
-2.34830761179908, -2.39799094116799, -2.37373288731684, -2.19039487337143,
-2.17003793409615, -2.22505776553193, -2.30640152341961, -2.21880622042115,
-2.21039315621698, -2.294126428977, -1.93888869626962, -2.23758086989921,
-2.37908034073483, -2.36257901260101, -2.4021644964107, -2.41218787827608,
-2.35825527028976, -2.3284051877118, -1.80440438182757, -0.451087514089169,
-1.50359480720157, -2.20399138892979, -2.25058992243427, -2.29749148286179,
-2.16856567860513, -1.93576601076367, -2.09190575790345, -1.98368936201681,
-2.10311255036163, -2.21887159162467, -2.26226743315126, -2.35633238493592,
-2.407018936, -2.34719820268328, -2.36613768370737, -2.30361375259329,
-2.05473632620086, -2.11665669129059), OLS_5 = c(-2.2911912568638,
-2.28123967681215, -2.29119683586224, -2.14805590207021, -2.28325670505768,
-2.27261386268403, -2.268006850245, -2.28322682471889, -1.84560662105751,
-0.576090713535621, -2.0945064647732, -2.0859234999636, -1.68828464788266,
-2.20368547406672, -2.18986194988925, -1.73587625378362, -1.72735189094969,
-0.385841816, -2.21101101562234, -2.28700200417098, -2.28696051646281,
-1.63411554699496, -2.28380625780731, -2.27896044246845, -2.19661244829441,
-2.18039652225164, -1.18146437845759, -1.6101070827248, -1.31682391434599,
-0.781364138557704, -1.89278224977018, -2.28683751979873, -2.28170279433502,
-2.25507742887343, -2.27469315563211, -2.19106352335337, -2.20216376634672,
-1.50940418054145, -1.57481865165838, -2.00915316980509, -1.94938461398854,
-1.98455653642811, -2.22145418758665, -2.1605019074557, -2.22929361026136,
-2.28754415075922, -2.28401553991566, -2.28099274980288, -2.2790962708342,
-2.25448944582185, -2.24095826297856, -2.28753886744317, -2.2790962708342,
-2.28249611688223, -2.29051254450738, -2.28856644852124, -2.29051401027405,
-2.28539476406181, -2.27754391464367, -2.28663199585719, -2.29003809100396,
-2.21974449731936, -2.26453458741267, -2.24273365564754, -2.0758820670505,
-2.05727208640147, -2.1075473897372, -2.18169509790509, -2.10183883759691,
-2.0941547862828, -2.17052540371127, -1.84552009498619, -2.11897925887711,
-2.2475539961602, -2.23266092019845, -2.26826235385205, -2.27716458812284,
-2.22875067319638, -2.20169236013157, -1.72209385129724, -0.476893951190187,
-1.44569200778405, -2.08830648613957, -2.13084935049209, -2.17358829667077,
-2.05592583172644, -1.84265559786228, -1.98577321824112, -1.88660772323127,
-1.99603456442263, -2.10189853669134, -2.14149931364702, -2.22701080101746,
-2.27258448425562, -2.21873931960315, -2.23587705524471, -2.17915915787995,
-1.95172754860073, -2.00843362344438), OLS_6 = c(-2.14615029819501,
-2.1274826763545, -2.14613692884822, -2.038363166, -2.14482079785526,
-2.13839956793073, -2.1352633011825, -2.14480554064275, -1.77137087834078,
-0.604458131512312, -1.92044345866761, -1.91142894340333, -1.5035051350835,
-2.03720410348948, -2.07364942604987, -1.67230210256299, -1.66457879312031,
-0.427523081653794, -2.09111249534671, -2.1358169999572, -2.13575175544593,
-1.44873737433719, -2.14509683128765, -2.12442374236989, -2.02946586195686,
-2.01185030632841, -0.994510606111227, -1.42450007218492, -1.12983335353955,
-0.596198212559954, -1.7115906309286, -2.13555900800151, -2.12811588444992,
-2.09509015766854, -2.11889016916752, -2.02341958358771, -2.03553614239934,
-1.32305159796573, -1.38891263096519, -1.83141440901763, -1.76969899713653,
-1.80596583024281, -2.05682837956465, -1.99043348930533, -2.06558998487816,
-2.14664801486533, -2.13135448546891, -2.1271468279034, -2.14250449627423,
-2.12545741758249, -2.11509475538252, -2.1466464083569, -2.14250449627423,
-2.14442504670383, -2.14684049810003, -2.13838680613343, -2.1468398441846,
-2.14583791450693, -2.14156460788614, -2.14633891192499, -2.14114136796206,
-2.05492693125432, -2.10632999229998, -2.08080895764288, -1.90090288083161,
-1.88144765246016, -1.93417313784094, -2.01325520240903, -1.9281579797124,
-1.92007377040746, -2.00120005643771, -1.66327168223962, -1.94624457366434,
-2.08634430885867, -2.06937728900294, -2.11086434271986, -2.12206882778948,
-2.06498074945642, -2.03501978622706, -1.5377512452434, -0.29431817292714,
-1.25902518147068, -1.91393009686737, -1.95881793980313, -2.00449939786682,
-1.88004274517372, -1.66034827254381, -1.80722288608151, -1.70526848086161,
-1.81783189921089, -1.92822084254891, -1.97013652098612, -2.0630309651189,
-2.1162243283256, -2.0538104595074, -2.07300962091288, -2.0105124912345,
-1.7721107506457, -1.83066883021211)), .Names = c("Y", "X", "Null",
"OLS_1", "OLS_2", "OLS_3", "OLS_4", "OLS_5", "OLS_6"), row.names = c(NA,
99L), class = "data.frame")
My scatter plot will consist of the first column (Y) and the second column (X).
The third column i will not use.
From the fourth column are the curves that are fitted values of OLS regressions.
How do I include them using the plot function?
i am doing this, but its not working
plot(data[,2],data[,1])
for(i in 4:9){
lines(data[,i])
}
What am i doing wrong?
Basically you want
data <- data[order(data$X), ] ## reordering so that `X` is increasing
plot(data$X, data$Y)
for (i in 4:9) {
lines(data$X, data[,i], col = i) ## remember to set `x-coordinates`
}
legend("topright", legend = names(data)[4:9], col = 4:9, lty = 1) ## add legend

Resources