5

So I have a map that is plotting air pressure in Catalonia:

enter image description here

Here's a close up:

enter image description here

I would now like to select all observations with air pressure above 97 kPa (dark blue) and create a new data frame with them for further analysis. Here's the tricky bit, I want to select observations that meet the altitude filter AAAAND are along the Mediterranean coast. Most observations above 97 kPa are along the Mediterranean coast but there are some outliers that are inland.

In the end, I guess some sort of diagonal latitude, longitude filtering must occur but I don't know how to specify it. Is there some sort of way to lace, or draw a polygon on data and then have it select everything inside?

This is my data:

structure(list(final.Latitude = c(42.161626, 41.516819, 41.786856, 
41.748215, 42.393932, 41.767667, 41.938401, 41.494079, 41.752819, 
41.422327, 41.253914, 42.104854, 40.544337, 41.647625, 41.26267, 
40.745573, 40.881084, 41.201499, 41.494183, 40.873663, 41.211076, 
41.814818, 41.737032, 42.434746, 41.796036, 41.197585, 42.119308, 
41.665698, 41.141899, 40.883885, 40.814408, 40.710754, 41.65649, 
41.541525, 41.581905, 41.61424, 42.215454, 41.137955, 41.553355, 
42.17195, 40.909931, 42.757417, 41.89469, 41.49472, 41.447145, 
41.791172, 41.862813, 41.677615, 41.398371, 41.094337, 42.83454, 
41.331905, 41.954854, 41.560246, 41.758456, 41.516953, 41.625954, 
40.974225, 42.098215, 42.238615, 41.841862, 41.280658, 41.491805, 
41.675766, 42.416667, 41.220308, 42.043361, 41.009161, 41.136268, 
41.747716, 41.816881, 41.851497, 42.306365, 41.525909, 41.732976, 
41.187937, 41.788901, 41.862027, 41.874859, 41.074787, 41.724519, 
42.313455, 42.838364, 41.590543, 42.422663, 41.530049, 42.374163, 
41.639757, 41.914026, 41.531976, 42.282191, 41.474805, 41.760742, 
41.498525, 41.277658, 41.648019, 41.175305, 41.672663, 42.269094, 
41.561134, 41.467288, 42.403712, 41.100187, 41.82609, 41.516667, 
41.636616, 41.417024, 41.688442, 41.165596, 41.208101, 41.850617, 
41.197377, 42.367024, 42.361421, 41.594469, 42.391185, 42.578381, 
41.868737, 41.145762, 42.496539, 41.161892, 41.231334, 41.510179, 
41.634688, 41.763027, 41.929978, 40.671331, 41.713584, 41.462664, 
41.692433, 41.053748, 41.136997, 41.207852, 41.683047, 42.344326, 
41.4404, 42.411528, 41.041842, 41.984444, 42.034332, 40.655163, 
41.355913, 41.727408, 41.607816, 41.193202, 41.731762, 42.028479, 
41.102165, 41.955568, 41.360861, 41.743718, 41.580977, 41.852114, 
42.073092, 41.601927, 41.315489, 41.548474, 42.810764, 41.709801, 
42.371338, 41.47145, 42.621379, 41.826875, 41.374287, 41.616667, 
42.296039, 42.074764, 41.699929, 41.646217, 41.885617, 42.002284, 
41.726337, 41.284957, 41.47402, 40.734082, 40.716001, 41.480477, 
41.18225, 41.503525, 41.126212, 42.233618, 41.598858, 41.477979, 
41.605161, 41.039296, 42.360065, 41.759637, 41.846688, 42.109172, 
41.265454, 41.468488, 42.017482, 41.625876, 41.759101, 41.191922, 
41.699429, 41.751713, 42.22352, 41.248929, 42.271593, 41.354354, 
41.543718, 42.068952, 41.87566, 42.010081, 41.316667, 41.985403, 
42.181691, 42.065168, 41.501108, 42.218238, 41.873141, 41.946288, 
41.666667, 41.917381, 41.175447, 41.971022, 41.437188, 42.316667, 
42.042256, 41.191613, 42.057709, 41.522319, 41.42334, 41.678745, 
41.554229, 41.186914, 42.249185, 41.234832, 42.315525, 42.408303, 
41.477337, 41.413134, 41.915639, 41.188829, 41.740291, 41.309827, 
41.327836, 41.051187, 41.607475, 41.794363, 42.380192, 41.278987, 
41.717096, 41.940471, 41.422931, 41.728017, 41.002665, 41.224484, 
41.155913, 42.433333, 42.20033, 41.496859, 42.188615, 41.168999, 
41.894646, 41.138704, 42.227089, 41.466667, 41.224079, 41.54329, 
42.228907, 41.826019, 41.076036, 41.973982, 41.762849, 42.233975, 
41.744147, 42.212313, 41.383333, 41.26126, 41.45356, 41.533333, 
41.694361, 41.253569, 42.145754, 42.13269, 42.323734, 41.838472, 
41.808423, 41.237045, 41.257745, 42.035974, 41.456014, 41.99404, 
42.112564, 42.413192, 41.53343, 41.384189, 41.556626, 41.977765, 
41.750928, 41.737402, 42.184708, 41.874234, 41.116667, 41.995717, 
41.561111, 41.482015, 41.967881, 40.90787, 41.849001, 41.699143, 
41.394159, 41.701173, 42.422255, 41.125037, 41.146003, 41.673185, 
41.952427, 41.533986, 40.812492, 42.166362, 41.526426, 40.597668, 
41.322164, 42.036402, 41.880549, 42.20347, 41.379576, 41.752114, 
41.648215, 41.386759, 41.243112, 41.286117, 41.220468, 42.061813, 
41.929335, 41.247787, 42.283618, 41.957459, 42.137402, 41.316881, 
42.138901, 41.847644, 42.246931, 42.475945, 42.329978, 42.196145, 
42.217381, 42.375733, 42.254854, 41.613785, 41.348501, 41.947002, 
41.228003, 41.504069, 41.225245, 42.105996, 41.383666, 41.428805, 
41.184644, 42.393307, 42.105944, 42.369698, 42.077865, 42.330101, 
42.592736, 42.495852, 42.593676, 42.324487, 42.350054, 42.016913, 
42.173822, 42.373156, 42.198189, 42.254595, 42.372036, 42.286585, 
42.246175, 42.737724, 42.785504, 42.742824, 42.747696, 42.104657, 
42.212158, 42.21305, 42.324369, 42.36003), final.Longitude = c(1.092285, 
1.901004, 1.096829, 1.629193, 2.846645, 2.250857, 2.76217, 0.458438, 
0.659356, 0.937106, 1.093504, 3.085332, 0.48089, 0.693825, 1.170103, 
0.618666, 0.498917, 1.046086, 2.294718, 0.400167, 0.975421, 0.636328, 
0.584498, 0.627843, 0.568341, 1.11167, 2.101356, 0.555637, 1.375338, 
0.801686, 0.714752, 0.578086, 1.082864, 0.924567, 2.549358, 2.539721, 
2.641934, 0.908485, 2.401142, 3.074909, 0.260671, 0.71233, 1.045118, 
0.672773, 2.244789, 0.810942, 1.874909, 1.016459, 2.1741, 0.311004, 
0.716868, 1.922554, 3.207173, 1.016022, 0.904336, 1.433333, 1.011437, 
0.517667, 1.844004, 2.708886, 1.358886, 0.722351, 1.348755, 2.789078, 
1.883333, 1.439364, 2.910885, 0.383916, 0.989194, 2.558458, 2.305603, 
1.215786, 2.967557, 2.368952, 1.513526, 1.569665, 1.986723, 3.074338, 
0.878136, 1.052436, 2.464097, 2.36524, 0.738999, 2.581156, 2.926337, 
1.686508, 2.920236, 2.357387, 1.680977, 1.620736, 2.016739, 1.979945, 
2.161279, 0.765658, 1.968755, 0.970925, 1.324377, 1.271554, 2.848572, 
1.13935, 0.689467, 3.150642, 0.886446, 2.175124, 1.3, 1.518095, 
1.919698, 0.695244, 1.444914, 1.672893, 0.959004, 1.635581, 2.833618, 
1.870308, 2.405817, 2.999929, 1.086656, 2.111956, 0.81979, 1.272164, 
0.473006, 0.550333, 0.920149, 0.873428, 1.668666, 2.811885, 0.519665, 
1.01746, 1.024914, 2.113455, 0.438496, 0.649917, 1.247073, 2.28626, 
3.064954, 1.860957, 1.844289, 0.632897, 2.821111, 1.880192, 0.468583, 
0.664668, 2.059243, 2.289721, 0.777024, 2.503212, 3.10289, 0.752582, 
2.233086, 2.110014, 2.628836, 1.6172, 0.586083, 3.010814, 1.547502, 
1.453319, 0.824509, 0.710496, 0.902696, 1.458522, 1.532405, 1.248929, 
2.893647, 0.549688, 0.633333, 2.912027, 0.916209, 2.845646, 2.741505, 
2.232976, 2.28476, 1.825837, 0.75331, 1.93062, 0.37308, 0.530085, 
2.316382, 1.045336, 1.811358, 0.732084, 1.776623, 2.02648, 1.612224, 
0.879873, 0.596649, 3.000214, 2.01358, 1.041892, 2.208315, 1.365168, 
2.280459, 1.735019, 1.415025, 2.395075, 1.208506, 1.976123, 1.903569, 
2.860457, 1.294718, 2.231477, 1.849072, 1.894075, 1.313526, 1.17631, 
1.564897, 1.816667, 2.094575, 2.48476, 1.900428, 0.959546, 2.907316, 
0.720168, 2.556673, 2.75, 3.163098, 1.269737, 3.148144, 2.009243, 
3.116667, 2.10703, 1.255497, 1.267667, 1.750761, 1.203614, 0.855824, 
1.677122, 1.209089, 0.967787, 0.845336, 2.921769, 0.741027, 1.387723, 
1.514579, 1.185612, 0.85608, 0.638549, 0.988663, 2.094718, 0.871539, 
1.038313, 1.050881, 1.73626, 1.297127, 1.420879, 2.840791, 1.396841, 
1.70621, 0.599667, 1.310832, 1.107423, 1.133333, 2.190467, 2.157387, 
2.407067, 0.976254, 2.803533, 1.051562, 3.042647, 1.283333, 1.21775, 
2.109423, 1.73576, 1.895503, 1.139829, 2.791613, 1.323696, 2.286188, 
1.805639, 2.511456, 2.083333, 1.772075, 1.703618, 1.383333, 2.444646, 
1.550857, 2.569984, 2.978193, 3.186045, 2.285046, 2.745075, 1.807316, 
0.932084, 2.165025, 1.17831, 1.517809, 2.159886, 1.130453, 0.486334, 
1.796288, 0.567081, 2.516524, 2.292468, 1.977908, 0.900076, 2.287188, 
1.25, 2.418595, 2.008056, 2.267024, 1.241291, 0.512357, 2.228122, 
2.71888, 0.720332, 1.053843, 0.982562, 0.864294, 1.400214, 0.606714, 
3.126837, 0.514198, 0.521602, 0.894424, 1.955372, 0.446585, 0.876504, 
3.034546, 3.126409, 1.818155, 0.983416, 0.813849, 2.509957, 1.932048, 
1.249001, 1.249929, 1.533654, 3.046003, 2.255497, 1.330192, 2.981263, 
2.773767, 1.932191, 2.016775, 2.888187, 2.390149, 2.936294, 0.716667, 
2.316524, 3.056888, 2.970593, 3.068024, 2.889007, 0.964319, 0.956673, 
2.384404, 1.033249, 2.392219, 0.780085, 2.993361, 0.947085, 0.974277, 
0.589454, 0.722588, 1.469659, 1.190359, 1.646143, 1.039464, 1.261727, 
1.21003, 1.202521, 1.540233, 1.582939, 1.295875, 1.316197, 1.522829, 
1.395169, 1.608097, 1.605217, 1.424143, 1.501501, 0.720617, 0.692133, 
0.943847, 0.726361, 0.857982, 1.00119, 0.931612, 0.936593, 0.880921
), final.airpressure = c(90.3429670210118, 100.056314503294, 
97.3091644688341, 95.6535847066497, 99.3265763810809, 96.5299354845676, 
99.2908252491736, 99.9962987211271, 98.4839088116185, 95.1270315428243, 
91.7964083849734, 101.021480506879, 100.453323066872, 98.7679512938402, 
98.4130259181779, 101.215628071472, 101.154916909799, 98.1888998199279, 
100.236578057928, 97.3442020744334, 96.8780695175868, 97.2157924966616, 
97.6952727499611, 89.3510660330727, 97.4026261211394, 97.8595387529882, 
91.4445861078662, 98.1653373115852, 100.694700579387, 101.094242163897, 
101.227774675623, 101.227774675623, 97.3325214710347, 97.3675674866686, 
101.203482924827, 99.8643906852719, 99.1241567035337, 97.2274590917429, 
100.260637723706, 101.239922737453, 95.3556106036821, 87.4837596830127, 
97.5312824821367, 98.2596212844703, 101.252072257138, 98.5311924350098, 
97.4260055568285, 97.941775300055, 101.215628071472, 96.8548215707678, 
90.6144028722749, 96.5878708247223, 98.9221495412202, 96.8896955834789, 
98.1300041505036, 93.6882866645953, 97.648390271736, 101.094242163897, 
93.1166652957402, 97.2974880696958, 95.940976332665, 96.9013230445826, 
100.344892041253, 101.167056228173, 88.3170492768192, 99.1955517955091, 
100.815606749275, 97.9065226068125, 98.9696435706551, 99.2908252491736, 
91.5763611671335, 96.333215139154, 101.009358656544, 99.5532993411879, 
93.3852278362198, 100.513613145927, 94.8306977255515, 101.057854786839, 
97.4961775397119, 101.033603811924, 97.4961775397119, 90.4405901323857, 
90.8866542537754, 101.142779048056, 98.9221495412202, 97.5429869382878, 
100.032303869205, 99.0052790563431, 95.3441686169426, 97.1458223183509, 
85.7068860068727, 99.7326566531564, 92.3488424802678, 97.1225101185704, 
101.288529565072, 98.1064557754255, 100.610152546045, 94.8762273867043, 
99.7565953632834, 95.206971819536, 97.988798636897, 101.203482924827, 
96.2176846131662, 90.9412026091979, 93.1054919663179, 96.2061391837523, 
97.2507964822998, 98.7916584468953, 100.74304563755, 101.179197003352, 
95.4357429674702, 101.203482924827, 99.0052790563431, 87.5362656890491, 
99.5532993411879, 99.8284459750613, 86.5024876526411, 91.2801339042574, 
96.9943929811272, 86.751973904839, 95.5732694326337, 100.755135528406, 
97.554692799058, 98.4130259181779, 95.1384474715514, 100.092341260942, 
99.8044900228565, 98.0358445503587, 94.5012604507927, 95.4013922834961, 
97.0060330066725, 100.441269391339, 99.7326566531564, 98.3067972240066, 
100.646378721282, 98.9696435706551, 88.4230933496698, 101.009358656544, 
100.477434757695, 95.7799308085443, 99.302740863126, 95.1042037946933, 
92.2491595688505, 99.5771950003942, 97.4961775397119, 99.1955517955091, 
101.142779048056, 98.6139934081953, 95.2412524995432, 101.227774675623, 
99.0528129975234, 97.5898188107684, 97.5781087351297, 100.791413906912, 
96.8083424123042, 94.5693258589238, 98.1653373115852, 93.9021396579619, 
98.3539958134293, 93.2620406569011, 94.1164807924228, 89.3081878129729, 
99.3981172677039, 96.6110546956734, 99.4577740333045, 99.613049243971, 
92.1606428704777, 101.264223234853, 101.276375670773, 94.5126012824831, 
95.8719236916393, 98.4720914516169, 96.8199601104416, 100.646378721282, 
97.1808011101903, 100.670536751023, 100.997238260744, 98.7560998507858, 
98.2478308373571, 99.0171604026964, 99.3504176202466, 96.3100979416952, 
96.2292314281149, 97.8947745289984, 100.803509602308, 100.610152546045, 
96.0331238936967, 92.848875102663, 94.5466319440964, 98.5784987599884, 
101.082111582687, 92.5374264091341, 93.0608120542108, 95.1042037946933, 
100.068321981469, 95.9524899406276, 98.1064557754255, 99.5771950003942, 
98.5548427591196, 90.3971890662113, 98.15355817787, 99.8284459750613, 
95.7799308085443, 95.9755213018661, 92.693019947478, 98.7916584468953, 
94.6033769450608, 96.0792308579518, 94.8193187245782, 96.7502748288739, 
100.140397117231, 95.8489171908438, 97.2741394745025, 101.13064264277, 
100.549804560724, 99.8763751312065, 100.658457011412, 99.6967593586619, 
100.924546421398, 92.5596380567634, 99.8164672802803, 94.6715159035427, 
97.4610852327918, 96.7502748288739, 98.7324012307697, 98.15355817787, 
100.140397117231, 95.1498647702722, 97.2391270868996, 100.803509602308, 
91.631323470876, 94.842078092087, 94.4672461199774, 97.0060330066725, 
97.554692799058, 98.2242541872674, 90.408037379785, 101.227774675623, 
98.389409626026, 97.4143151376009, 97.5664000646158, 88.837883277904, 
98.3067972240066, 92.3820940484839, 100.224550390235, 94.6828771671133, 
97.0293172485913, 99.2312686227987, 99.2193615850005, 99.9123370990196, 
92.8823067150292, 93.2620406569011, 100.368977705507, 94.6033769450608, 
97.7539075022777, 100.140397117231, 99.8164672802803, 101.239922737453, 
94.6715159035427, 99.9483120154717, 99.0409273731154, 87.5782931823713, 
98.000557998281, 101.300684917927, 100.320812156865, 94.785189913392, 
92.3488424802678, 98.0123187708731, 97.2507964822998, 99.8524076773845, 
100.791413906912, 98.3776036052463, 93.4524894115233, 98.8272298464039, 
100.104353062587, 95.4701060199168, 100.284703164497, 100.74304563755, 
93.5759281491901, 100.405117042172, 101.203482924827, 100.924546421398, 
91.1597236157778, 95.5503346002599, 93.5646997115319, 92.9826737947488, 
93.2508498834821, 99.9003483379078, 98.4130259181779, 99.4935852775937, 
91.8735497628596, 97.1108561166103, 94.8306977255515, 94.6033769450608, 
94.0261723258617, 100.501552236019, 91.2910881775692, 98.0123187708731, 
99.6847964653268, 96.7270575491, 101.167056228173, 94.3313111822093, 
100.912436202455, 95.6995094483651, 97.8712826022554, 89.0620370589062, 
96.936213800838, 101.142779048056, 98.7560998507858, 100.791413906912, 
99.8883610153609, 101.179197003352, 95.7914250898844, 97.2507964822998, 
99.7206894524044, 93.7220205195241, 100.960885798805, 100.730957197395, 
88.5505143251773, 94.0036087522406, 98.5075477862968, 98.6613394871811, 
99.1955517955091, 99.3861902092696, 98.7442498298192, 100.730957197395, 
101.045728571852, 95.6076820035206, 98.2832064236821, 101.094242163897, 
100.128380990558, 92.5263225842078, 101.106374200865, 99.8763751312065, 
91.8184421669338, 100.670536751023, 90.0723442570617, 89.1475776674053, 
101.264223234853, 100.779319662913, 99.2908252491736, 100.140397117231, 
97.8595387529882, 91.0285480828219, 94.7624441974078, 96.9711171200268, 
101.203482924827, 97.4260055568285, 100.332851376663, 93.5534726212054, 
95.6650638255479, 100.912436202455, 91.631323470876, 87.211235711269, 
85.7377460403079, 91.6753170637597, 94.3879269519511, 87.6098270436714, 
91.9286904332642, 90.9739473358343, 92.4042684118727, 90.408037379785, 
96.3100979416952, 86.3780137131668, 88.9659019811689, 94.2634170829669, 
87.6729288330611, 91.3787697026177, 93.1390159773857, 89.9859162986326, 
91.4775120851854, 90.690550948402, 86.9395606892669, 87.1589246647255, 
95.0585647311234, 95.3441686169426, 94.5920252209444, 92.8377339061359, 
89.8456477071546)), .Names = c("final.Latitude", "final.Longitude", 
"final.airpressure"), row.names = c(NA, -379L), class = "data.frame")

