如何将点分类到另一组点的区域?

时间:2014-01-21 00:15:32

标签: r ggplot2

我有一组点在x-y空间中的区域,基于这些区域,我需要对第二组点进行分类(根据它们属于哪个区域)。例如,区域的数据框看起来像这样(这些可以标记为a,b,c,d,e等,但我没有与这些区域对应的数据):

test <- structure(list(x1 = c(6.080515, 6.019633, 5.958752, 5.89787, 
5.836989, 5.776107, 5.715226, 5.654344, 5.593463, 5.532582, 5.4717, 
5.410819, 5.349937, 5.289056, 5.228174, 5.167292, 5.106411, 5.04553, 
8.577537, 8.516655, 8.455774, 8.394893, 8.334011, 8.27313, 8.212248, 
8.151367, 8.090485, 8.029604, 7.968721, 7.907841, 7.846959, 7.786078, 
7.725197, 7.664315, 7.603434, 7.542551, 7.481671, 7.420789, 7.359908, 
7.299026, 7.238145, 7.177263, 7.116381, 7.055501, 6.994619, 6.933738, 
6.872856, 6.811975, 6.751093, 6.690212, 6.62933, 6.568449, 6.507567, 
6.446686, 6.385805, 6.324923, 6.264042, 6.20316, 6.142279, 10.161337, 
10.100456, 10.039574, 9.978693, 9.917812, 9.85693, 9.796049, 
9.735167, 9.674285, 9.613404, 9.552523, 9.491641, 9.43076, 9.369878, 
9.308997, 9.248115, 9.187234, 9.126353, 9.065471, 9.00459, 8.943708, 
8.882827, 8.821945, 8.761064, 8.700182, 8.639301, 11.136322, 
11.075442, 11.01456, 10.953679, 10.892797, 10.831916, 10.771035, 
10.710153, 10.649272, 10.58839, 10.527509, 10.466627, 10.405746, 
10.344864, 10.283982, 10.223101, 11.867783, 11.806902, 11.746019, 
11.685139, 11.624257, 11.563376, 11.502495, 11.441613, 11.380732, 
11.319849, 11.258969, 11.198087, 5.048178, 12.842769, 12.781888, 
12.721006, 12.660125, 12.599243, 12.538362, 12.47748, 12.416599, 
12.355718, 12.294836, 12.233955, 12.173073, 12.112192, 12.05131, 
11.990429, 11.929546, 5.04906, 13.513348, 13.452466, 13.391585, 
13.330703, 13.269822, 13.208941, 13.148058, 13.087178, 13.026296, 
12.965415, 12.904533, 5.110825, 5.049943, 14.062164, 14.001282, 
13.940401, 13.879519, 13.818638, 13.757755, 13.696875, 13.635993, 
13.575112, 14.550098, 14.489216, 14.428335, 14.367452, 5.111707, 
14.306572, 14.245691, 14.184809, 14.123928, 15.098914, 15.038032, 
14.977151, 14.916269, 14.855388, 14.794506, 14.733625, 14.672743, 
14.611862, 5.173471, 15.465085, 15.404203, 15.343322, 15.282441, 
15.221559, 15.160678, 5.174354, 15.831256, 15.770375, 15.709493, 
15.648612, 15.58773, 15.526848, 5.236118, 16.319191, 16.258309, 
16.258309, 16.197428, 16.136545, 16.075665, 16.014783, 15.953902, 
15.89302, 5.237001, 16.685362, 16.62448, 16.563599, 16.502718, 
16.441836, 16.380955, 16.380955, 16.320073, 16.320073, 5.298765, 
17.051533, 16.990652, 16.929769, 16.868889, 16.808007, 16.747126, 
16.442719, 5.299647, 17.356823, 17.295941, 17.23506, 17.174179, 
17.113296, 16.443601, 5.361412, 17.601232, 17.54035, 17.479468, 
17.418587, 16.505365, 5.423176, 17.906522, 17.84564, 17.784759, 
17.723877, 17.662995, 16.56713, 5.48494, 18.15093, 18.090049, 
18.029167, 17.968286, 16.628894, 5.485822, 18.577983, 18.517101, 
18.456219, 18.395338, 18.334457, 18.273576, 18.212694, 16.690658, 
5.486705, 16.69154, 5.548469, 16.753304, 5.549352, 16.754187, 
5.611116, 16.815951, 5.67288, 16.877715, 5.673763, 16.939479, 
5.735527, 17.001243, 5.736409, 17.002126, 5.798173, 17.06389, 
5.859937, 17.125654, 5.86082, 17.126537, 5.922584, 17.1883, 5.984348, 
17.250065, 5.985231, 17.250948, 6.046995, 17.312711, 6.108759, 
6.047877, 17.374476, 6.170523, 17.375359, 6.171406, 17.437123, 
6.23317, 17.498887, 6.294934, 6.234053, 17.499769, 6.356698, 
17.561533, 6.357581, 17.623298, 6.419344, 17.685062, 6.481109, 
6.420227, 17.685944, 6.481992, 17.747708, 6.543756, 17.748591, 
6.60552, 17.810355, 6.667284, 6.606402, 17.872119, 6.668166, 
17.933883, 6.729931, 17.934766, 6.791695, 17.935649, 6.853458, 
6.792577, 17.997412, 6.854341, 18.059177, 6.916105, 18.120941, 
6.97787, 18.121823, 7.039634, 6.978752, 18.183588, 7.040516, 
18.18447, 7.10228, 18.185352, 7.164043, 18.247117, 7.164927, 
18.308881, 7.226691, 18.309764, 7.288455, 18.371528, 7.289338, 
18.433292, 7.351102, 18.434174, 7.351985, 18.495939, 7.413749, 
18.496821, 7.475513, 18.497704, 7.476395, 18.559468, 7.538159, 
18.621232, 7.599924, 18.622115, 7.600806, 18.683879, 7.66257, 
18.684761, 7.663453, 18.746524, 7.725217, 18.80829, 7.786981, 
18.809172, 7.787864, 18.810055, 7.849628, 18.871819, 7.850511, 
18.933583, 7.912275, 18.934466, 7.974039, 18.99623, 7.974921, 
18.997112, 7.975804, 19.058876, 18.997995, 8.037568, 19.059759, 
8.099332, 19.121522, 8.100215, 19.122406, 8.161979, 19.18417, 
19.123288, 8.162862, 19.185053, 8.224626, 19.246817, 8.28639, 
19.247699, 8.287271, 19.309463, 19.248582, 8.288155, 19.310346, 
8.349919, 19.37211, 8.411683, 19.372993, 8.412566, 19.434757, 
8.413448, 19.496521, 8.475213, 19.497404, 8.476095, 19.498286, 
8.537859, 19.56005, 19.499169, 8.538742, 19.560933, 8.600506, 
19.622697, 8.601389, 19.62358, 8.663153, 19.685344, 8.664035, 
19.686226, 8.725798, 19.687109, 8.726682, 19.748873, 8.727565, 
19.810637, 8.789329, 19.81152, 8.851093, 19.812402, 8.851976, 
19.874167, 19.813285, 8.852858, 19.875049, 8.914622, 19.936813, 
8.915505, 19.937696, 8.977269, 19.938578, 8.978152, 20.000343, 
9.039916, 20.001225, 9.040798, 20.062989, 9.041681, 20.124753, 
9.103445, 20.125636, 9.165209, 20.126519, 9.166092, 20.188283, 
20.127401, 9.166974, 20.189165, 9.228739, 20.25093, 9.229621, 
20.251812, 9.230504, 20.313576, 9.292268, 20.314459, 9.293151, 
20.315342, 9.294033, 20.377106, 9.355796, 20.377988, 9.35668, 
20.378871, 9.418444, 20.440635, 9.419327, 20.441518, 9.481091, 
20.503282, 9.481973, 20.504163, 9.482856, 20.565929, 9.54462, 
20.566811, 9.545503, 20.567694, 9.546385, 20.629458, 20.568576, 
9.608149, 20.630341, 9.609032, 20.692105, 9.609915, 20.692987, 
9.671679, 20.69387, 9.672561, 20.755634, 9.673444, 20.756517, 
9.735208, 20.818281, 9.736091, 20.819163, 9.797854, 20.820046, 
9.798738, 20.88181, 9.79962, 20.882692, 9.861384, 20.883575, 
9.862267, 20.945339, 9.86315, 20.946221, 9.924914, 20.947105, 
9.925796, 21.008869, 9.926679, 21.00975, 9.988443, 21.010634, 
9.989325, 21.072398, 9.990208, 21.073281, 10.051972, 21.074163, 
10.052854, 21.135928, 10.053738, 21.13681, 10.115502, 21.198574, 
10.116383, 21.199457, 10.117267, 21.20034, 10.179031, 21.262104, 
10.179914, 21.262986, 10.180796, 21.263869, 10.181679, 21.264752, 
10.243443, 21.326516, 10.244325, 21.327398, 10.30609, 21.328281, 
10.306973, 21.390045, 10.307854, 21.390928, 10.369619, 21.452692, 
10.370502, 21.453574, 10.371385, 21.454457, 10.372267, 21.516221, 
10.37315, 21.517104, 10.434914, 21.517986, 10.435796, 21.57975, 
10.436679, 21.580633, 10.498443, 21.581516, 10.499325, 21.64328, 
10.500209, 21.644162, 10.501091, 21.645045, 10.562854, 21.645928, 
10.563738, 21.707692, 10.564621, 21.708574, 10.626385, 21.709457, 
10.627267, 21.771221, 10.62815, 21.772104, 10.629033, 21.772986, 
10.690796, 21.834751, 10.691679, 21.835633, 10.692562, 21.836516, 
10.693445, 21.837398, 10.755209, 21.899163, 10.756091, 21.900045, 
10.756974, 21.900928, 10.818738, 21.962692, 10.819621, 21.963575, 
10.820503, 21.964457, 10.821386, 22.026221, 10.822269, 22.027104, 
10.884033, 22.027987, 10.884915, 22.028869, 10.885798, 22.029752, 
10.947562, 22.091516, 10.948445, 22.092399, 10.949327, 22.093281, 
10.95021, 22.155045, 10.951093, 22.155928, 11.012857, 22.217692, 
11.013739, 22.218575, 11.014622, 22.219457, 11.015505, 22.22034, 
11.077269, 22.221223, 11.078151, 22.282987, 11.079034, 22.283869, 
11.079917, 22.284752, 11.141681, 22.346516, 11.142563, 22.347399, 
11.143446, 22.348281, 11.144329, 22.349163, 11.145211, 22.350047, 
11.146094, 22.411811, 11.207858, 22.412692, 11.20874, 22.413576, 
11.209623, 22.47534, 11.210506, 22.476223, 11.27227, 22.477105, 
11.273153, 22.477988, 11.274035, 22.539752, 11.274918, 22.540635, 
11.2758, 22.541517, 11.276683, 22.5424, 11.277566, 22.604163, 
11.33933, 22.605047, 11.340212, 22.605929, 11.341095, 22.667692, 
11.402859, 22.668576, 11.403742, 22.669459, 11.404624, 22.670341, 
11.405507, 22.732105, 11.40639, 22.732988, 11.407271, 22.733871, 
11.408155, 22.734753, 11.469919, 22.735636, 11.470802, 22.7974, 
11.471684, 22.798283, 11.472567, 22.799165, 11.47345, 11.535213, 
11.536096, 11.536979, 11.537862, 11.599626, 11.600508, 11.601391, 
11.602274, 11.603155, 11.604039, 11.604922, 11.666686, 11.667568, 
11.668451, 10, 10.1, 10.2, 10.3, 10.4, 10.5, 10.6, 10.7, 10.8, 
10.9, 11, 11.1, 11.2, 11.3, 11.4, 11.5, 11.6, 11.7, 11.8, 11.9, 
12, 12.1, 12.2, 12.3, 12.4, 12.5, 12.6, 12.7, 12.8, 12.9, 13, 
13.1, 13.2, 13.3, 13.4, 13.5, 13.6, 13.7, 13.8, 13.9, 14, 14.1, 
14.2, 14.3, 14.4, 14.5, 14.6, 14.7, 14.8, 14.9, 15, 15.1, 15.2, 
15.3, 15.4, 15.5, 15.6, 15.7, 15.8, 15.9, 16, 16.1, 16.2, 16.3, 
16.4, 16.5, 16.6, 16.7, 16.8, 16.9, 17, 17.1, 17.2, 17.3, 17.4, 
17.5, 17.6, 17.7, 17.8, 17.9, 18, 18.1, 18.2, 18.3, 18.4, 18.5, 
18.6, 18.7, 18.8, 18.9, 19, 19.1, 19.2, 19.3, 19.4, 19.5, 19.6, 
19.7, 19.8, 19.9, 20, 20.1, 20.2, 20.3, 20.4, 20.5, 20.6, 20.7, 
20.8, 20.9, 21, 21.1, 21.2, 21.3, 21.4, 21.5, 21.6, 21.7, 21.8, 
21.9, 22, 22.1, 22.2, 22.3, 22.4, 22.5, 22.6, 22.7, 22.8, 22.9, 
23, 23.1, 23.2, 23.3, 23.4, 23.5, 23.6, 23.7, 23.8, 23.9, 24, 
24.1, 24.2, 24.3, 24.4, 24.5, 24.6, 24.7, 24.8, 24.9, 25, 5, 
5.1, 5.2, 5.3, 5.4, 5.5, 5.6, 5.7, 5.8, 5.9, 6, 6.1, 6.2, 6.3, 
6.4, 6.5, 6.6, 6.7, 6.8, 6.9, 7, 7.1, 7.2, 7.3, 7.4, 7.5, 7.6, 
7.7, 7.8, 7.9, 8, 8.1, 8.2, 8.3, 8.4, 8.5, 8.6, 8.7, 8.8, 8.9, 
9, 9.1, 9.2, 9.3, 9.4, 9.5, 9.6, 9.7, 9.8, 9.9, 10, 10.1, 10.2, 
10.3, 10.4, 10.5, 10.6, 10.7, 10.8, 10.9, 11, 11.1, 11.2, 11.3, 
11.4, 11.5, 11.6, 11.7, 11.8, 11.9, 12, 12.1, 12.2, 12.3, 12.4, 
12.5, 12.6, 12.7, 12.8, 12.9, 13, 13.1, 13.2, 13.3, 13.4, 13.5, 
13.6, 13.7, 13.8, 13.9, 14, 14.1, 14.2, 14.3, 14.4, 14.5, 14.6, 
14.7, 14.8, 14.9, 15, 15.1, 15.2, 15.3, 15.4, 15.5, 15.6, 15.7, 
15.8, 15.9, 16, 16.1, 16.2, 16.3, 16.4, 16.5, 16.6, 16.7, 16.8, 
16.9, 17, 17.1, 17.2, 17.3, 17.4, 17.5, 17.6, 17.7, 17.8, 17.9, 
18, 18.1, 18.2, 18.3, 18.4, 18.5, 18.6, 18.7, 18.8, 18.9, 19, 
19.1, 19.2, 19.3, 19.4, 19.5, 19.6, 19.7, 19.8, 19.9, 20, 10, 
10.1, 10.2, 10.3, 10.4, 10.5, 10.6, 10.7, 10.8, 10.9, 11, 11.1, 
11.2, 11.3, 11.4, 11.5, 11.6, 11.7, 11.8, 11.9, 12, 12.1, 12.2, 
12.3, 12.4, 12.5, 12.6, 12.7, 12.8, 12.9, 13, 13.1, 13.2, 13.3, 
13.4, 13.5, 13.6, 13.7, 13.8, 13.9, 14, 14.1, 14.2, 14.3, 14.4, 
14.5, 14.6, 14.7, 14.8, 14.9, 15, 15.1, 15.2, 15.3, 15.4, 15.5, 
15.6, 15.7, 15.8, 15.9, 16, 16.1, 16.2, 16.3, 16.4, 16.5, 16.6, 
16.7, 16.8, 16.9, 17, 17.1, 17.2, 17.3, 17.4, 17.5), y1 = c(3.2326, 
3.2334, 3.2342, 3.2349, 3.2358, 3.2366, 3.2374, 3.2382, 3.2389, 
3.2398, 3.2406, 3.2414, 3.2422, 3.2429, 3.2438, 3.2446, 3.2454, 
3.2462, 3.2604, 3.2612, 3.262, 3.2628, 3.2636, 3.2644, 3.2652, 
3.266, 3.2668, 3.2676, 3.2684, 3.2692, 3.2701, 3.2709, 3.2717, 
3.2725, 3.2732, 3.2741, 3.2749, 3.2757, 3.2765, 3.2772, 3.2781, 
3.2789, 3.2797, 3.2805, 3.2812, 3.2821, 3.2829, 3.2837, 3.2845, 
3.2852, 3.2861, 3.2869, 3.2877, 3.2885, 3.2893, 3.2901, 3.2909, 
3.2917, 3.2925, 3.3003, 3.3011, 3.3019, 3.3027, 3.3035, 3.3043, 
3.3051, 3.3059, 3.3067, 3.3075, 3.3083, 3.3091, 3.3099, 3.3107, 
3.3115, 3.3123, 3.3131, 3.3139, 3.3147, 3.3155, 3.3163, 3.3171, 
3.3179, 3.3187, 3.3195, 3.3203, 3.3482, 3.349, 3.3498, 3.3506, 
3.3514, 3.3522, 3.353, 3.3538, 3.3546, 3.3554, 3.3562, 3.357, 
3.3578, 3.3586, 3.3594, 3.3602, 3.3992, 3.4, 3.4008, 3.4016, 
3.4024, 3.4032, 3.404, 3.4048, 3.4056, 3.4064, 3.4072, 3.408, 
3.4282, 3.4471, 3.4479, 3.4487, 3.4495, 3.4503, 3.4511, 3.4519, 
3.4527, 3.4535, 3.4543, 3.4551, 3.4559, 3.4567, 3.4575, 3.4583, 
3.4591, 3.4889, 3.499, 3.4998, 3.5006, 3.5013, 3.5022, 3.503, 
3.5038, 3.5046, 3.5053, 3.5062, 3.507, 3.5488, 3.5495, 3.5525, 
3.5533, 3.5541, 3.5549, 3.5556, 3.5565, 3.5573, 3.5581, 3.5589, 
3.6067, 3.6075, 3.6082, 3.6091, 3.6095, 3.6099, 3.6107, 3.6115, 
3.6122, 3.6602, 3.661, 3.6618, 3.6625, 3.6634, 3.6642, 3.665, 
3.6658, 3.6665, 3.6693, 3.7161, 3.7168, 3.7177, 3.7185, 3.7193, 
3.7201, 3.73, 3.7719, 3.7727, 3.7734, 3.7743, 3.7751, 3.7759, 
3.7899, 3.8262, 3.827, 3.827, 3.8277, 3.8286, 3.8294, 3.8302, 
3.831, 3.8317, 3.8506, 3.882, 3.8829, 3.8837, 3.8845, 3.8853, 
3.886, 3.886, 3.8869, 3.8869, 3.9104, 3.938, 3.9388, 3.9396, 
3.9404, 3.9412, 3.942, 3.946, 3.971, 3.9946, 3.9954, 3.9962, 
3.9969, 3.9978, 4.0066, 4.031, 4.0521, 4.0529, 4.0537, 4.0545, 
4.0665, 4.0909, 4.1088, 4.1096, 4.1104, 4.1112, 4.112, 4.1264, 
4.1507, 4.1663, 4.1671, 4.1679, 4.1687, 4.1863, 4.2114, 4.2213, 
4.2221, 4.2229, 4.2237, 4.2245, 4.2253, 4.2261, 4.2461, 4.2721, 
4.3068, 4.332, 4.3667, 4.3926, 4.4274, 4.4525, 4.4872, 4.5124, 
4.5471, 4.5731, 4.607, 4.6329, 4.6669, 4.6936, 4.7276, 4.7534, 
4.7873, 4.8134, 4.8473, 4.874, 4.908, 4.9339, 4.9679, 4.9938, 
5.0277, 5.0545, 5.0884, 5.1144, 5.1482, 5.1742, 5.175, 5.2082, 
5.2341, 5.2688, 5.2948, 5.3287, 5.3547, 5.3886, 5.4144, 5.4153, 
5.4493, 5.4744, 5.5091, 5.5351, 5.569, 5.595, 5.6289, 5.6548, 
5.6556, 5.6896, 5.7155, 5.7494, 5.7753, 5.8101, 5.8353, 5.87, 
5.8951, 5.8959, 5.9299, 5.9558, 5.9897, 6.0157, 6.0504, 6.0756, 
6.1111, 6.1354, 6.1362, 6.171, 6.1961, 6.2308, 6.256, 6.2907, 
6.3159, 6.3514, 6.3757, 6.3765, 6.4113, 6.4364, 6.472, 6.4963, 
6.5326, 6.5562, 6.5925, 6.6168, 6.6524, 6.6766, 6.713, 6.7366, 
6.7729, 6.7973, 6.8328, 6.8572, 6.8935, 6.9178, 6.9534, 6.9777, 
7.014, 7.0376, 7.0747, 7.0983, 7.1346, 7.1581, 7.1945, 7.218, 
7.2551, 7.2787, 7.315, 7.3386, 7.3757, 7.3992, 7.4356, 7.4591, 
7.4954, 7.519, 7.5561, 7.5796, 7.6168, 7.6395, 7.6766, 7.7002, 
7.7365, 7.7601, 7.7972, 7.8199, 7.8571, 7.8806, 7.9178, 7.9413, 
7.9776, 7.9784, 8.0012, 8.0383, 8.0611, 8.0982, 8.1217, 8.1589, 
8.1816, 8.2187, 8.2196, 8.2423, 8.2793, 8.3022, 8.3393, 8.362, 
8.4, 8.4227, 8.4599, 8.4607, 8.4834, 8.5205, 8.5433, 8.5804, 
8.6032, 8.6411, 8.6638, 8.701, 8.7245, 8.7608, 8.7844, 8.8215, 
8.8451, 8.8822, 8.9049, 8.9421, 8.9428, 8.9656, 9.0027, 9.0255, 
9.0626, 9.0862, 9.1233, 9.146, 9.1832, 9.2067, 9.2438, 9.2666, 
9.3045, 9.3273, 9.3644, 9.3879, 9.4243, 9.4478, 9.4849, 9.5077, 
9.5455, 9.5684, 9.6055, 9.6063, 9.629, 9.6662, 9.6888, 9.726, 
9.7496, 9.7867, 9.8094, 9.8474, 9.87, 9.9073, 9.93, 9.968, 9.9907, 
10.0278, 10.0514, 10.0877, 10.1112, 10.1484, 10.1711, 10.2091, 
10.2318, 10.2689, 10.2697, 10.2925, 10.3296, 10.3524, 10.3895, 
10.413, 10.4502, 10.4737, 10.51, 10.5335, 10.5707, 10.5943, 10.6314, 
10.6549, 10.6913, 10.7147, 10.7519, 10.7755, 10.8126, 10.8354, 
10.8725, 10.896, 10.9332, 10.9559, 10.9929, 11.0166, 11.0537, 
11.0773, 11.1136, 11.1371, 11.1743, 11.1978, 11.2349, 11.2585, 
11.2948, 11.2955, 11.3184, 11.3555, 11.379, 11.4154, 11.4397, 
11.4761, 11.4996, 11.5367, 11.5603, 11.5966, 11.6209, 11.6573, 
11.6808, 11.7172, 11.7415, 11.7778, 11.8014, 11.8385, 11.862, 
11.8984, 11.9226, 11.959, 11.9826, 12.0197, 12.0433, 12.0796, 
12.104, 12.1403, 12.1638, 12.201, 12.2245, 12.2608, 12.2852, 
12.3215, 12.3451, 12.3822, 12.4057, 12.4421, 12.4664, 12.5027, 
12.5263, 12.5634, 12.587, 12.6233, 12.6476, 12.684, 12.7075, 
12.7438, 12.7682, 12.8045, 12.8289, 12.8652, 12.8887, 12.9251, 
12.9494, 12.9858, 13.0101, 13.0464, 13.0708, 13.1071, 13.1306, 
13.167, 13.1913, 13.2277, 13.2511, 13.2883, 13.3119, 13.3482, 
13.3726, 13.4089, 13.4324, 13.4687, 13.4931, 13.5293, 13.5537, 
13.5901, 13.6145, 13.65, 13.6751, 13.7107, 13.7349, 13.7713, 
13.7957, 13.8312, 13.8564, 13.8919, 13.9162, 13.9526, 13.9769, 
14.0124, 14.0376, 14.0731, 14.0983, 14.1338, 14.1581, 14.1945, 
14.2187, 14.2543, 14.2795, 14.315, 14.3394, 14.3757, 14.4, 14.4356, 
14.4607, 14.4963, 14.5213, 14.5569, 14.5813, 14.6168, 14.642, 
14.6775, 14.7026, 14.7382, 14.7633, 14.7988, 14.8232, 14.8587, 
14.8839, 14.9194, 14.9445, 14.9801, 15.0044, 15.0399, 15.0651, 
15.1006, 15.1258, 15.1613, 15.1864, 15.2212, 15.2471, 15.2818, 
15.307, 15.3425, 15.3677, 15.4032, 15.4283, 15.4639, 15.4882, 
15.5237, 15.5489, 15.5844, 15.6096, 15.6451, 15.6702, 15.705, 
15.7309, 15.7657, 15.7908, 15.8255, 15.8515, 15.8862, 15.9121, 
15.9468, 15.9727, 16.0076, 16.0327, 16.0682, 16.0934, 16.128, 
16.1541, 16.1888, 16.2147, 16.2495, 16.2746, 16.3093, 16.3353, 
16.37, 16.396, 16.4307, 16.4566, 16.4914, 16.5173, 16.552, 16.5779, 
16.6118, 16.6379, 16.6726, 16.6985, 16.7332, 16.7591, 16.7931, 
16.8199, 16.8538, 16.8798, 16.9144, 16.9404, 16.9752, 17.0011, 
17.035, 17.0618, 17.0957, 17.1225, 17.1564, 17.1831, 17.2171, 
17.2438, 17.277, 17.3037, 17.3376, 17.3644, 17.3982, 17.425, 
17.4582, 17.4849, 17.5189, 17.5455, 17.5795, 17.6063, 17.6402, 
17.667, 17.7001, 17.7276, 17.7608, 17.7883, 17.8214, 17.849, 
17.882, 17.9089, 17.9428, 17.9695, 18.0027, 18.0302, 18.0633, 
18.0909, 18.124, 18.1516, 18.2114, 18.2721, 18.3328, 18.3935, 
18.4533, 18.514, 18.5747, 18.6354, 18.696, 18.7567, 18.8174, 
18.8773, 18.938, 18.9986, 18, 17.9735, 17.947, 17.9205, 17.894, 
17.8675, 17.841, 17.8145, 17.788, 17.7615, 17.735, 17.7085, 17.682, 
17.6555, 17.629, 17.6025, 17.576, 17.5495, 17.523, 17.4965, 17.47, 
17.4435, 17.417, 17.3905, 17.364, 17.3375, 17.311, 17.2845, 17.258, 
17.2315, 17.205, 17.1785, 17.152, 17.1255, 17.099, 17.0725, 17.046, 
17.0195, 16.993, 16.9665, 16.94, 16.9135, 16.887, 16.8605, 16.834, 
16.8075, 16.781, 16.7545, 16.728, 16.7015, 16.675, 16.6485, 16.622, 
16.5955, 16.569, 16.5425, 16.516, 16.4895, 16.463, 16.4365, 16.41, 
16.3835, 16.357, 16.3305, 16.304, 16.2775, 16.251, 16.2245, 16.198, 
16.1715, 16.145, 16.1185, 16.092, 16.0655, 16.039, 16.0125, 15.986, 
15.9595, 15.933, 15.9065, 15.88, 15.8535, 15.827, 15.8005, 15.774, 
15.7475, 15.721, 15.6945, 15.668, 15.6415, 15.615, 15.5885, 15.562, 
15.5355, 15.509, 15.4825, 15.456, 15.4295, 15.403, 15.3765, 15.35, 
15.3235, 15.297, 15.2705, 15.244, 15.2175, 15.191, 15.1645, 15.138, 
15.1115, 15.085, 15.0585, 15.032, 15.0055, 14.979, 14.9525, 14.926, 
14.8995, 14.873, 14.8465, 14.82, 14.7935, 14.767, 14.7405, 14.714, 
14.6875, 14.661, 14.6345, 14.608, 14.5815, 14.555, 14.5285, 14.502, 
14.4755, 14.449, 14.4225, 14.396, 14.3695, 14.343, 14.3165, 14.29, 
14.2635, 14.237, 14.2105, 14.184, 14.1575, 14.131, 14.1045, 14.078, 
14.0515, 14.025, 4, 4.0397, 4.0794, 4.1191, 4.1588, 4.1985, 4.2382, 
4.2779, 4.3176, 4.3573, 4.397, 4.4367, 4.4764, 4.5161, 4.5558, 
4.5955, 4.6352, 4.6749, 4.7146, 4.7543, 4.794, 4.8337, 4.8734, 
4.9131, 4.9528, 4.9925, 5.0322, 5.0719, 5.1116, 5.1513, 5.191, 
5.2307, 5.2704, 5.3101, 5.3498, 5.3895, 5.4292, 5.4689, 5.5086, 
5.5483, 5.588, 5.6277, 5.6674, 5.7071, 5.7468, 5.7865, 5.8262, 
5.8659, 5.9056, 5.9453, 5.985, 6.0247, 6.0644, 6.1041, 6.1438, 
6.1835, 6.2232, 6.2629, 6.3026, 6.3423, 6.382, 6.4217, 6.4614, 
6.5011, 6.5408, 6.5805, 6.6202, 6.6599, 6.6996, 6.7393, 6.779, 
6.8187, 6.8584, 6.8981, 6.9378, 6.9775, 7.0172, 7.0569, 7.0966, 
7.1363, 7.176, 7.2157, 7.2554, 7.2951, 7.3348, 7.3745, 7.4142, 
7.4539, 7.4936, 7.5333, 7.573, 7.6127, 7.6524, 7.6921, 7.7318, 
7.7715, 7.8112, 7.8509, 7.8906, 7.9303, 7.97, 8.0097, 8.0494, 
8.0891, 8.1288, 8.1685, 8.2082, 8.2479, 8.2876, 8.3273, 8.367, 
8.4067, 8.4464, 8.4861, 8.5258, 8.5655, 8.6052, 8.6449, 8.6846, 
8.7243, 8.764, 8.8037, 8.8434, 8.8831, 8.9228, 8.9625, 9.0022, 
9.0419, 9.0816, 9.1213, 9.161, 9.2007, 9.2404, 9.2801, 9.3198, 
9.3595, 9.3992, 9.4389, 9.4786, 9.5183, 9.558, 9.5977, 9.6374, 
9.6771, 9.7168, 9.7565, 9.7962, 9.8359, 9.8756, 9.9153, 10, 12, 
11.9017, 11.8034, 11.7051, 11.6068, 11.5085, 11.4102, 11.3119, 
11.2136, 11.1153, 11.017, 10.9187, 10.8204, 10.7221, 10.6238, 
10.5255, 10.4272, 10.3289, 10.2306, 10.1323, 10.034, 9.9357, 
9.8374, 9.7391, 9.6408, 9.5425, 9.4442, 9.3459, 9.2476, 9.1493, 
9.051, 8.9527, 8.8544, 8.7561, 8.6578, 8.5595, 8.4612, 8.3629, 
8.2646, 8.1663, 8.068, 7.9697, 7.8714, 7.7731, 7.6748, 7.5765, 
7.4782, 7.3799, 7.2816, 7.1833, 7.085, 6.9867, 6.8884, 6.7901, 
6.6918, 6.5935, 6.4952, 6.3969, 6.2986, 6.2003, 6, 6, 6, 6, 6, 
6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6)), .Names = c("x1", "y1"), class = "data.frame", row.names = c(NA, 
-1114L))

