Selecting a range of latitudes and longitudes for plotting - r

I am trying to select a range of latitude and longitudes (and all grid cells within that range) to plot y-axis values (precipitation) as a function of x-axis values (emissions) based on aggregating (averaging) each of those grid cells. Thus, let's say that I wanted to aggregate/average each of the values of these grid cells and plot this as a function x-axis values. The values already computed for each grid cell for the y-axis correspond to average precipitation and consist of 140 values per grid cell. So, the idea would be the average the first value of each grid cell, then the second value of each grid cell, then the third value of each grid cell.....all the way to value #140 of each grid cell. This would yield 140 values that would be plotted as a single line plot. The variable containing these y-axis values is called "RCP1pctCO2Mean", which is 3-dimensional and has the following attributes:
class : RasterBrick
dimensions : 64, 128, 8192, 140 (nrow, ncol, ncell, nlayers)
resolution : 2.8125, 2.789327 (x, y)
extent : -181.4062, 178.5938, -89.25846, 89.25846 (xmin, xmax, ymin,
ymax)
coord. ref. : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0
data source : in memory
names : 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, ...
min values : 0.5261063, 0.5625295, 0.5301681, 0.6019284, 0.5177065,
0.6775601, 0.5336965, 0.5213086, 0.5971723, 0.5305891, 0.5514522,
0.5481200, 0.4681806, 0.5437223, 0.5988844, ...
max values : 113.13776, 114.50780, 94.93643, 100.35409, 102.65459,
101.80622, 104.95480, 99.17839, 104.68667, 119.48342, 100.60391,
108.91896, 104.38969, 99.35735, 99.30752, ...
dput(head(RCP1pctCO2ModelMean,5))
structure(c(8.06056747436523, 7.75062637329102, 7.73271865844727,
7.65678634643555, 7.75580596923828, 8.74016799926758, 8.28938064575195,
8.10810775756836, 8.10339508056641, 7.86748199462891, 10.6165672302246,
11.057600402832, 11.9450210571289, 12.3012786865234, 12.1853874206543,
8.51755447387695, 9.16703491210938, 7.79627380371094, 7.53922271728516,
8.14367599487305, 7.83845596313477, 8.22455596923828, 8.21650848388672,
8.186181640625, 8.34122924804688, 9.1652702331543, 9.03225784301758,
9.04341430664062, 8.98676376342774, 9.0090087890625, 10.5393569946289,
11.1039672851562, 11.2379959106445, 11.333048248291, 12.0459312438965,
10.0039085388184, 9.90224914550781, 9.24856033325195, 9.26007308959961,
9.17901306152344, 9.55168991088867, 10.1229606628418, 10.4827072143555,
11.0505676269531, 12.1166931152344, 9.01049194335937, 9.39016952514648,
10.1307640075684, 9.67219467163086, 9.63750610351562, 8.14553070068359,
8.06989059448242, 7.88368453979492, 7.78139190673828, 7.76021575927734,
8.09026031494141, 8.47544021606445, 8.53607864379883, 9.07613296508789,
8.45474319458008, 9.91985092163086, 10.0807098388672, 9.90288009643555,
10.3433769226074, 10.349861907959, 11.2688484191895, 11.9406700134277,
12.0929878234863, 11.8347038269043, 10.4144630432129, 12.5179420471191,
13.8380874633789, 14.6360946655273, 14.0480010986328, 13.3910736083984,
9.96924514770508, 10.3363311767578, 10.7060684204102, 10.889599609375,
10.6653129577637, 10.5626991271973, 10.047868347168, 10.2751319885254,
10.8663063049316, 11.7612907409668, 8.94287414550781, 9.23690567016602,
8.96449661254883, 8.62908248901367, 8.30552215576172, 8.18369979858398,
7.69772872924805, 7.5989143371582, 7.7111930847168, 7.66501693725586,
10.2653533935547, 9.95307464599609, 9.71684875488281, 9.06423645019531,
9.75586776733399, 9.83898010253906, 9.89386901855469, 9.82217864990234,
9.5716667175293, 9.0698844909668, 9.32229309082031, 9.11556243896484,
8.89283828735352, 9.28563232421875, 9.30773696899414, 7.3968879699707,
7.93817901611328, 7.98456954956055, 7.95158386230469, 8.098583984375,
8.80923156738281, 8.49755935668945, 8.15531387329102, 8.46451263427734,
8.68563690185547, 8.89066619873047, 9.1489013671875, 9.33551254272461,
10.1721000671387, 11.0765029907227, 8.04215698242188, 8.19046478271484,
8.40826110839844, 8.67290573120117, 9.18575973510742, 8.3109245300293,
9.11093368530273, 9.81162109375, 9.86040802001953, 9.50505447387695,
10.5363166809082, 9.481689453125, 9.08926620483398, 9.52505798339844,
8.68244476318359, 8.98393630981445, 10.1831520080566, 10.5507629394531,
10.1666023254395, 9.69676284790039, 8.34152221679688, 8.42654571533203,
8.35252838134766, 8.00343780517578, 7.82494201660156, 9.53874359130859,
9.61768341064453, 9.41213912963867, 9.64521713256836, 10.2018295288086,
10.4372673034668, 10.2102493286133, 11.5178192138672, 10.6066253662109,
8.7693229675293, 10.0142112731934, 10.0218872070313, 9.85999298095703,
9.61988754272461, 9.6459342956543, 8.68394012451172, 8.94484100341797,
8.99103088378906, 9.28208084106445, 9.25485153198242, 9.43952713012695,
9.46648941040039, 9.29736251831055, 9.17809677124023, 9.15292205810547,
9.86052169799805, 9.73173980712891, 9.56215209960937, 9.07076568603516,
8.6406623840332, 9.93644332885742, 10.4607345581055, 10.7315849304199,
10.8101539611816, 10.7245536804199, 8.11333770751953, 8.19898986816406,
9.07229690551758, 8.52103500366211, 8.86794967651367, 13.8089904785156,
14.1628616333008, 14.1612731933594, 13.8391723632812, 13.4812423706055,
10.3034606933594, 10.3893714904785, 10.6155006408691, 10.7073356628418,
10.2250640869141, 10.7160522460937, 10.9937141418457, 11.4714202880859,
11.451326751709, 11.2873542785645, 10.1245864868164, 10.7054138183594,
11.2963111877441, 11.2652069091797, 11.1456642150879, 10.2463417053223,
10.7579162597656, 10.9033828735352, 10.8239326477051, 10.9808113098145,
9.11014785766602, 9.57601928710938, 9.13134765625, 8.95542297363281,
9.004345703125, 10.011986541748, 10.4038223266602, 10.7760765075684,
10.8257308959961, 11.1445190429687, 10.5380798339844, 10.2488784790039,
10.957502746582, 10.7088333129883, 10.6826118469238, 8.83894119262695,
9.23114776611328, 8.83077545166016, 8.79364471435547, 9.5740608215332,
9.49266204833984, 9.36201629638672, 8.86088562011719, 8.70853042602539,
8.84630584716797, 8.54691619873047, 9.06980133056641, 9.68758087158203,
10.2382141113281, 11.163306427002, 8.64593887329102, 8.68509368896484,
9.1695442199707, 9.96113128662109, 10.5646827697754, 10.6185249328613,
11.3931076049805, 11.984349822998, 12.3294868469238, 12.6237396240234,
9.07107849121094, 8.50119094848633, 8.165673828125, 8.46488571166992,
8.7583610534668, 9.64865646362305, 11.1105819702148, 11.4002655029297,
10.5027854919434, 10.1196365356445, 9.72924041748047, 10.009349822998,
10.326407623291, 9.36514053344727, 7.79587249755859, 9.68682708740234,
9.97370910644531, 10.053092956543, 10.0790946960449, 9.88732604980469,
8.5629150390625, 8.67814178466797, 8.62764282226562, 8.40693130493164,
8.43646850585938, 8.18491287231445, 8.28544006347656, 8.32042083740234,
8.37117156982422, 8.74075393676758, 10.0410667419434, 9.86564865112305,
9.83465423583984, 10.2372169494629, 10.6772819519043, 10.7158340454102,
11.1418548583984, 11.3801231384277, 12.3574913024902, 12.7044395446777,
10.933625793457, 10.1660957336426, 8.9215690612793, 9.42180328369141,
10.0563125610352, 10.6793243408203, 10.605298614502, 10.532593536377,
10.4527519226074, 10.4930633544922, 9.60543518066406, 9.36136474609375,
9.02150115966797, 8.90760498046875, 9.39027709960937, 11.475407409668,
11.8939018249512, 12.2665367126465, 12.3773796081543, 12.2639549255371,
12.1986907958984, 12.9296035766602, 14.6138290405273, 12.5118690490723,
11.1584533691406, 11.6089347839355, 12.184122467041, 12.7126617431641,
12.8846618652344, 12.7170280456543, 10.4427520751953, 11.0104148864746,
11.0574317932129, 10.7889305114746, 11.428002166748, 12.8730773925781,
13.0610855102539, 13.9772033691406, 14.7699234008789, 15.2759140014648,
11.0441619873047, 12.8132598876953, 13.1194686889648, 13.3944534301758,
13.439665222168, 11.8274314880371, 12.5198951721191, 13.1492401123047,
13.310026550293, 13.7720932006836, 10.7566696166992, 11.1153205871582,
10.8651023864746, 10.8898574829102, 11.1934638977051, 10.3416526794434,
10.3679290771484, 10.5599853515625, 10.4897705078125, 10.3551536560059,
10.7984680175781, 9.9300895690918, 9.24563140869141, 8.82577590942383,
8.11003036499023, 9.96226196289063, 10.2302505493164, 10.1707107543945,
9.78019790649414, 9.43962249755859, 11.1743797302246, 11.4034492492676,
10.9475212097168, 11.3065505981445, 11.3690071105957, 9.07691192626953,
8.84776763916016, 8.99648666381836, 9.46799697875977, 10.1394309997559,
9.5390739440918, 9.72808990478516, 10.1971199035645, 10.3575813293457,
10.6958648681641, 12.1900230407715, 11.986629486084, 12.139289855957,
12.308757019043, 12.2176116943359, 10.5569931030273, 10.2808883666992,
9.90982055664062, 9.57319793701172, 9.34677658081055, 9.55350112915039,
9.78641738891602, 10.0147956848145, 10.3079559326172, 10.7283164978027,
8.64087753295899, 8.30433197021484, 8.60956497192383, 8.60016937255859,
8.54900741577148, 9.71597595214844, 8.8296272277832, 8.63880462646484,
8.47726745605469, 8.84029846191406, 10.1157096862793, 9.88607330322266,
9.32783508300781, 9.36255416870117, 10.1161201477051, 11.8296203613281,
12.8728881835937, 13.2580642700195, 13.5822448730469, 14.2079498291016,
8.7088752746582, 8.82841796875, 9.62056427001953, 9.44509429931641,
8.63022689819336, 10.438533782959, 10.7936264038086, 11.1668098449707,
11.4698097229004, 11.6895088195801, 10.657413482666, 10.6490295410156,
10.6090156555176, 10.7308578491211, 10.2125785827637, 8.9413688659668,
8.8182373046875, 8.83381042480469, 8.8937255859375, 8.81535415649414,
9.55851669311523, 9.21836166381836, 9.18197784423828, 9.25029678344726,
9.27070083618164, 8.93240661621094, 8.90249099731445, 9.04313659667969,
9.20108184814453, 9.35103607177734, 10.5618942260742, 11.06572265625,
11.445832824707, 11.9180442810059, 11.9453758239746, 12.2584243774414,
11.9192649841309, 11.7775825500488, 11.8008255004883, 12.2185432434082,
9.08272094726562, 9.66560821533203, 9.48288192749023, 9.70498886108398,
9.56975555419922, 10.349006652832, 10.0300796508789, 9.5968864440918,
9.54965896606445, 9.00812759399414, 10.6454582214355, 10.7713653564453,
11.4645782470703, 11.8325187683105, 10.4740211486816, 9.41022796630859,
9.07474822998047, 8.61860656738281, 8.40045318603516, 8.653662109375,
9.65197830200195, 9.63723449707031, 9.51137924194336, 9.57775497436523,
9.69926528930664, 8.98936157226563, 9.40332794189453, 9.6757568359375,
9.79740600585938, 9.78034057617188, 10.2542152404785, 10.1619010925293,
10.3302688598633, 10.5525291442871, 10.7628135681152, 10.2399101257324,
10.7667839050293, 10.9936302185059, 10.8458152770996, 10.6235733032227,
9.24678955078125, 9.91125411987305, 9.86469879150391, 10.2658912658691,
10.1045028686523, 8.5959098815918, 8.49214401245117, 8.39304656982422,
8.6228157043457, 8.61747360229492, 8.91071395874024, 8.69116439819336,
8.56472625732422, 8.52434997558594, 8.64141616821289, 11.7017639160156,
12.3285125732422, 12.0996276855469, 11.056103515625, 10.7624618530273,
11.0327911376953, 12.0061622619629, 11.4006072998047, 10.8051193237305,
9.97594757080078, 10.138427734375, 10.6871589660645, 11.5516906738281,
12.4771171569824, 12.7644668579102, 9.02487182617188, 8.8429817199707,
9.50254135131836, 9.22763977050781, 8.60505981445312, 9.68806610107422,
9.77085647583008, 9.95944061279297, 10.3024574279785, 10.7512054443359,
8.78317031860352, 8.94360504150391, 9.19855346679688, 9.44350967407227,
10.2734016418457, 11.8314613342285, 12.4076805114746, 12.5829055786133,
12.3372856140137, 11.6992652893066, 8.82733459472656, 8.83338012695313,
9.01612014770508, 9.31243057250976, 9.52871017456055, 9.1939811706543,
9.20086669921875, 9.17466049194336, 9.3692512512207, 9.51042022705078,
9.55322723388672, 9.08766632080078, 9.01230697631836, 9.08840942382812,
8.89073867797851, 10.0157516479492, 9.88614654541016, 9.88927917480469,
9.89448471069336, 9.81435546875, 10.0982810974121, 10.8838890075684,
12.3737106323242, 12.0203163146973, 12.3476577758789, 8.12273559570312,
8.38578720092773, 8.5104736328125, 8.67580947875977, 8.8884521484375,
11.280297088623, 9.81758041381836, 9.12517623901367, 9.57337417602539,
9.93379745483398, 9.97190170288086, 9.9986442565918, 10.1007339477539,
10.368416595459, 10.8695823669434, 10.6418167114258, 10.1072540283203,
9.56202163696289, 10.1464050292969, 11.0315299987793, 15.5208480834961,
16.458154296875, 16.6033325195312, 16.9603851318359, 17.5446258544922,
11.6627197265625, 11.2967132568359, 11.0439735412598, 10.7337928771973,
10.724112701416, 10.414665222168, 10.1816375732422, 10.0053001403809,
10.0604530334473, 10.3831871032715, 12.5386772155762, 12.61123046875,
11.556861114502, 10.815104675293, 10.3798713684082, 12.8164276123047,
12.5301361083984, 12.1416007995605, 11.1006622314453, 10.5525985717773,
12.7033218383789, 13.5069030761719, 14.3273956298828, 14.1952850341797,
13.8862731933594, 11.5364295959473, 11.4725784301758, 11.4327659606934,
11.6338516235352, 11.9929733276367, 10.9388458251953, 10.3944259643555,
10.7864356994629, 10.4639877319336, 10.4192794799805, 10.584090423584,
11.0457565307617, 11.1049293518066, 11.2156471252441, 11.2940994262695,
11.5789703369141, 11.3852058410645, 11.312281036377, 10.8983863830566,
10.7498306274414, 11.648999786377, 12.1443000793457, 12.3075752258301,
12.4243774414062, 12.5793846130371, 9.53582992553711, 10.3421630859375,
10.6005973815918, 10.7018409729004, 10.1489151000977, 15.3236968994141,
16.6732727050781, 17.0259368896484, 16.9183380126953, 18.6379180908203,
11.5663963317871, 13.1421661376953, 15.772624206543, 15.2286743164063,
14.1305938720703, 10.4395881652832, 10.3302070617676, 10.3431907653809,
10.6234725952148, 10.6850440979004, 10.1355682373047, 10.7770133972168,
11.2220344543457, 11.121208190918, 10.9759727478027, 10.4280487060547,
11.4163436889648, 11.5880157470703, 11.367276763916, 11.7584564208984,
12.3622993469238, 12.8618209838867, 13.0096267700195, 12.7293991088867,
12.3698738098145, 13.4502487182617, 13.9361175537109, 14.3981384277344,
14.8455154418945, 14.7898071289063, 10.8285942077637, 11.0353393554688,
13.0554183959961, 11.4933654785156, 10.6509674072266, 12.3171768188477,
11.9701278686523, 11.7984405517578, 11.3652336120605, 11.2212104797363,
11.7747032165527, 12.0631629943848, 11.609635925293, 12.5300575256348,
12.5889640808105, 14.9442291259766, 15.244026184082, 15.1138778686523,
14.8327117919922, 14.0672607421875, 13.8624649047852, 14.0361633300781,
13.746142578125, 13.3890182495117, 12.7229309082031, 11.7338531494141,
12.4392601013184, 12.6126739501953, 12.3359680175781, 12.0300300598145,
13.0475494384766, 13.4870010375977, 13.1330673217773, 12.8889770507812,
11.5218353271484, 12.9545684814453, 12.6848930358887, 12.5185249328613,
12.1927146911621, 12.1243927001953, 13.6282653808594, 13.0329132080078,
14.021321105957, 12.7892906188965, 11.5679710388184, 12.4451988220215,
12.6035362243652, 12.8059692382812, 13.2598541259766, 14.1865646362305,
11.4154563903809, 12.0055801391602, 12.315015411377, 12.8833023071289,
13.0320266723633, 11.3923133850098, 12.2035797119141, 12.3115371704102,
12.4416717529297, 12.8906295776367, 11.4880004882812, 11.4702560424805,
11.063402557373, 11.520093536377, 11.4941116333008, 10.2884147644043,
10.4248268127441, 10.2943084716797, 10.4938522338867, 10.171199798584,
10.8181655883789, 10.7096382141113, 10.3980682373047, 9.85380783081055,
9.06302337646484, 11.087833404541, 11.297770690918, 11.4384208679199,
11.8333045959473, 12.6423866271973, 11.0677742004395, 11.1663566589355,
14.6535186767578, 12.696688079834, 11.9809646606445, 12.5395179748535,
13.5047088623047, 13.6589874267578, 13.3168762207031, 13.8064010620117,
13.2664398193359, 12.1466331481934, 11.7755020141602, 11.2214569091797,
11.967204284668, 13.3286544799805, 13.0230804443359, 12.4111663818359,
11.5664184570313, 11.507154083252, 10.7482841491699, 10.2523262023926,
10.5137763977051, 10.6416915893555, 10.925708770752, 12.1461143493652,
12.5572776794434, 13.0962661743164, 14.0371612548828, 15.6014785766602,
12.1392105102539, 12.4931434631348, 12.5056259155273, 12.1932655334473,
12.1573226928711, 12.3337669372559, 12.2943237304688, 12.3938110351563,
12.3535987854004, 12.2837997436523, 11.199015045166, 11.0299026489258,
11.4233680725098, 11.3639068603516, 10.776929473877, 15.9578018188477,
18.5940185546875, 16.6747985839844, 12.2459892272949, 11.7588233947754,
10.8550109863281, 9.30074844360352, 9.1522102355957, 9.23973083496094,
9.94997406005859, 21.4329086303711, 20.6476043701172, 20.4366928100586,
19.5227752685547, 18.6518371582031, 13.4605987548828, 13.7962448120117,
14.7393859863281, 14.0098480224609, 13.7022705078125, 13.8268768310547,
13.9131362915039, 14.1420150756836, 14.5985153198242, 15.3337585449219,
10.2976509094238, 10.6874450683594, 10.6152442932129, 10.3048629760742,
10.0026962280273, 10.8500930786133, 12.313077545166, 12.8156112670898,
12.4003845214844, 12.062833404541, 11.8806510925293, 11.9595237731934,
12.7401191711426, 13.2080368041992, 12.648217010498, 14.9934814453125,
16.2576446533203, 16.2247314453125, 15.6419891357422, 14.6681213378906,
10.2608001708984, 10.204532623291, 10.8466209411621, 10.8580635070801,
10.5870002746582, 12.5249855041504, 13.235661315918, 13.3415161132812,
13.2680938720703, 13.6001312255859, 10.9847831726074, 10.9526977539062,
12.1701835632324, 10.9867134094238, 9.62559356689453, 14.671305847168,
15.7670822143555, 16.5200897216797, 17.2838760375977, 18.5971923828125,
12.4218063354492, 11.5890487670898, 11.3723731994629, 11.5280731201172,
12.0742370605469, 11.0745147705078, 10.6635795593262, 10.507258605957,
10.358984375, 10.2047622680664, 11.231372833252, 11.1456260681152,
10.8480117797852, 10.3289199829102, 9.81418380737305, 13.1054672241211,
13.3634078979492, 13.6211837768555, 14.0335067749023, 13.8940582275391,
11.4586517333984, 11.3107444763184, 12.1839057922363, 11.6442543029785,
10.0313461303711, 11.5685440063477, 13.461506652832, 13.9238220214844,
12.9616546630859, 12.0961059570313, 15.3089569091797, 14.529133605957,
12.9434677124023, 11.6556198120117, 10.7868843078613, 13.7810775756836,
14.6285568237305, 14.3505905151367, 12.6306495666504, 11.0644081115723,
13.6406951904297, 13.0658325195313, 11.6025444030762, 10.8227111816406,
10.6873641967773, 14.1373397827148, 15.3703903198242, 16.4337646484375,
16.3727081298828, 16.0835571289062, 9.32263946533203, 9.81831588745117,
10.2201850891113, 10.4734298706055, 10.432006072998, 9.83030319213867,
9.97000503540039, 10.0799949645996, 10.1364440917969, 10.2572006225586,
11.0884750366211, 11.7081008911133, 11.6705154418945, 11.3407791137695,
10.8895950317383, 12.5218650817871, 14.0942642211914, 14.4718688964844,
13.89228515625, 13.6701507568359, 12.1981781005859, 12.0445930480957,
13.2136001586914, 12.3655586242676, 11.3412635803223, 12.4833915710449,
11.7754150390625, 11.693514251709, 12.1434707641602, 12.578157043457,
10.8240905761719, 10.6200454711914, 10.1548942565918, 9.93075942993164,
10.2509727478027, 15.1843124389648, 15.0255569458008, 14.8466598510742,
15.1112106323242, 16.322314453125, 13.0618423461914, 14.1408599853516,
13.0202850341797, 13.6123336791992, 14.1315383911133, 12.7721778869629,
12.7291236877441, 13.1240371704102, 13.9317779541016, 14.8634048461914,
11.2654998779297, 12.3573486328125, 13.258935546875, 12.5885932922363,
12.298176574707, 13.9735443115234, 14.5013107299805, 15.1036834716797,
15.3642013549805, 15.3964065551758, 11.2573120117187, 11.9989395141602,
12.0881286621094, 12.2077728271484, 12.2564170837402, 10.4119132995605,
10.5908760070801, 10.9370986938477, 10.9051147460937, 11.1913566589355,
12.3859100341797, 12.3887336730957, 13.8762435913086, 14.5053375244141,
14.3133880615234, 13.3645736694336, 12.5263206481934, 12.3438980102539,
12.354638671875, 13.1039337158203, 11.9940643310547, 11.8350044250488,
12.025373840332, 12.0098609924316, 12.8584075927734, 13.5398986816406,
13.7642166137695, 14.2189575195312, 14.8441345214844, 15.6326904296875,
14.128108215332, 13.0739379882812, 11.9704299926758, 11.3222679138184,
12.2943817138672, 13.596760559082, 13.6019485473633, 13.8898880004883,
14.744856262207, 15.6628112792969, 12.945686340332, 13.7123504638672,
...
.Dim = c(10L, 140L), .Dimnames = list(NULL,
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", "layer.139", "layer.140")))
For the x-axis, I already have 140 values to be plotted. The x variable is called "RCP1pctCO2cumulative" and is 1-dimensional. Here are the values:
dput(RCP1pctCO2cumulative)
c(0, 0.00697490638121963, 0.0139075990766287, 0.021697436273098,
0.0302329704165459, 0.0389980841428041, 0.0486239280551672,
0.0591636016964912,
0.0698652867227793, 0.0806971430778503, 0.0917696304619312,
0.102874741703272,
0.115288959443569, 0.127942492067814, 0.140930585563183, 0.154951927065849,
0.169483987987041, 0.18332038372755, 0.197460225224495, 0.211912712454796,
0.227608525753021, 0.243575794994831, 0.257900005578995, 0.273098181188107,
0.288984017074108, 0.305822652578354, 0.321933174133301, 0.338437202572823,
0.356079193949699, 0.373604437708855, 0.389580103754997, 0.406976979970932,
0.423514771461487, 0.440741804242134, 0.459019854664803, 0.477243804931641,
0.494844323396683, 0.511731442809105, 0.529583910107613, 0.54879242181778,
0.568029862642288, 0.587030050158501, 0.606201952695847, 0.626413214206696,
0.645859175920486, 0.66550235748291, 0.685490393638611, 0.705517685413361,
0.725719237327576, 0.74520371556282, 0.764879602193832, 0.78538470864296,
0.806885379552841, 0.827258145809174, 0.847640424966812, 0.869502395391464,
0.891108268499374, 0.912525850534439, 0.934271132946014, 0.956432664394379,
0.977902102470398, 0.999574398994446, 1.02313443422317, 1.0449088037014,
1.06765602231026, 1.09229186177254, 1.11614397168159, 1.13850414156914,
1.16062269210815, 1.18386173844337, 1.2078672170639, 1.23217906951904,
1.25505521297455, 1.27949770689011, 1.30394254922867, 1.32788743972778,
1.35124933719635, 1.37474408149719, 1.39838930368423, 1.42129179239273,
1.44566521644592, 1.47075144052506, 1.49397988319397, 1.51616193056107,
1.54051134586334, 1.56606665849686, 1.59053231477737, 1.61592463254929,
1.64096541404724, 1.66613935232162, 1.69049085378647, 1.71510955095291,
1.73991868495941, 1.76491104364395, 1.79114699363708, 1.81589571237564,
1.84101955890656, 1.864857172966, 1.8899533867836, 1.91575677394867,
1.9422261595726, 1.96840007305145, 1.99298502206802, 2.01899757385254,
2.04547454118729, 2.07122528553009, 2.09852977991104, 2.12717348337173,
2.15352830886841, 2.17984690666199, 2.20684263706207, 2.23266286849976,
2.25915194749832, 2.28647100925446, 2.31463184356689, 2.34280925989151,
2.36965861320496, 2.39779574871063, 2.42558825016022, 2.45266799926758,
2.48168079853058, 2.50962221622467, 2.53702754974365, 2.56574511528015,
2.59377150535583, 2.62192821502686, 2.6505597114563, 2.68051118850708,
2.70983340740204, 2.73927145004272, 2.76805067062378, 2.79780313968658,
2.8272668838501, 2.85747616291046, 2.88714888095856, 2.91689431667328,
2.94743349552155, 2.97868416309357, 3.00965495109558, 3.03861720561981
I tried doing something like this to start:
expansion <- expand.grid(103:116, 3:16) #to specify a range of longitudes
and latitudes
lonlat <- extract(RCP1pctCO2Mean, expansion)
plot(RCP1pctCO2cumulative, lonlat, type="l",col="green", lwd="3",
xlab="Cumulative emissions (TtC)", ylab="One-day maximum precipitation
(mm/day)", main="Test model")
But this yields the error:
Error in xy.coords(x, y, xlabel, ylabel, log) :
'x' and 'y' lengths differ
If it helps, here are the dimensions of the new variables created:
dim(lonlat)
[1] 196 140
dim(expansion)
[1] 196 2
What could I be doing wrong (the lengths appear to be the same)? Ultimately, the goal would be to plot the 140 values as a function of emissions (i.e. "RCP1pctCO2Mean" as a function of "RCP1pctCO2cumulative" once each of those selected grid cells are aggregated). The above works absolutely fine when just selecting "one" specific grid cell, but not an aggregate of grid cells. Is this possible to do for an aggregate of grid cells?
Thanks, and any assistance would be greatly appreciated!

Related

radius in nn2() function in RANN r-package

I was trying to use the solution offered here to find all the location from df which are within the 70 km distance from my point of interest userLocation=c(6.9,55.2), but it does not work properly !
df = structure(list(lng = c(6.2694184, 6.25737207, 6.23839104, 6.25844252,
6.22595901, 6.21351832, 6.2010845, 6.1886414, 6.1762058, 6.1637609,
6.15132287, 6.13887619, 6.12643637, 6.14361895, 6.16332364, 6.18302157,
6.2027276, 6.22242688, 6.24213488, 6.26842752, 6.26745135, 6.24518597,
6.26645948, 6.24420242, 6.22357831, 6.26548171, 6.24321746, 6.2226023,
6.20041884, 6.18070459, 6.16099845, 6.16716672, 6.17960629, 6.18686265,
6.2078525, 6.19203657, 6.20447434, 6.21691835, 6.2293537, 6.24179593,
6.26009321, 6.26448764, 6.2422317, 6.21927538, 6.20186455, 6.26350828,
6.24124514, 6.22028969, 6.26251321, 6.2402584, 6.23404584, 6.26153227,
6.22171658, 5.94065657, 6.10363006, 6.11606487, 6.12850589, 6.14093826,
6.15337749, 6.16582359, 6.17826103, 6.19070472, 6.20313974, 6.20009703,
5.96044213, 5.96988333, 5.98023582, 5.98966667, 5.99910246, 6.00003829,
6.00947365, 6.01889843, 6.02832882, 6.01983402, 6.02925771, 6.038687,
6.0481219, 6.05754688, 6.03963788, 6.04906608, 6.05848435, 6.06792377,
6.07735326, 6.08677283, 6.05941948, 6.06885218, 6.07829049, 6.08771889,
6.09713671, 6.10657633, 6.11600538, 6.07922538, 6.08864707, 6.10756108,
6.12000483, 6.13243993, 6.12019786, 6.14488189, 6.15733073, 6.16977091,
6.16621949, 6.13805015, 6.13652024, 5.941545, 6.20491484, 6.18423897,
6.17806466, 6.16355552, 6.15738696, 6.14558294, 6.14286638, 6.13670293,
6.12217027, 6.11601258, 6.10148275, 6.09533146, 6.08080511, 6.07464337,
6.06011984, 6.03729438, 6.05394895, 6.02546329, 6.0136389, 6.03674112,
6.05743408, 6.07812006, 6.09879971, 6.11948795, 6.11063647, 6.08914275,
6.08440881, 6.0018212, 6.02491713, 5.98999461, 6.01308427, 5.97815849,
6.00125809, 5.96632973, 5.98943792, 5.9995124, 6.02119838, 6.04364466,
6.0223476, 6.04560587, 6.03821257, 6.06131821, 6.06046748, 5.97888909,
5.95766873, 6.24771247, 6.04931495, 6.25538943, 6.23227728, 6.25434093,
6.25329159, 6.25225759, 6.25120656, 6.25015469, 6.24911757, 6.06338238,
6.08539205, 6.10756976, 6.12975108, 6.15193667, 6.17411029, 6.19630377,
6.21848591, 6.22602495, 6.23123663, 6.20931486, 6.23019515, 6.20826628,
6.22915282, 6.20721685, 6.22810966, 6.21962063, 6.20209266, 6.20618216,
6.19702482, 6.1799057, 6.15772301, 6.13554395, 6.11336914, 6.09118237,
6.09738412, 6.11958004, 6.12698723, 6.14767387, 6.16835417, 6.18613747,
6.185096, 6.165456, 6.14476821, 6.15765091, 6.23561071, 6.08001353,
6.22353732, 6.2376767, 6.21143885, 6.19936347, 6.18727866, 6.17520066,
6.16311385, 6.15103386, 6.13894506, 6.12686243, 6.11478725, 6.10270261,
6.09818625, 6.12128852, 6.2468456, 6.22571713, 6.24558662, 6.22445138,
6.24434288, 6.22320086, 6.24308194, 6.22194875, 6.24182062, 6.22068065,
6.24057332, 6.21942655, 6.2113264, 6.22341814, 6.19699748, 6.18490568,
6.1988361, 6.17283631, 6.16074252, 6.14867115, 6.13657473, 6.13954049,
6.16263694, 6.18482009, 6.20327221, 6.20009595, 6.19278885, 6.17005571
), lat = c(54.67598304, 54.83924292, 54.83162024, 54.82483795,
54.82033259, 54.80904336, 54.79775292, 54.78646988, 54.77517665,
54.76389082, 54.75260377, 54.74131515, 54.73002531, 54.72096456,
54.71392047, 54.70687309, 54.69983176, 54.69278713, 54.68573957,
54.68934722, 54.7027117, 54.69910571, 54.71606682, 54.71246092,
54.70614626, 54.72943123, 54.72582507, 54.71951053, 54.71576339,
54.72280423, 54.72985112, 54.74274399, 54.75402944, 54.73569581,
54.72983408, 54.7653223, 54.77660496, 54.78789538, 54.79918423,
54.81047187, 54.80230996, 54.74279524, 54.73918917, 54.74155047,
54.75043676, 54.75615956, 54.75255324, 54.75849353, 54.76951451,
54.76590829, 54.77879358, 54.78287875, 54.84106585, 54.79004116,
54.73264696, 54.7439301, 54.755221, 54.76651031, 54.77779842,
54.78908531, 54.80037062, 54.81166369, 54.82295519, 54.83631649,
54.78306731, 54.79535153, 54.77609951, 54.7883729, 54.80065457,
54.76912877, 54.78140068, 54.7936805, 54.80595963, 54.7621547,
54.77443373, 54.78671208, 54.79898973, 54.81126633, 54.75518666,
54.76746422, 54.77974071, 54.7920169, 54.80429202, 54.81656608,
54.74821493, 54.76049101, 54.7727664, 54.78504073, 54.79732298,
54.80959594, 54.82187682, 54.74124062, 54.75351485, 54.76118719,
54.77247897, 54.78376916, 54.79513973, 54.79505815, 54.80634591,
54.8176321, 54.8309898, 54.82587825, 54.81251828, 54.80340625,
54.85043439, 54.85669012, 54.84379843, 54.8629602, 54.85006754,
54.83850747, 54.86921769, 54.85633303, 54.87548056, 54.86259492,
54.88174916, 54.86885358, 54.88800555, 54.8751176, 54.89427628,
54.89688611, 54.88137801, 54.88534048, 54.87379377, 54.87235508,
54.86608859, 54.85982748, 54.85356275, 54.84730376, 54.83491117,
54.82992904, 54.84309445, 54.86224596, 54.86080945, 54.85069668,
54.84926234, 54.8391549, 54.83771415, 54.82760306, 54.82617384,
54.81401612, 54.81866848, 54.82193287, 54.83203, 54.85454492,
54.8418856, 54.84463218, 54.83126946, 54.807705, 54.81314447,
54.95082492, 54.90870481, 54.85261135, 54.8544958, 54.86597391,
54.87933643, 54.89269028, 54.90605271, 54.91941509, 54.93277779,
54.91930405, 54.92337651, 54.92712528, 54.93087901, 54.93462874,
54.93838307, 54.94213376, 54.94588009, 54.93325056, 54.86785843,
54.86365241, 54.88122101, 54.87701472, 54.89458354, 54.890377,
54.90794604, 54.9204011, 54.92916856, 54.90373959, 54.91606989,
54.92541899, 54.92166542, 54.91791682, 54.91416421, 54.91041621,
54.89753741, 54.90128459, 54.88863077, 54.8823668, 54.87609923,
54.88468492, 54.89804726, 54.89094652, 54.89720452, 54.90830415,
55.08370977, 54.93641839, 55.07226944, 55.06170442, 55.06083624,
55.04939327, 55.03795778, 55.02652115, 55.01508302, 55.00364374,
54.99220296, 54.98077001, 54.96932695, 54.95789135, 54.94469353,
54.94333719, 54.96418243, 54.9585686, 54.97754901, 54.9719349,
54.99090692, 54.98529252, 55.00427341, 54.99865908, 55.01763087,
55.01201625, 55.03099761, 55.02538271, 55.03790915, 55.04934232,
55.02208092, 55.01064489, 54.99998971, 54.99920808, 54.98776941,
54.97632995, 54.9648976, 54.95153594, 54.95007267, 54.95382515,
54.96186591, 54.98662348, 54.97394968, 54.97118391)), class = "data.frame", row.names = c(NA,
-238L))
What I have done is as follow :
Add the point of interest to the beginning of df
df = rbind(userLocation,df)
Set the radius to 0.64 since according to here, every 0.1 is equivalent to 11.1 km !
radius <- 0.64
#Identifying neighbors
res <- nn2(df, k=nrow(df), searchtype="radius", radius = radius)
Since my point of interest is the first row in df I would expect all the non zero index in the first row are the points within my 70 km threshold
Ind <- res$nn.idx[1,][res$nn.idx[1,]>0]
My Ind object has just one value!
Ind
[1] 1
but if I plot the data, all of the points are within 70 km distance :
I would appreciate it if someone could help me here.

Change SpatialLinesDataFrame coordinates to fit into a shapefile

I have a SpatialLinesDataFrame of streets in Malawi and Shapefile of Malawi country. The problem is that the coordinates of some parts of one of the streets are located in the Malawi lake (and it's clearly not possible in the reality), so I think there are some problems between the shapefile and the SpatialLinesDataFrame. How can I correct the coordinates of the SpatialLinesDataFrame so that they can overlap correctly? The shapefile I am using for Malawi can be found here : mwi_adm_nso_20181016_SHP.zip (the name of the file I am using is mwi_admbnda_adm3_nso_20181016.shp).
Here the code to create the SpatialLinesDataframe of the street that has segments in the lake.
# download the shapefile of Malawi
mySHPM <-
shapefile(
paste(
path,
"/mwi_admbnda_adm3_nso_20181016.shp",
sep = ""
)
)
# subset to focus on the area where the street is
mySHP <- mySHPM[mySHPM$ADM2_EN %in% c("Karonga", "Rumphi"), ]
# define the SpatialLinesDataframe for the street that goes in the lake
Xline <- c(34.21031,34.21031,34.21027,34.21026,34.21023,34.21022,34.21006,34.20951,34.20848,34.20763,
34.20712,34.20624,34.20524,34.20443,34.20412,34.20409,34.20409,34.20421,34.20418,34.20414,
34.20403,34.20437,34.20503,34.20548,34.20598,34.20625,34.20589,34.20493,34.20467,34.20449,
34.20376,34.20349,34.20377,34.20444,34.20598,34.20694,34.20912,34.20978,34.20999,34.20985,
34.20950,34.20864,34.20822,34.20820,34.20838,34.20890,34.20905,34.20876,34.20798,34.20697,
34.20599,34.20399,34.20219,34.20074,34.19889,34.19631,34.19428,34.19126,34.18883,34.18757,
34.18614,34.18343,34.18179,34.17895,34.17602,34.17429,34.17208,34.17101,34.16972,34.16955,
34.16949,34.16944,34.16954,34.16986,34.16973,34.16935,34.16908,34.16896,34.16874,34.16783,
34.16684,34.16574,34.16509,34.16471,34.16463,34.16445,34.16422,34.16420,34.16399,34.16386,
34.16358,34.16319,34.16275,34.16217,34.16153,34.16145,34.16154,34.16207,34.16210,34.16196,
34.16145,34.16095,34.16044,34.15993,34.15940,34.15889,34.15835,34.15780,34.15720,34.15652,
34.15592,34.15546,34.15524,34.15497,34.15463,34.15426,34.15387,34.15335,34.15306,34.15270,
34.15222,34.15185,34.15141,34.15107,34.15080,34.15071,34.15080,34.15120,34.15137,34.15167,
34.15143,34.15091,34.15038,34.14988,34.14946,34.14922,34.14899,34.14851,34.14815,34.14800,
34.14762,34.14732,34.14697,34.14715,34.14691,34.14640,34.14608,34.14568,34.14541,34.14543,
34.14590,34.14617,34.14634,34.14649,34.14670,34.14647,34.14623,34.14614,34.14628,34.14649,
34.14633,34.14608,34.14591,34.14556,34.14515,34.14494,34.14472,34.14444,34.14414,34.14378,
34.14354,34.14322,34.14309,34.14320,34.14355,34.14380,34.14419,34.14451,34.14485,34.14522,
34.14538,34.14530,34.14525,34.14522,34.14522,34.14520,34.14521,34.14487,34.14455,34.14419,
34.14378,34.14341,34.14304,34.14306,34.14355,34.14411,34.14461,34.14485,34.14480,34.14468,
34.14448,34.14448,34.14438,34.14445,34.14449,34.14443,34.14419,34.14395,34.14341,34.14293,
34.14259,34.14268,34.14319,34.14367,34.14371,34.14401,34.14402,34.14379,34.14326,34.14281,
34.14225,34.14182,34.14130,34.14082,34.14021,34.14002,34.13975,34.13975,34.14023,34.14068,
34.14111,34.14147,34.14145,34.14144,34.14097,34.14035,34.13982,34.13912,34.13863,34.13819,
34.13769,34.13761,34.13751,34.13746,34.13699,34.13661,34.13670,34.13716,34.13736,34.13718,
34.13663,34.13655,34.13655,34.13662,34.13683,34.13707,34.13728,34.13741,34.13740,34.13761,
34.13824,34.13843,34.13842,34.13796,34.13723,34.13716,34.13718,34.13709,34.13689,34.13702,
34.13744,34.13761,34.13754,34.13755,34.13722,34.13682,34.13665,34.13634,34.13631,34.13623,
34.13625,34.13639,34.13652,34.13656,34.13695,34.13717,34.13716,34.13694,34.13683,34.13632,
34.13584,34.13532,34.13487,34.13437,34.13384,34.13348,34.13303,34.13258,34.13235,34.13206,
34.13204,34.13173,34.13134,34.13130,34.13102,34.13098,34.13089,34.13066,34.13052,34.13049,
34.13005,34.13001,34.12955,34.12952,34.12938,34.12859,34.12828,34.12784,34.12704,34.12610,
34.12483,34.12405,34.12284,34.12198,34.12105,34.11939,34.11927,34.11853,34.11828,34.11751,
34.11658,34.11550,34.11403,34.11364,34.11339,34.11303,34.11240,34.11173,34.11087,34.11034,
34.10987,34.10982,34.11021,34.11024,34.11016,34.10976,34.10923,34.10895,34.10898,34.10904,
34.10887,34.10870,34.10847,34.10803,34.10774,34.10691,34.10652,34.10620,34.10544,34.10540,
34.10547,34.10630,34.10641,34.10682,34.10677,34.10665,34.10664,34.10727,34.10819,34.10943,
34.11040,34.11054,34.11062,34.11087,34.11081,34.11079,34.11079)
Yline <-
c(-10.45258, -10.45259, -10.45294, -10.45369, -10.45489, -10.45648, -10.45859, -10.46091, -10.46452, -10.46747,
-10.46972, -10.47282, -10.47625, -10.47942, -10.48181, -10.48305, -10.48489, -10.48688, -10.49033, -10.49375,
-10.49606, -10.49860, -10.50286, -10.50589, -10.50926, -10.51182, -10.51374, -10.51642, -10.51806, -10.52037,
-10.52250, -10.52405, -10.52527, -10.52634, -10.52831, -10.52905, -10.53041, -10.53148, -10.53272, -10.53478,
-10.53576, -10.53710, -10.53811, -10.53825, -10.53968, -10.54224, -10.54315, -10.54454, -10.54563, -10.54692,
-10.54783, -10.54895, -10.54994, -10.55071, -10.55176, -10.55325, -10.55491, -10.55747, -10.55951, -10.56054,
-10.56163, -10.56337, -10.56472, -10.56799, -10.57132, -10.57269, -10.57448, -10.57609, -10.57886, -10.58046,
-10.58276, -10.58438, -10.58613, -10.58850, -10.59053, -10.59210, -10.59307, -10.59355, -10.59357, -10.59333,
-10.59312, -10.59287, -10.59279, -10.59252, -10.59238, -10.59253, -10.59336, -10.59393, -10.59408, -10.59396,
-10.59353, -10.59313, -10.59288, -10.59271, -10.59255, -10.59260, -10.59274, -10.59306, -10.59316, -10.59317,
-10.59296, -10.59305, -10.59316, -10.59342, -10.59353, -10.59358, -10.59341, -10.59330, -10.59337, -10.59367,
-10.59402, -10.59435, -10.59463, -10.59494, -10.59532, -10.59560, -10.59559, -10.59548, -10.59519, -10.59497,
-10.59484, -10.59461, -10.59478, -10.59503, -10.59486, -10.59503, -10.59509, -10.59530, -10.59564, -10.59584,
-10.59583, -10.59578, -10.59600, -10.59620, -10.59639, -10.59614, -10.59587, -10.59588, -10.59584, -10.59549,
-10.59499, -10.59452, -10.59414, -10.59381, -10.59365, -10.59368, -10.59346, -10.59331, -10.59317, -10.59330,
-10.59354, -10.59390, -10.59430, -10.59461, -10.59489, -10.59503, -10.59519, -10.59505, -10.59496, -10.59479,
-10.59466, -10.59449, -10.59425, -10.59387, -10.59365, -10.59346, -10.59352, -10.59341, -10.59355, -10.59355,
-10.59316, -10.59312, -10.59314, -10.59323, -10.59332, -10.59374, -10.59384, -10.59372, -10.59391, -10.59422,
-10.59455, -10.59488, -10.59493, -10.59490, -10.59490, -10.59477, -10.59437, -10.59416, -10.59399, -10.59421,
-10.59427, -10.59405, -10.59378, -10.59391, -10.59426, -10.59459, -10.59435, -10.59452, -10.59462, -10.59500,
-10.59540, -10.59564, -10.59566, -10.59519, -10.59477, -10.59469, -10.59481, -10.59492, -10.59478, -10.59458,
-10.59454, -10.59468, -10.59489, -10.59522, -10.59558, -10.59608, -10.59612, -10.59612, -10.59556, -10.59541,
-10.59550, -10.59559, -10.59540, -10.59518, -10.59522, -10.59529, -10.59534, -10.59547, -10.59551, -10.59564,
-10.59592, -10.59610, -10.59623, -10.59621, -10.59598, -10.59587, -10.59586, -10.59580, -10.59568, -10.59572,
-10.59554, -10.59546, -10.59492, -10.59453, -10.59403, -10.59389, -10.59357, -10.59333, -10.59308, -10.59286,
-10.59283, -10.59279, -10.59239, -10.59197, -10.59184, -10.59131, -10.59120, -10.59101, -10.59062, -10.59048,
-10.59021, -10.59022, -10.59011, -10.59019, -10.59054, -10.59093, -10.59098, -10.59106, -10.59102, -10.59058,
-10.59023, -10.59009, -10.59001, -10.59002, -10.59028, -10.59057, -10.59101, -10.59129, -10.59150, -10.59152,
-10.59151, -10.59108, -10.59072, -10.59039, -10.59010, -10.58976, -10.58972, -10.58904, -10.58882, -10.58845,
-10.58838, -10.58833, -10.58859, -10.58851, -10.58845, -10.58817, -10.58790, -10.58771, -10.58768, -10.58799,
-10.58799, -10.58738, -10.58720, -10.58723, -10.58757, -10.58774, -10.58812, -10.58875, -10.58917, -10.58920,
-10.58930, -10.58927, -10.58915, -10.58919, -10.58933, -10.58968, -10.58981, -10.58970, -10.58913, -10.58871,
-10.58863, -10.58875, -10.58869, -10.58857, -10.58840, -10.58776, -10.58771, -10.58709, -10.58688, -10.58693,
-10.58698, -10.58680, -10.58643, -10.58626, -10.58621, -10.58628, -10.58687, -10.58748, -10.58799, -10.58860,
-10.58908, -10.58929, -10.59015, -10.59142, -10.59176, -10.59204, -10.59191, -10.59216, -10.59234, -10.59253,
-10.59294, -10.59297, -10.59298, -10.59285, -10.59288, -10.59313, -10.59308, -10.59325, -10.59455, -10.59463,
-10.59521, -10.59622, -10.59643, -10.59773, -10.59809, -10.59836, -10.59890, -10.60086, -10.60199, -10.60337,
-10.60447, -10.60461, -10.60458, -10.60432, -10.60430, -10.60432, -10.60433)
coords <- as.data.frame(cbind(x = Xline, y = Yline))
Slo1 <- Line(coords)
Sli1 <- Lines(list(Slo1),ID="ID")
HMline <- SpatialLines(list(Sli1))
class(HMline)
proj4string(HMline) <- proj4string(mySHP)
plot(mySHP)
plot(HMline, add = T, col = "red")
# you can notice a small segment going outside the shapefile. if you can't, look at this:
# crop the street to save in "cropped" just the segments that are not overlapping the shapefile
cropped <- gDifference(HMline, mySHP)
plot(HMline)
plot(cropped, add = T, col = "red", lwd = 4)
Thanks for your help!

Problem with correlog function in pgirness

I am trying to produce an autocorrelogram using output from the correlog function in pgirmess as is described here.
My UTM coordinate data are: structure(list(V1 = c(698073L, 698095L, 698274L, 697806L, 698602L,
698632L, 697425L, 698272L, 698125L, 697935L, 698681L, 699287L,
698687L, 698042L, 698052L, 697477L, 698096L, 698782L, 698203L,
698113L, 698046L, 697923L, 699398L, 698143L, 698555L, 697973L,
698042L, 698080L, 697918L, 698253L, 698687L, 698719L, 698079L,
697982L, 698273L, 698995L, 698267L, 700678L, 698087L, 698887L,
698599L, 698883L, 697947L, 698159L, 697464L, 697897L, 698867L,
697775L, 698071L, 698043L, 698115L, 697979L, 697976L, 698064L,
698024L, 698048L, 698200L, 698092L, 698328L, 697998L, 697772L,
697957L, 698590L, 698789L, 698060L, 698071L, 699316L, 699094L,
698633L, 699289L, 698089L, 697819L, 697894L, 698000L, 700218L,
700004L, 699684L, 699743L, 726256L, 724864L, 725175L, 725438L,
725778L, 700386L, 700740L, 700701L, 724204L, 724049L, 723872L,
722994L, 698150L, 698197L, 698064L, 698285L, 698685L, 698716L,
698316L, 698170L, 698920L, 699335L, 698273L, 698100L, 698605L,
698089L, 697784L, 697693L, 697990L, 697809L, 697945L, 698108L,
697945L, 697736L, 698567L, 699035L, 697651L, 698062L, 699035L
), V2 = c(1853363L, 1853581L, 1853596L, 1854098L, 1853795L, 1853764L,
1853338L, 1853684L, 1853627L, 1853540L, 1853912L, 1853399L, 1853897L,
1853236L, 1853744L, 1853505L, 1853458L, 1853926L, 1853736L, 1853858L,
1853783L, 1853586L, 1853612L, 1853498L, 1853834L, 1853269L, 1853528L,
1853639L, 1853533L, 1853503L, 1853897L, 1853704L, 1853685L, 1853638L,
1853595L, 1853602L, 1853530L, 1852898L, 1853424L, 1853770L, 1853792L,
1853859L, 1853586L, 1853708L, 1853603L, 1852869L, 1853973L, 1853898L,
1853608L, 1853762L, 1853695L, 1853586L, 1853617L, 1853775L, 1853251L,
1853162L, 1853693L, 1853603L, 1853654L, 1853463L, 1853532L, 1853392L,
1853853L, 1853843L, 1853550L, 1853587L, 1853509L, 1853461L, 1853724L,
1853506L, 1853535L, 1853643L, 1853807L, 1853556L, 1852417L, 1852815L,
1853113L, 1852880L, 1785260L, 1786479L, 1786054L, 1785839L, 1785467L,
1852514L, 1852029L, 1852391L, 1787745L, 1787746L, 1787926L, 1788326L,
1853416L, 1853437L, 1853409L, 1853531L, 1853848L, 1853734L, 1853660L,
1853465L, 1854071L, 1853365L, 1853577L, 1853698L, 1853878L, 1853286L,
1853532L, 1853503L, 1853423L, 1854024L, 1854096L, 1853483L, 1853798L,
1853664L, 1853804L, 1852806L, 1853503L, 1853304L, 1852806L)), class = "data.frame", row.names = c(NA,
-117L))
My response variable data are: c(44.172, 43.975, 43.338, 43.854, 41.475, 42.414, 45.035, 44.025,
43.278, 44.116, 44.606, 44.242, 45.682, 44.495, 42.732, 44.02,
44.626, 43.535, 44.025, 43.409, 43.207, 43.46, 42.505, 45.348,
42.495, 44.505, 43.636, 42.879, 42.086, 43.364, 43.667, 43.005,
43.939, 44.813, 45.364, 42.475, 43.768, 42.909, 43.535, 44.949,
43.187, 42.5, 43.495, 43.318, 42.561, 43.747, 42.005, 44.293,
44.808, 43.702, 42.677, 44.566, 45.237, 43.859, 44.237, 43.909,
44.596, 44.667, 44.04, 44, 42.192, 44.929, 42.949, 43.944, 43.53,
43.298, 43.025, 43.424, 43.712, 42.51, 44.152, 44.01, 41.833,
43.505, 44.646, 44.566, 42.929, 43.636, 44.54, 43.232, 43.182,
42.217, 43.904, 42.864, 43.475, 42.323, 43.025, 43.429, 43.298,
44.914, 42.884, 43.601, 44.475, 42.505, 41.268, 44.227, 42.955,
42.833, 46.056, 44.293, 42.556, 43.717, 43.283, 43.086, 42.707,
43.859, 42.884, 44.682, 41.551, 42.803, 45.242, 42.929, 42.848,
44.288, 43.283, 43.288, 44.444)
I tried:
pgi.cor <- correlog(coords=coords, z=sp.rich$mean], method="Moran",nbclass=11), but this appears to have cause problem calculating distances between the duplicate coordinates:
Error in dnearneigh(coords[-zero, ], breaks[i, 1], breaks[i, 2]) : non-positive number of rows in x
Therefore, I tried: pgi.cor <- correlog(coords=unique(coords), z=sp.rich$mean[-c(31,117)], method="Moran",nbclass=11) (because rows 31 and 117 are duplicates of other rows). However this gives me the same error result. Any ideas? Thanks!

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"))

Spatial analysis with R package spatstat, sidebar does not show correct values

I'm trying to create a map with the spatstat package of R so that the sidebar shows the values of the third (preferably) or fourth column of my data frame and that the colors are also reflective of that third (or fourth column) chosen.
My script:
x=c(6.839887, 6.671494, 6.651083, 6.655289, 6.591903, 6.653641, 6.661709, 6.671664, 6.660044, 6.624659, 6.648162, 6.536877, 6.654134, 6.674678,6.618935, 6.677705, 6.643918, 6.644119, 6.670517, 6.583619, 6.649991, 6.647649, 6.656308, 6.645772, 6.648740, 6.643103, 6.652199, 6.666641,6.633400, 6.621282, 6.635427, 6.646127, 6.630862, 6.657919, 6.671616, 6.622935, 6.648225, 6.676911, 6.640234, 6.719334, 6.653202, 6.656747,6.724692, 6.639747, 6.630575, 6.657916, 6.618957, 6.640006, 6.645280, 6.614058, 6.576136, 6.631994, 6.617391, 6.782351, 6.620072, 6.661061,6.597216, 6.648755, 6.618436, 6.659507, 6.653993, 6.663255, 6.630893, 6.656322, 6.617265, 6.649022, 6.629346, 6.595224, 6.540263, 6.623435,6.652709, 6.608565, 6.618335, 6.645100, 6.790914, 6.643620, 6.462808, 6.680115, 6.716004, 6.668781, 6.765199, 6.674251, 6.647542, 6.724564,6.724556)
y=c(17.16749, 17.16727, 17.16678, 17.16673, 17.16813, 17.16663, 17.16652, 17.16636, 17.16629, 17.16856, 17.16521, 17.16519, 17.17002, 17.16465,17.17015, 17.16407, 17.16356, 17.17122, 17.16334, 17.17152, 17.16282, 17.16278, 17.16272, 17.17257, 17.16198, 17.17279, 17.16169, 17.16161,17.16146, 17.17352, 17.17389, 17.16076, 17.17420, 17.16046, 17.15917, 17.17571, 17.15895, 17.15881, 17.15860, 17.15827, 17.15797, 17.15776,17.17761, 17.15664, 17.15622, 17.15610, 17.15571, 17.15561, 17.15527,17.15514, 17.15494, 17.15447, 17.15438, 17.18041, 17.18053, 17.15402,17.18090, 17.15384, 17.18121, 17.15355, 17.15352, 17.15349, 17.18213,17.15242, 17.15201, 17.14978, 17.18591, 17.18688, 17.18707, 17.18761,17.14712, 17.18788, 17.18794, 17.14619, 17.18868, 17.14588, 17.14511,17.14471, 17.14440, 17.14430, 17.19116, 17.19140, 17.14222, 17.14123,17.33627)
z=c(32.23228,526.46061, -1300.03539, -376.04329, 139.67322,-913.24800, -526.46061, 354.55511, 483.48424, 161.16141, 182.64960, 419.0196, 75.20866, -225.62598, -1536.40546, -397.53148, -1106.64169, -440.50786, 118.18504,-290.09054, -1471.94089, 440.50786,-848.78343, -1385.98814, -676.87793, -1622.35821, -1450.45271,75.20866, -1557.89365, 161.16141, 376.04329, 354.55511, -32.23228,-1171.10626,-75.20866, 547.94880, -805.80706, 870.27162, -698.36612,-32.23228, -2331.46842, -182.64960, 75.20866, -719.85431,-1837.24009,913.24800, -1106.64169, 698.36612, 483.48424, -676.87793, -3019.09045, 891.75981, 1106.64169, 333.06692, -913.24800,333.06692, 934.73619, 354.55511, 75.20866, -891.75981, -247.11416, -1966.16922, 139.67322, -784.31887, -569.43699, -118.18504,-440.50786, 397.53148, -655.38974, 139.67322, 53.72047, -633.90155,-633.90155, 419.01967, -547.94880, 75.20866, 569.43699, 290.09054, -376.04329, 547.94880, 75.20866, -10.74409, 182.64960,-397.53148, -479.53833 )
w=c(96326.91, 96769.46, 95127.94, 95960.41, 96423.22, 95476.93, 95825.18,96615.67, 96731.03, 96442.47, 96461.73, 96673.36, 96365.44, 96095.53,94914.31, 95941.10, 95302.53, 95902.47, 96403.96, 96037.64, 94972.60,96692.58, 95535.03, 95050.29, 95689.84, 94836.56, 94992.03, 96365.44,94894.87, 96442.47, 96634.90, 96615.67, 96269.09, 95244.36, 96230.54,96788.68, 95573.74, 97076.62, 95670.50, 96269.09, 94193.69, 96134.12,96365.44, 95651.15, 94642.01, 97114.98, 95302.53, 96923.12, 96731.03,95689.84, 93567.91, 97095.80, 97287.46, 96596.43, 95476.93, 96596.43,97134.15, 96615.67, 96365.44, 95496.30, 96076.24, 94525.17, 96423.22,95593.10, 95786.52, 96191.98, 95902.47, 96654.13, 95709.18, 96423.22,96346.17, 95728.52, 95728.52, 96673.36, 95805.85, 96365.44, 96807.89,96557.96, 95960.41, 96788.68, 96365.44, 96288.37, 96461.73,95941.10, 99451.20)
shap.lo=data.frame(x,y,z,w)
library(spatstat)
shap.lo.win <- owin(range(shap.lo[,1]), range(shap.lo[,2]))
centroid.owin(shap.lo.win) ; area.owin(shap.lo.win)
shap.lo.ppp <- as.ppp(shap.lo[,c(1,2,3)], shap.lo.win) # making a ppp object
plot(density(shap.lo.ppp,0.02), col=topo.colors(25), main='', xlab='x',
ylab='y')
points(x, y)
the result is shown below
I would like to know why the sidebar shows different values than the ones shown in the third column of my data frame, that is, in addition to displaying no negative values, shows values much larger than those contained in the third column.
Is it possible to do this, that is, make the colors and the sidebar represent the third or fourth column of the data frame?
I thank the help of all you!
Let me quote directly from the help file for density.ppp:
This function is often misunderstood.
The result of density.ppp is not a spatial smoothing of the
marks or weights attached to the point pattern. To perform
spatial interpolation of values that were observed at the points
of a point pattern, use Smooth.ppp.
The result of density.ppp is not a probability density. It is
an estimate of the intensity function of the point process that
generated the point pattern data. Intensity is the expected
number of random points per unit area. The units of intensity are
“points per unit area”. Intensity is usually a function of
spatial location, and it is this function which is estimated by
density.ppp. The integral of the intensity function over a
spatial region gives the expected number of points falling in this
region.
So try Smooth.ppp (note the upper case S), and see if you can make that produce the results you expected.

Resources