This is the code to plot based on this post: Plotting contours on an irregular grid

library(akima)
library(ggplot2)
library(reshape2)

x <- newfinal$final.Longitude
y <- newfinal$final.Latitude
z <- newfinal$final.airpressure

require(akima)

fld <- interp(x,y,z)

df <- melt(fld$z, na.rm = TRUE)
names(df) <- c("x", "y", "Rain")
df$Lon <- fld$x[df$x]
df$Lat <- fld$y[df$y]


ggplot(data = df, aes(x = Lon, y = Lat, z = Rain)) +
  geom_tile(aes(fill = Rain)) +
  stat_contour() +
  ggtitle("Air pressure Catalonia") +
  xlab("Longitude") +
  ylab("Latitude") +
  scale_fill_continuous(name = "kPa",
                        low = "white", high = "blue") +
  theme(plot.title = element_text(size = 20,),
        legend.title = element_text(size = 10),
        axis.text = element_text(size = 10),
        axis.title.x = element_text(size = 10, vjust = -0.5),
        axis.title.y = element_text(size = 10, vjust = 0.2),
        legend.text = element_text(size = 10))
delcast
  • 496
  • 1
  • 5
  • 16
  • How did you plot the shape file for the map? What format is that data in? – Stephen Henderson Mar 09 '18 at 12:59
  • All the plotting instructions are there. The format was a data frame with 3 variables: latitude, longitude and air pressure. It is also included. – delcast Mar 09 '18 at 14:02
  • What is your "observation"? I assume there is another dataframe with latitude and longitude with data you want to analyze? – www Mar 09 '18 at 14:30
  • My observation is just air pressure, if you scroll down in the code bit, it's all there. I would like to simply make another data frame with coordinates that have air pressure over 97 but that are along the coast (not inland). No more data. That's it! – delcast Mar 09 '18 at 17:14
  • @ÁngelaGill Yes but you want the intersect between the coastline and the df. You give the df but we don't know how the map/coastline are coded. There are many different packages and formats for this type of data. – Stephen Henderson Mar 09 '18 at 18:10
  • Oh! Ok. Sorry, got it. So the base map (the one in the first figure) is a ggmap from Stamen getmap() using the coordinates for Catalonia, zoom level 6. The second one doesn't have any map and is the code I attached. Let me know if there's anything else. "coastline intersection" is exactly what I want I think. I wonder how I could get it. – delcast Mar 09 '18 at 19:02