我需要分类的数据框如下所示:

test2 <- structure(list(x2 = c(10L, 15L, 7L, 14L, 15L), y2 = c(8, 5, 14, 
7.5, 12)), .Names = c("x2", "y2"), class = "data.frame", row.names = c(NA, 
-5L))

这些可以通过以下方式显示:

library(ggplot2)
ggplot(test, aes(x=x1, y=y1)) + 
  geom_point(color="gray") + 
  geom_point(data=test2, aes(x=x2,   y=y2), color="red")      

有些点在线上或非常接近线,如本例所示。有谁知道如何尝试将红点分类到他们属于哪个区域?我的想法是将回归分别拟合到每一行,然后用它来决定点落入哪个区域,但我必须在x和y方向上进行。此外,我的一些线非常非线性,所以这可能很难做到。也许有人对我如何处理这个问题有了另一个想法。 谢谢!

1 个答案:

答案 0 :(得分:0)

这是我一直在想的匹配两个表格的方法。我看到你编辑了这个问题,所以这可能没用。也许您可以使用此示例,无论如何发现它对您的数据有用。

DT <- data.table(na.omit(test))
q <- DT[, lapply(.SD, quantile)]

breaks_x1 = c(4.9, round(q[,x1])[2:5])
breaks_y1 = round(q[,y1])