1 Answers1

5

Overview

Using the shape file for the western part of the Mediterranean Sea, I calculated the coordinates for the boundaries of the Balearic (Iberian Sea) and Western Basin portions. Then, I calculated the distance - in kilometers - of each 379 coordinate pairs in df to each coordinate pair for the boundaries of Balearic and Western Basin portions.

After filtering distance to only include the minimum distance values, I identified which points in df contained a distance that was less than or equal to 20 kilometers (based on the distance from Traiguera to Vinaròs).

Finally, df2 was created by returning the rows where final.airpressure was greater than 97 and whose coordinate pair was less than or equal to 20 kilometers from the Mediterranean Sea.

Reproducible Example

Note: df is not written down below to reduce the lines of code. To create df, simply copy and paste the dput() and store it.

Additionally, the shape file for the western part of the Mediterranean Sea needed to be downloaded in in my browser and unzipped inside my working directory prior to importing into .

SS of Western Basin Download with Instructions

After reading Plotting contours on an irregular grid, I use the data from df2 to reproduce that plot using base R. The GeoJSON file of the administrative boundaries of the counties of Catalonia was taken from their open data portal.

# load necessary packages
library( akima )
library( sf )
library( leaflet )
library( geosphere )

# create sf data frame
# of Catalonia
catalonia.polygon <-
read_sf( dsn = "https://analisi.transparenciacatalunya.cat/api/geospatial/txvb-mhz6?method=export&format=GeoJSON" 
         , layer = "OGRGeoJSON"
         , stringsAsFactors = FALSE )

# dissolve into one polygon
catalonia.polygon <-
  st_union( x = catalonia.polygon )

# create sf data frame
# of the western basin
western.basin <-
  read_sf( dsn = getwd()
           , layer = "iho"
           , stringsAsFactors = FALSE )

# view first version of the map
my.map <-
  leaflet() %>%
  setView( lng = 1.514619
           , lat = 41.875227
           , zoom = 8 ) %>%
  addTiles() %>%
  addPolygons( data = catalonia.polygon
               , fill = "#D24618"
               , color = "#D24618" ) %>%
  addCircleMarkers( data = df
                    , lng = ~final.Longitude
                    , lat = ~final.Latitude
                    , fillColor = "#10539A"
                    , fillOpacity = 0.75
                    , stroke = FALSE
                    , radius = 10
                    , group = "No Filter" ) %>%
  addPolygons( data = western.basin
               , label = ~name )

# display map
my.map

SS of Map without Filtering

# get the boundaries of each
# polygon within the western basin
list.of.polygon.boundaries <- 
  sapply( 
    X = methods::as( object = western.basin, Class = "Spatial" )@polygons
    , FUN = function( i )
      sp::coordinates( obj = i@Polygons[[1]] )
  )

# label each set of boundaries
names( list.of.polygon.boundaries ) <- western.basin$name