# Convert numeric variables in factors, using quantiles as breaks
DT$fc_x1 <- cut(DT$x1, breaks = breaks_x1, labels = c("a", "b", "c", "d"), rigth = TRUE)
DT$fc_y1 <- cut(DT$y1, breaks = breaks_y1, labels = c("a", "b", "c", "d"), rigth = TRUE)

# Create label combination of both variables
DT$label <- paste0(DT$fc_x1, DT$fc_y1)
table(DT$label)

test2$fc_x2 <- cut(test2$x2, breaks = breaks_x1, labels = c("a", "b", "c", "d"), rigth = TRUE)
test2$fc_y2 <- cut(test2$y2, breaks = breaks_y1, labels = c("a", "b", "c", "d"), rigth = TRUE)

test2$label <- paste0(test2$fc_x2, test2$fc_y2)

test2
# x2   y2 fc_x2 fc_y2 label
# 1 10  8.0     a     b    ab
# 2 15  5.0     c     a    ca
# 3  7 14.0     a     c    ac
# 4 14  7.5     c     b    cb
# 5 15 12.0     c     c    cc

tst <- DT[,j=list(x1, 
                y1, 
                label = as.factor(label)
                )]

test2 <- data.table(melt(test2)[3:5])
setkey(test2, label, value)
setkey(tst, label)

datapoly <- merge(tst, test2, by = "label")

ggplot(datapoly, aes(x=x1, y=y1)) + 
  geom_polygon(aes(fill=value, group=label))

check out the documentation of geom_polygon

enter image description here