# from Google maps
# it looks like we only care about the
# the Balearic and Western Basin parts of the sea
# since they are nearest Catalonia
list.of.polygon.boundaries <-
  list.of.polygon.boundaries[ c("Balearic (Iberian Sea)", "Mediterranean Sea - Western Basin" ) ]

# calculate each points distance (in kilometers)
# from each boundary point within
# each polygon in list.of.polygon.boundaries
# ~1 minute to complete
distance <-
  apply(
    X = df[ c("final.Longitude", "final.Latitude") ]
    , MARGIN = 1
    , FUN = function( i )
      lapply(
        X = list.of.polygon.boundaries
        , FUN = function( j )
          distGeo(
            p1 = i
            , p2 = j
          ) / 1000 # to transform results into kilometers
      )
  )

# find the minimum distance value
# for each list in distance
distance.min <-
  lapply(
    X = distance
    , FUN = function( i )
      lapply(
        X = i
        , FUN = function( j )
          min( j )
      )
  )

# set the maximum distance
# allowed between a point in df
# and the sea to 20 kilometers
max.km <- 20

# identify which points in df
# are less than or equal to max.km
less.than.or.equal.to.max.km <-
  sapply(
    X = distance.min
    , FUN = function( i )
      sapply(
        X = i
        , FUN = function( j )
          j <= max.km
      )
  )

# convert matrix results into
# vector of TRUE/FALSE indices
less.than.or.equal.to.max.km <-
  apply(
    X = less.than.or.equal.to.max.km
    , MARGIN = 2
    , FUN = any
  )

# create subset from df
# where the air pressure is greater than 97 kpa
# AND the coordinate pairs are located
# along the Mediterranean coast
df2 <-
    df[ which( df$final.airpressure > 97 &
             less.than.or.equal.to.max.km ), ]

# View our results
my.map %>%
  hideGroup( group = "No Filter" ) %>%
  addCircleMarkers( data = df2
                    , lng = ~final.Longitude
                    , lat = ~final.Latitude
                    , fillColor = "#10539A"
                    , fillOpacity = 0.75
                    , stroke = FALSE
                    , radius = 10
                    , group = "Filter" )

SS of Filtered Map

Plot Contours on Irregular Grid

# plot intended results
bivariate.interpolation <-
  akima::interp(
    x = df2$final.Longitude
    , y = df2$final.Latitude
    , z = df2$final.airpressure
    )
# using base R
filled.contour(
  x = bivariate.interpolation$x
  , y = bivariate.interpolation$y
  , z = bivariate.interpolation$z
  , color.palette = colorRampPalette( c("white", "blue" ) )
  , xlab = "Longitude"
  , ylab = "Latitude"
  , main = "Catalonia Air Pressure"
  , key.title = title(
    main = "Air Pressure (kPa)"
    , cex.main = 0.5 )
)

# end of script #

SS of Plotting Contour

Final Data from df2

structure(list(final.Latitude = c(42.104854, 40.544337, 41.26267, 
40.745573, 40.881084, 41.201499, 41.494183, 40.873663, 41.197585, 
41.141899, 40.883885, 40.814408, 40.710754, 41.581905, 41.61424, 
41.137955, 41.553355, 42.17195, 41.447145, 41.398371, 41.954854, 
40.974225, 41.675766, 41.220308, 41.009161, 41.136268, 41.747716, 
42.306365, 41.525909, 41.187937, 41.862027, 41.074787, 41.724519, 
41.590543, 42.422663, 42.374163, 41.639757, 41.277658, 41.175305, 
42.403712, 41.417024, 41.165596, 41.208101, 41.197377, 41.594469, 
42.391185, 40.671331, 41.053748, 41.207852, 42.344326, 41.041842, 
40.655163, 41.607816, 41.731762, 42.028479, 41.102165, 41.360861, 
41.743718, 42.073092, 41.826875, 42.296039, 41.699929, 41.646217, 
40.734082, 40.716001, 41.480477, 41.18225, 41.039296, 42.360065, 
41.265454, 41.468488, 41.191922, 41.248929, 41.354354, 41.316667, 
42.218238, 41.666667, 41.917381, 41.175447, 41.971022, 41.437188, 
42.316667, 41.191613, 41.186914, 42.315525, 41.188829, 41.327836, 
41.051187, 41.278987, 41.002665, 41.224484, 41.155913, 41.496859, 
41.168999, 41.138704, 42.227089, 41.224079, 41.54329, 41.076036, 
41.383333, 41.26126, 41.694361, 41.253569, 42.13269, 42.323734, 
41.808423, 41.237045, 41.384189, 41.116667, 41.482015, 40.90787, 
41.699143, 41.146003, 41.952427, 40.812492, 40.597668, 42.036402, 
41.880549, 41.648215, 41.386759, 41.243112, 41.286117, 41.220468, 
42.061813, 41.247787, 42.283618, 41.316881, 42.138901, 42.246931, 
42.196145, 42.217381, 42.375733, 42.254854, 41.504069, 42.105996
), final.Longitude = c(3.085332, 0.48089, 1.170103, 0.618666, 
0.498917, 1.046086, 2.294718, 0.400167, 1.11167, 1.375338, 0.801686, 
0.714752, 0.578086, 2.549358, 2.539721, 0.908485, 2.401142, 3.074909, 
2.244789, 2.1741, 3.207173, 0.517667, 2.789078, 1.439364, 0.383916, 
0.989194, 2.558458, 2.967557, 2.368952, 1.569665, 3.074338, 1.052436, 
2.464097, 2.581156, 2.926337, 2.920236, 2.357387, 1.968755, 1.324377, 
3.150642, 1.919698, 1.444914, 1.672893, 1.635581, 2.405817, 2.999929, 
0.519665, 0.438496, 1.247073, 3.064954, 0.632897, 0.468583, 2.289721, 
2.503212, 3.10289, 0.752582, 2.110014, 2.628836, 3.010814, 2.893647, 
2.912027, 2.845646, 2.741505, 0.37308, 0.530085, 2.316382, 1.045336, 
0.596649, 3.000214, 1.365168, 2.280459, 1.208506, 1.294718, 1.849072, 
1.816667, 2.907316, 2.75, 3.163098, 1.269737, 3.148144, 2.009243, 
3.116667, 1.255497, 1.209089, 2.921769, 0.85608, 2.094718, 0.871539, 
1.297127, 0.599667, 1.310832, 1.107423, 2.157387, 0.976254, 1.051562, 
3.042647, 1.21775, 2.109423, 1.139829, 2.083333, 1.772075, 2.444646, 
1.550857, 2.978193, 3.186045, 2.745075, 1.807316, 1.796288, 1.25, 
2.267024, 0.512357, 2.71888, 1.400214, 3.126837, 0.521602, 0.446585, 
3.034546, 3.126409, 2.509957, 1.932048, 1.249001, 1.249929, 1.533654, 
3.046003, 1.330192, 2.981263, 2.016775, 2.888187, 2.936294, 3.056888, 
2.970593, 3.068024, 2.889007, 2.392219, 2.993361), final.airpressure = c(101.021480506879, 
100.453323066872, 98.4130259181779, 101.215628071472, 101.154916909799, 
98.1888998199279, 100.236578057928, 97.3442020744334, 97.8595387529882, 
100.694700579387, 101.094242163897, 101.227774675623, 101.227774675623, 
101.203482924827, 99.8643906852719, 97.2274590917429, 100.260637723706, 
101.239922737453, 101.252072257138, 101.215628071472, 98.9221495412202, 
101.094242163897, 101.167056228173, 99.1955517955091, 97.9065226068125, 
98.9696435706551, 99.2908252491736, 101.009358656544, 99.5532993411879, 
100.513613145927, 101.057854786839, 101.033603811924, 97.4961775397119, 
101.142779048056, 98.9221495412202, 100.032303869205, 99.0052790563431, 
101.288529565072, 100.610152546045, 101.203482924827, 97.2507964822998, 
100.74304563755, 101.179197003352, 101.203482924827, 99.5532993411879, 
99.8284459750613, 99.8044900228565, 97.0060330066725, 99.7326566531564, 
100.646378721282, 101.009358656544, 99.302740863126, 99.5771950003942, 
99.1955517955091, 101.142779048056, 98.6139934081953, 101.227774675623, 
99.0528129975234, 100.791413906912, 99.3981172677039, 99.613049243971, 
101.264223234853, 101.276375670773, 97.1808011101903, 100.670536751023, 
100.997238260744, 98.7560998507858, 100.803509602308, 100.610152546045, 
98.5784987599884, 101.082111582687, 100.068321981469, 98.5548427591196, 
98.15355817787, 98.7916584468953, 100.140397117231, 101.13064264277, 
100.549804560724, 99.8763751312065, 100.658457011412, 99.6967593586619, 
100.924546421398, 99.8164672802803, 100.140397117231, 100.803509602308, 
97.554692799058, 101.227774675623, 98.389409626026, 98.3067972240066, 
99.2312686227987, 99.2193615850005, 99.9123370990196, 100.368977705507, 
97.7539075022777, 99.8164672802803, 101.239922737453, 99.9483120154717, 
99.0409273731154, 101.300684917927, 99.8524076773845, 100.791413906912, 
98.8272298464039, 100.104353062587, 100.284703164497, 100.74304563755, 
100.405117042172, 101.203482924827, 98.4130259181779, 100.501552236019, 
99.6847964653268, 101.167056228173, 100.912436202455, 101.142779048056, 
100.791413906912, 101.179197003352, 99.7206894524044, 100.960885798805, 
100.730957197395, 98.6613394871811, 99.1955517955091, 99.3861902092696, 
98.7442498298192, 100.730957197395, 101.045728571852, 98.2832064236821, 
101.094242163897, 101.106374200865, 99.8763751312065, 100.670536751023, 
101.264223234853, 100.779319662913, 99.2908252491736, 100.140397117231, 
101.203482924827, 100.332851376663)), .Names = c("final.Latitude", 
"final.Longitude", "final.airpressure"), row.names = c(12L, 13L, 
15L, 16L, 17L, 18L, 19L, 20L, 26L, 29L, 30L, 31L, 32L, 35L, 36L, 
38L, 39L, 40L, 45L, 49L, 53L, 58L, 64L, 66L, 68L, 69L, 70L, 73L, 
74L, 76L, 78L, 80L, 81L, 84L, 85L, 87L, 88L, 95L, 97L, 102L, 
107L, 109L, 110L, 112L, 115L, 116L, 127L, 131L, 133L, 135L, 138L, 
141L, 144L, 146L, 147L, 148L, 150L, 151L, 154L, 163L, 166L, 168L, 
169L, 175L, 176L, 177L, 178L, 185L, 186L, 190L, 191L, 195L, 199L, 
201L, 206L, 211L, 214L, 215L, 216L, 217L, 218L, 219L, 221L, 227L, 
230L, 235L, 238L, 239L, 243L, 248L, 249L, 250L, 253L, 255L, 257L, 
258L, 260L, 261L, 264L, 270L, 271L, 274L, 275L, 277L, 278L, 280L, 
281L, 289L, 296L, 299L, 301L, 303L, 308L, 310L, 312L, 315L, 317L, 
318L, 322L, 323L, 324L, 325L, 326L, 327L, 329L, 330L, 333L, 334L, 
336L, 339L, 340L, 341L, 342L, 347L, 349L), class = "data.frame")

Session Info

R version 3.4.3 (2017-11-30)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS High Sierra 10.13.2

Matrix products: default
BLAS: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/3.4/Resources/lib/libRlapack.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets 
[6] methods   base     

other attached packages:
[1] geosphere_1.5-7    leaflet_1.1.0.9000
[3] sf_0.6-0           akima_0.6-2       

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.15    magrittr_1.5    units_0.5-1    
 [4] xtable_1.8-2    lattice_0.20-35 R6_2.2.2       
 [7] rlang_0.2.0     udunits2_0.13   tools_3.4.3    
[10] grid_3.4.3      e1071_1.6-8     DBI_0.8        
[13] htmltools_0.3.6 crosstalk_1.0.0 class_7.3-14   
[16] yaml_2.1.17     digest_0.6.15   shiny_1.0.5    
[19] htmlwidgets_1.0 mime_0.5        sp_1.2-7       
[22] compiler_3.4.3  pillar_1.2.1    classInt_0.1-24
[25] httpuv_1.3.6.2 
Cristian E. Nuno
  • 2,822
  • 2
  • 19
  • 33
  • 1
    This is amazing! Thank you for explaining everything with enough detail that I can understand how you did it and replicate it. – delcast Mar 10 '18 at 09:05
  • No problem! I think this is a great question and can definitely be solved in multiple ways. – Cristian E. Nuno Mar 10 '18 at 14:27
  • Hey @aspiringurbandatascientist! This is extremely silly but when I try to read the Western basin info as an sf data frame, it shows error. I can read it with OGR but it imports as SPDF and can’t use functions like st_geometry or st_union. What I do is that I unzip the file and it has about 8 mini files within. I place them in the working directory but read_sf does not work. Thank you! – delcast Mar 21 '18 at 16:42
  • No such thing as silly questions. What's the error message when you run `western.basin <- read_sf( dsn = getwd() , layer = "iho" , stringsAsFactors = FALSE )`? – Cristian E. Nuno Mar 21 '18 at 16:46
  • You’re so kind. It’s this: error in cpl_read_ogr(dsn, layer, as.character(options), quiet, type,: open failed. This happens me with all shapefiles so it’s me clearly not knowing how to use the sf package with s4 objects. – delcast Mar 21 '18 at 16:55
  • 1
    @ÁngelaGill just for safeguard, I added a screenshot of how I downloaded the western basin .zip file containing the 8 mini files that, together, make up the western basin shapefile. – Cristian E. Nuno Mar 21 '18 at 16:55
  • thank you for that. Yes, so far so good. That is indeed what I downloaded and unzipped. I place each one in my working directory. – delcast Mar 21 '18 at 16:58
  • Would you mind sharing the output of these two commands: `sf::sf_extSoftVersion()` and `sessionInfo()`? – Cristian E. Nuno Mar 21 '18 at 17:02
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/167290/discussion-between-angela-gill-and-aspiringurbandatascientist). – delcast Mar 21 '18 at 20:32
  • anything that might help! – delcast Mar 21 '18 at 21:12