问题
I have 4 variables x1,x2 y1,y2 (365 values for each variable). I want to plot the 2d kernel density with specific contour levels. I need to overlay the density plots (x1 vs y1) and (x2 vs y2).
x1 <- c(772.522, 1806.75, 2388.73, 2619.04, 2695.6, 2747.14, 2772.58,
2773.86, 2812.93, 3338.98, 3299.18, 3269.85, 3179.74, 3185.36,
3274.99, 3391.08, 3541.91, 3563.56, 3551.63, 3626.92, 3602.07,
3535.31, 3482.09, 3567.54, 3502.1, 3440.78, 3437.95, 3722.05,
3702.45, 3636.89, 3565.1, 3485.51, 3398.42, 3311, 3231.97, 3129.51,
3055.22, 2968.45, 3435.38, 3605.31, 3468.35, 3845.2, 3858.71,
4388.68, 5056.55, 5601.96, 5968.48, 6033.75, 5938.22, 5807.13,
5671.36, 5612.84, 5475.63, 5329.19, 5179.73, 5239.82, 5264.78,
5553.4, 5478.35, 5352.85, 5227.08, 5213.33, 5160.05, 5399.89,
5554.96, 5592.91, 5541.88, 5517.83, 5614.53, 5522.72, 5410.01,
5289.24, 5154.86, 5014.21, 4868.89, 4732.69, 4608.15, 4457.99,
4299.06, 4142.57, 3991.74, 3841.69, 3695.19, 3552.06, 3436.21,
3308.64, 3178.24, 3056.39, 2938.67, 2824.59, 2714.82, 2610.78,
2515.79, 2424.83, 2346.9, 2274.12, 2202.44, 2132.47, 2068.17,
1986.15, 1905.02, 1828.44, 1754.68, 1685.86, 1621.62, 1560.92,
1504.27, 1450.55, 1400.78, 1352.88, 1304.74, 1257.36, 1219.04,
1213.48, 1202.94, 1423.37, 1542.41, 1494.66, 1482.53, 1599.09,
1544, 1482.98, 1446.54, 1395.88, 1346.6, 1295.43, 1248.17, 1206.5,
1161.74, 1142.75, 1304.01, 1261.43, 1221.17, 1339.85, 1382.48,
1333.32, 1298.32, 1269.32, 1259.52, 1236.89, 1268.37, 1327.74,
1459.69, 1451.84, 1418.96, 1390, 1609.57, 1638.19, 1610.33, 1624.47,
1575.08, 1526.3, 1487.86, 1474.29, 1497.28, 1457.82, 1444.52,
1448.25, 1458.49, 1496.27, 1534.7, 1593.66, 1636.95, 1632.44,
1660.17, 1738.57, 1765.32, 1784.72, 2015.57, 2050.61, 2051.55,
2045.69, 2044.79, 2050.25, 2038.62, 2016.73, 1996.23, 1986.9,
1963.9, 1929.55, 1886.8, 1834.12, 1780.86, 1732.32, 1680.39,
1624.13, 1568.53, 1519.12, 1474.84, 1428.67, 1380.09, 1334.61,
1290.76, 1247.5, 1212.04, 1183.16, 1152.2, 1171.52, 1130.61,
1092.57, 1091.14, 1054.6, 1020.15, 988.19, 1027.7, 1014.29, 979.729,
947.145, 915.957, 1002.37, 1161.34, 1130.55, 1168.49, 1126.99,
1086.23, 1048.46, 1011.48, 976.161, 942.963, 968.045, 1072.01,
1075.4, 1059.16, 1043.81, 1176.16, 1140.94, 1101.78, 1078.93,
1043.95, 1004.95, 968.521, 934.568, 904.955, 878.469, 849.94,
821.994, 795.893, 770.1, 745.538, 722.857, 701.089, 680.118,
660.585, 667.87, 666.708, 646.888, 626.794, 607.768, 591.769,
635.32, 738.938, 717.112, 732.378, 891.413, 1165.41, 1137.85,
1345.26, 1373.03, 1341.85, 1381.03, 1332.81, 1279.92, 1261.64,
1448.94, 1417.41, 1399.06, 1365.79, 1312.99, 1262.5, 1215.59,
1173.54, 1130.01, 1322.27, 1411.67, 1357, 1304.07, 1252.96, 1204.73,
1159.53, 1116, 1081.3, 1042.57, 1003.76, 967.089, 932.187, 897.657,
864.375, 832.293, 801.206, 771.326, 742.13, 716.694, 690.45,
664.076, 639.827, 617.01, 593.567, 570.818, 551.133, 593.432,
833.715, 871.919, 845.388, 865.802, 937.158, 972.532, 1030.36,
1006.08, 974.112, 937.399, 902.049, 872.061, 886.442, 892.396,
859.156, 825.958, 793.783, 762.704, 758.36, 999.93, 967.713,
961.368, 1012.97, 998.855, 1197.95, 1163.77, 1122.32, 1213.45,
1302.05, 1281.74, 1254.06, 1204.14, 1155.98, 1109.55, 1064.83,
1021.78, 980.367, 940.548, 925.483, 1144.38, 1125.92, 1109.17,
1222.15, 1503.71, 2656.42, 2550.13, 2446.94, 2358.74, 2263.33,
2171.81, 2248.6, 2316.71, 2675.05, 3015.03, 3716.48, 4441.43,
4742.74, 5476.79, 5313.57, 5106.1, 5178.79, 5160.45, 5020.48,
4825.68, 4730.04)
y1 <- c(0.127958331257105, 0.010291666626775, 0.0578749990284753, 0.830833333233992,
-0.0829583332330609, -0.217708332619319, 0.172125002286824, 0.232208333676681,
0.235375001948948, 0.0380416669261952, 0.0393333347359051, -0.0440416663574676,
0.162666665079693, -0.0932500026344011, -0.0905833330471069,
-0.305250000208616, 1.0349166871359, 0.334833333579202, -0.0301250003588696,
-0.175166667904705, -0.0697083329238618, 0.824125001827876, -0.532083340920508,
0.233000000123866, 0.0752083340097063, 0.409375000745058, 0.114333332865499,
0.359583331989901, -0.189749999437481, -0.164124998962507, -0.250208334065974,
0.694499998974303, -0.00312500035700699, 0.210833334363997, -0.0586666659607242,
0.305125000498568, 0.188458332403873, -0.101833333649362, 0.09737500102104,
0.273249999930461, -0.0283333340194076, 0.320541665268441, -0.0570416667421038,
-0.16370833478868, 0.0965000004313576, 0.156541665977178, 0.000791666388977319,
-0.17350000096485, 0.204625002418955, -0.175041667728995, -0.776166667540868,
0.0604166665192073, -0.0879583329757831, 0.357666667240361, 0.425541667888562,
-0.0276250006475796, 0.116624999713774, 0.044666666809159, -0.0109583338732288,
0.398333337565418, 0.201500000820185, -0.273708331709107, -0.126250000049671,
0.223624998082717, -0.0117499992872278, -0.0997916681614394,
0.121583334170282, 0.0962499987799674, -0.17191666799287, 0.002666666599301,
-0.340916665426145, 0.132625000396123, 0.32058333295087, 0.254250001162291,
0.372083335435794, -0.0369166672850649, 0.662124995142221, -0.0916666652386387,
0.0278750000870787, 0.0751666669190551, 0.620958338181178, 0.751416672021151,
-0.130499999620952, 0.170041667142262, 0.691666666107873, -0.0391250009512684,
0.294833332921068, -0.0795000011567026, 0.115291667714094, 0.0676250006072223,
0.318208330931763, -0.311458331843217, 0.45366666217645, 0.232166665392773,
0.117749998811632, 0.207750001301368, 0.92275000611941, -0.272541665161649,
0.103125000217309, 0.220291670741669, -0.191500000655651, 1.05833334475756,
0.671833337595065, -0.0487916663405485, -0.0473333336703945,
-0.169916665491958, -0.100500000247848, 0.0271666669577826, -0.10191666687994,
0.0568750000869234, 0.14375000144355, 0.108666666705782, 0.388583331524084,
-0.147958333914479, -0.103041666346447, -0.491375003010035, 0.0465833337899918,
0.286458336282521, 0.00633333355654031, 0.0260416660748888, -0.112708333239425,
-0.548541671286027, 0.0103333332614663, 0.148666666975866, -0.157583331689239,
0.325874996837229, -0.143708332757039, 0.0945833313356464, 0.0853333330742316,
0.313833336035411, -0.352624999048809, -0.136625000392087, -0.29474999755621,
-0.549458327392737, -0.0799166670185514, -0.0107916667620884,
-0.169333333459993, 0.321541666053236, 0.07195833309864, 0.146708333787198,
-0.246458334848285, 0.368250001221895, -0.159666667692363, -0.00275000064478566,
-0.0460416663748523, -0.138958334340714, -0.0874166679180538,
-0.0167500003784274, 0.091583332628943, 0.00845833330337579,
-0.0542083333760578, 0.112666667555459, -0.138541666480402, 0.259916665653388,
0.0581666673533618, -0.134541667697097, 0.525916664550702, 0.0101249999473415,
-0.127000000327826, -0.0889166663400829, -0.190124999731779,
-0.108375000612189, -0.107916666815678, 0.0988750007624428, 0.0848750000974784,
0.0244583335976737, -0.0702916663188565, -0.0600416688297022,
0.0206666665617377, 0.329208332424362, -0.0249166667636018, -0.167916666561117,
0.11137499815474, 0.00529166660271585, -0.412708333383004, 0.155208332464099,
0.322999999547998, -0.153541666455567, -0.0445416663618137, 0.0242500004387693,
-0.115666666689018, 0.0627916665980592, 0.10774999926798, -0.242875003643955,
-0.1862083322679, 0.0298750002645344, -0.059916666985373, -0.0553333335216545,
0.124124999691655, 0.215458335238509, -0.0642499998599912, -0.0367083334034154,
0.203250000505553, -0.0517083338151375, -0.0830416663084179,
-0.033833333698567, 0.272166667544904, 0.294208334758878, -0.234416666751107,
0.0510000000552585, -0.0260000005364418, 0.00383333330197881,
0.214041665196419, 0.212249997537583, -0.0273749998110967, 0.0852083338735004,
-0.133291667327285, -0.15349999970446, -0.0748333332982535, -0.0968749993480742,
0.0880833331029862, 0.190416667843238, -0.00887500051370201,
-0.0115416667006987, 0.149958331448336, -0.274749999245008, -0.0932916667855655,
0.109999999869615, -0.135416666356226, 0.0456666671185909, 0.135458334514018,
-0.073291666728134, 0.0852083340287209, 0.0665000005683396, 0.104958332454165,
-0.0821666670963168, -0.168583333181838, 0.178333333072563, 0.0781666664018606,
-0.175666667210559, -0.0343750003861108, 0.0142083335570836,
-0.0451250005474625, -0.154000000096858, -0.0315833336208016,
-0.0986250000860309, 0.201541664127338, -0.000624999937523777,
-0.0668333338884016, -0.0365833334314326, 0.0162083323860619,
-0.161374998899798, -0.0683333337462197, 0.0342499999824213,
-0.0376666667483126, -0.13674999990811, 0.0712083332861463, -0.0789166667188207,
0.0838333335850621, -0.107625000178814, -0.15395833303531, 0.151750000969817,
0.0107083340020229, 0.0111666666537834, 0.0764583332881254, 0.12216666713357,
-0.135750001917283, -0.139166665884356, -0.0763333337381482,
0.0223750005534384, 0.239708331103126, -0.121791667304933, 0.183583331371968,
-0.173791667446494, -0.00875000042530398, -0.107416666268061,
-0.00929166671994608, 0.0561666658128767, 0.082166666785876,
-0.0237500001627874, -0.048374999819013, 0.17375000162671, -0.15087499966224,
0.187791665395101, -0.0918750003135453, 0.309750000635783, -0.231125000243386,
-0.14383333416481, -0.0552083337291454, -0.121250000433065, 0.202124998904765,
-0.193333331495523, -0.0752083341746281, -0.153416666667908,
-0.0242500006376455, 0.0107499997441967, 0.0742916671248774,
-0.0477500005896824, -0.00087499994939814, -0.120625000757476,
0.22333333392938, 0.0522916664000756, -0.0239999999369805, 0.413791667670012,
0.00141666718991473, 0.162708333072563, 0.0484583335734593, 0.0710833334984879,
0.078208333812654, -0.0702916664692263, -0.108500000399848, -0.180708333849907,
0.123083333640049, 0.0157916666357778, 0.0192083331833904, -0.205250000581145,
0.0680416667601094, 0.0161666665517259, -0.11483333290865, -0.173625001683831,
-0.0131666665741553, -0.130791667072723, 0.209041668102145, -0.0475416670863827,
-0.101625000592321, -0.0217083335834711, 0.0751250004007791,
-0.0733333341777325, 0.0290416674300407, -0.136833332479, -0.0747916662755112,
-0.0304166664670144, 0.0384583333798219, -0.0781250001552204,
0.0489166672729577, 0.000500000169267878, -0.14054166796753,
0.0298750003178914, 0.00916666674796337, 0.0164583334699273,
0.0552083333604969, 0.0388333338196389, 0.359333331075807, 0.205291667332252,
-0.026708333355297, -0.0674583336221986, 0.0282916666183155,
-0.0927500004569689, -0.0379166668280959, -0.0953750004215787,
0.0110416668661249, -0.120208332935969, 0.0384999999660067, -0.0578333336549501,
0.0397500003067156, 0.0279166665568482, -0.0609166669504096,
0.104874998796731, -0.156874999403954, -0.0550833336698512, 0.195958332469066,
0.055291667037333, 0.0537499998608837, 0.145833333333333, 0.0199999999992239,
0.0791666666045785, -0.0392083331826143, 0.306416667997837, -0.00125000059294204,
0.124166667150954, -0.0162083334774555, 0.141874998798206, -0.0859166665468365,
-0.185750000178814, 0.0495833333213037)
x2 <- c(307.991, 460.697, 579.639, 1297.73, 2091.27, 3334.57, 3675.05,
3772.43, 3675.89, 3604.88, 3584.83, 3669.77, 3649.38, 3546.33,
3425.51, 3306.32, 3194.85, 3080.73, 2973.95, 2871.36, 2759.01,
2653.29, 2548.64, 2470.45, 2399.17, 2443.32, 2642.11, 2708.22,
2811.78, 2907.94, 3031.58, 3127.1, 3160.46, 3210.85, 3181.56,
3243.83, 3712.01, 3913.5, 3927.51, 3958.53, 3920.48, 3864.41,
3796.78, 3722.65, 3691.73, 3644.18, 3543.42, 3438.32, 3330.14,
3220.07, 3109.24, 3004.57, 2895.68, 2787.51, 2681.53, 2578.11,
2477.52, 2379.95, 2813.87, 2788.22, 2728.48, 2756.85, 2786.68,
2694.65, 2608.24, 2597.77, 2545.36, 2491.73, 2412.97, 2336.46,
2271.19, 2188.86, 2108.19, 2040.78, 1986.68, 1936.91, 1878.75,
1806.1, 1738.22, 1677.78, 1629.61, 1576.72, 1522.31, 1468.47,
1415.22, 1360.73, 1310.14, 1263.29, 1220.5, 1186.29, 1176.45,
1146.52, 1296.16, 1402.02, 1400.11, 1564.91, 1585.36, 1550.73,
1527.26, 1554.59, 1681.56, 1809.45, 1922.11, 1888.08, 1883.8,
1838.53, 1792.08, 1752.16, 1755.79, 1801.08, 1750.14, 1704.65,
1660.78, 1738.31, 1814.29, 1946.35, 1915.42, 1874.03, 1837.08,
1797.03, 1745.39, 1692.97, 1638.4, 1582.78, 1528, 1482.9, 1446,
1392.06, 1368.92, 1336.07, 1295.59, 1252.26, 1219.42, 1217.08,
1189.72, 1160.78, 1136.55, 1102.22, 1069.61, 1046.33, 1042.26,
1049.2, 1077.69, 1137.23, 1279.42, 1384.82, 1535.59, 1751.06,
1776.16, 1795.9, 1942.66, 2397.41, 3508.54, 3446.5, 3360.68,
3272.21, 3181.58, 3183.02, 3075.52, 2966.5, 2869.19, 2861.11,
2968.42, 3074.72, 2981.29, 2918.92, 2917.28, 2839.04, 2769.58,
2867.63, 3091.58, 2993.72, 2907.2, 2821.7, 2742.23, 3034.28,
3000.26, 2992.62, 2916.74, 3065.56, 3032.59, 3069.44, 3078.66,
3155.65, 3345.97, 3270.34, 3191.47, 3111.74, 3031.16, 2946.79,
2871.31, 2786.59, 2712.88, 2626.39, 2538.42, 2452.23, 2536.5,
2446.21, 2359.14, 2427.6, 2337.26, 2268.88, 2239.2, 2159.32,
2079.14, 2017.22, 2101.43, 2035.56, 1974.59, 1963.55, 2463.37,
2592.44, 2496.95, 2406.56, 2399.59, 2719.11, 2627.14, 2532.03,
2441.72, 2355.8, 2273.24, 2212.13, 2131.78, 2054.68, 2021.56,
1944.85, 1871.6, 1822.82, 1763.29, 1694.74, 1629.67, 1569.39,
1511.37, 1454.11, 1400.78, 1350.58, 1320.89, 1524.41, 1844.56,
1984.72, 3024.6, 2953.2, 2836.92, 2725.89, 2620.15, 2518.29,
2421.03, 2328, 2237.87, 2152.21, 2071.36, 1994.57, 1923.34, 1965.91,
1906.98, 1910.02, 1870.62, 1815.72, 1748.49, 1702.61, 1739.4,
1785.07, 1873.86, 2378.29, 2494.53, 2612.01, 2858.16, 2788.6,
2696.15, 2610.24, 2520.25, 2431.5, 2343.59, 2259.04, 2176.11,
2096.57, 2019.08, 1944.45, 1872.69, 1803.19, 1737.54, 1673.17,
1609.63, 1587.49, 1669.8, 1657.65, 1657.05, 1594.35, 1532.6,
1475, 1416.77, 1360.61, 1306.33, 1253.97, 1203.53, 1155.17, 1114.4,
1075.4, 1034.59, 993.862, 957.333, 918.364, 880.908, 845.121,
814.763, 781.644, 749.727, 719.079, 689.992, 666.463, 658.674,
639.19, 617.655, 595.126, 573.268, 551.763, 530.933, 514.663,
493.969, 473.986, 454.894, 436.422, 418.687, 402.434, 404.804,
422.748, 411.777, 527.699, 511.651, 490.849, 536.02, 555.457,
532.754, 510.963, 490.056, 469.998, 450.755, 432.295, 414.587,
397.6, 381.307, 365.679, 350.69, 336.315, 328.3, 664.914, 1045.6,
1086.51, 1042.35, 999.99, 959.336, 922.295, 889.513, 854.952,
820.273, 802.777, 839.017, 809.869, 776.747, 744.953, 733.373,
1046.14, 1004.25, 963.686, 924.941)
y2<-c(-0.0143333336454816, -0.130041667725891, 0.205333333889333,
0.0751666662593683, -0.567708330228925, 0.00870833483835061,
0.108500000167017, -0.152333330673476, 0.0720833349041641, 0.0236249993322417,
-0.00183332874439657, 0.633374993999799, 0.0230833344782392,
0.17537499712004, 0.126000000241523, 0.0728333333196739, 0.24050000286176,
0.470958332220713, 0.00229166596060774, -0.110000000180056, 0.159374999910748,
0.165541665841981, 0.204583332020169, -0.173458332836162, -0.0836250004940666,
-0.207041666842997, 0.191458333438883, -0.231000000378117, -0.450666667272647,
0.000625000917352736, 0.0672916673744718, -0.0514583328040317,
0.447916670391957, -0.0139166663090388, -0.143041666325492, 0.0312916650048768,
-0.245958331235064, -0.329958332081636, 0.304333332712607, -0.0889166676594565,
-0.361833333348234, 0.0753333327205231, 0.695874998966853, 0.41166666833063,
-0.18824999841551, 0.0396249986952171, 1.06683334087332, 0.0413749999182376,
0.0123749998650358, 0.229791667860506, 0.549791666368643, -0.164916665758938,
-0.135374999294678, 0.273583333939314, -0.0588750006087745, 0.277958332871397,
-0.313208335389694, 0.689124989633759, 0.094624999522542, -0.269583333283663,
0.191708333479861, 0.664500000576178, -0.137416665190055, 0.642000003407399,
0.450583333459993, 0.0582499998854473, 0.336791667776803, 0.0301666681965192,
0.29837500040109, -0.201166667509824, 0.451166668285926, 0.224791664397344,
-0.0301666668674443, 0.363333332662781, -0.104125000148391, -0.342625002066294,
0.846166675289472, 0.054874999797903, 0.338583329808898, 0.218583332529912,
0.195208333122234, 0.4053333333383, -0.247833332667748, -0.146250000533958,
0.185249998471894, -0.584749998602395, 0.0963333349985381, 0.755874996383985,
0.496833334366481, 0.0993333334918134, -0.292916670752068, -0.0159166666465656,
0.00379166667698883, -0.0822083334545217, 0.0154583335776503,
-0.207333335032066, 0.0127083321179574, -0.174791666586922, -0.0755833332271626,
0.549208330145727, 0.0531666671546797, -0.142875001144906, -0.123333334001169,
-0.0901666677576334, 0.037708332509889, -0.0903749998348455,
-0.195166666681568, 0.60275000333786, -0.194000001375874, -0.0709583333227783,
-0.0380833333668609, -0.0306666662266556, -0.0302083338416802,
-0.266958334483206, 0.337416667491198, 0.065041667073577, -0.238083333087464,
-0.285499998989205, -0.214749998723467, 0.189666667991939, -0.00158333376748487,
-0.137958332993245, -0.0680416666873498, 0.0718749998292575,
0.119708333474894, 0.0152916668060546, -0.0815833330949924, -0.122666667215526,
-0.0450416663031016, -0.157999999200304, 0.0971250010964771,
-0.021208333215327, 0.359666664153337, 0.313999998693665, 0.920874993006388,
0.199916666451221, -0.229291666299105, 0.765291665991147, -0.136083332821727,
-0.216124999336898, -0.532875002051393, -0.0467916677395503,
-0.1334166660284, 0.110416666216527, -0.0878749993862584, 0.380500002764165,
-0.039708333836946, -0.498291670034329, 0.275583331162731, 0.0325000000642225,
-0.127666668326128, -0.184624999257115, 0.0896250005656232, 0.239999998981754,
0.213041666895151, -0.0553750009275973, -0.0588333333532015,
-0.0314583331734563, -0.135000001328687, 0.0435416668624384,
0.0690833330930521, -0.100416666731083, -0.0135416674311273,
0.0191666668833932, -0.205666666850448, -0.136083332511286, 0.108666666705782,
0.0719166641841487, -0.0831250000434617, -0.0324166665765612,
0.28095833491534, -0.172041665762663, 0.19295833275343, -0.217958332970738,
0.137416667576569, -0.103708333956699, 0.493125003452102, 0.0395833332634841,
0.0919583337381482, -0.0902499996591359, -0.251624997705221,
-0.163791666428248, -0.0290000005625188, -0.0968750004443185,
0.15054166658471, -0.0782916670044263, -0.13729166580985, -0.115583334118128,
-0.116708333060766, -0.03329166630283, -0.0244583334521546, -0.0325416672664384,
-0.109916667609165, -0.129333334043622, -0.166249999466042, -0.0641666658145065,
-0.0281249999146288, -0.000416666768918124, 0.0460833331938678,
-0.00325000062972928, 0.0692916658784573, 0.0606250003523504,
0.0774999997229315, -0.071875000372529, 0.0102500002346157, 0.124166666607683,
0.134333334164694, 0.178166668862104, -0.245958332593242, 0.170708333665971,
-0.0618749993154779, 0.0354166668791246, -0.0117500002719074,
0.0573333327968915, 0.142208333127201, -0.0767499993477638, -0.101708334404975,
-0.139833334367722, 0.0271666664242124, -0.046666666942959, 0.00725000006301949,
0.223541667995354, -0.146083333219091, 0.059000000396433, -0.178999999538064,
-0.109958333428949, 0.220708332955837, -0.11587499982367, -0.176041666728755,
0.0539166663462917, 0.0158333336682214, 0.0103333337465301, -0.145249998817841,
0.0191249998752028, -0.0938750004085402, 0.108333333240201, -0.000708333022582034,
-0.0461666665166073, -0.0394166663754731, 0.349708331748843,
0.0017500000152116, -0.00470833342599993, -0.00558333326383339,
-0.131083334175249, 0.0782916669268161, -0.0562916661583586,
0.157583333532481, -0.153416668064892, 0.0437916667627481, -0.00499999996100087,
0.126625000266358, 0.104541668260936, 0.105791667068843, -0.034624999971129,
-0.00670833386053952, -0.0937916667123015, -0.107750000276913,
-0.410083334892988, 0.0678333326165254, -0.04479166752814, -0.0690416670404375,
0.0705833329605715, 0.0290416665714777, 0.186249999019007, 0.0559583333670162,
0.114208333194256, -0.054666666740862, -0.0615833334159106, 0.0185416670477328,
0.0032500000767565, -0.0410000003563861, -0.0481250003213063,
0.026041666555102, -0.00766666660395761, -0.0269166667906878,
-0.129833333194256, 0.044458333014821, -0.128583333707259, 0.158416667642693,
-0.0620416668243706, 0.0518333335445883, -0.0877500008791685,
-0.079041665730377, -0.200249998519818, -0.0592916671497126,
-0.0573333334953835, -0.0637916669559975, 0.0492916666747381,
0.077958333849286, 0.084416666252461, 0.219999999467594, 0.0175416672379167,
-0.0620000003837049, -0.10549999990811, -0.129083334002644, 0.0092500001580144,
0.050750000407182, -0.21812499811252, 0.0312500000776102, -0.0962083325721323,
0.0855000005103648, -0.0114583335089264, -0.0704583334348475,
0.0142083335279798, -0.108124999639889, -0.0675416669497887,
0.151958333017925, 0.00258333289821167, 0.116416666695538, 0.0430000008394321,
-0.107999999769769, 0.214999997988343, -0.048625000345055, -0.076583333623906,
0.039708333555609, -0.0127500006735014, 0.0430000002185504, -0.116583333661159,
0.0247500000793176, -0.00983333304369201, -0.011666667201401,
0.00229166702289755, -0.066125000443814, 0.129791667646108, 0.239958334112695,
0.197666665655561, -0.0584583321372823, -0.112875000263254, -0.13483333401382,
-0.00395833318179939, -0.0888750000546376, -0.136249998196339,
0.0827499999043842, -0.0271666669286788, 0.109333333559334, -0.193041666100423,
0.0270833332372907, 0.0983333326876163, -0.120541666634381, 0.078583334106952,
-0.0511249999593322, -0.00595833334470323, -0.0132916665170342,
0.0407916669549498, 0.139541667420417, -0.154333331932624, 0.0679166668560356,
-0.146000001269082, -0.0325416667280175, -0.017791666793831,
-0.0484999995484638, -0.0911666670193275, -0.00124999980713862,
0.0328333332242134, 0.0209583333150173, -0.0675416666393479,
0.0457916669547558, -0.0136249999874659, 0.498083334416151, -0.0294999997471071,
0.00112500010194102, 0.0847083332676751, 0.0993749999712842,
0.0766666668932885, -0.161374999520679)
I have a function(contlevels) that uses the MASS package and calculates the density(kde2d) of the two time series and also gives the specific contour level densities. The function calculates the density and returns the cumulative densities for the specific contour levels.
#####################################################
contlevels <- function(x,y,xmin,xmax,ymin,ymax,clev){
#####################################################
dd <- kde2d(x,y,n=c(60,60),lims=c(xmin,xmax,ymin,ymax))
xx <- dd$x
yy <- dd$y
zz <- dd$z
zsort <- sort(zz,decreasing=T)
p <- zz/sum(zz)
ps <- sort(p, decreasing=T)
n <- length(zz)
pscum <- array(0,dim=n)
pscum[1]<-ps[1]
pscum
for (i in 2:n){
pscum[i]<-pscum[i-1]+ps[i]
}
nlev <- length(clev)
cumlev <- array(0,dim=nlev)
for (ilev in 1:nlev){
for (i in 1:(n-1)){
if(pscum[i] >= clev[ilev]){
zsect <- (clev[ilev] - pscum[i])/(pscum[i+1]-pscum[i])
cumlev[ilev] <- zsort[i] + zsect*(zsort[i+1]-zsort[i])
break
}
}
}
contlevels <- list(xx=xx,yy=yy,zz=zz,cumlev=cumlev)
}
##########################################################################
Followings are the plotting initials
xmin=0
xmax=10000
ymin=-1
ymax=1
clev <- c(0.5,0.7,0.8) ## these are the contour levels I need to plot.
Segregating the variables from the function
cl1<-contlevels(x1,y1,xmin,xmax,ymin,ymax,clev)
xx <- cl1$x
yy <- cl1$y
zz <- cl1$z
cumlev1 <- cl1$cumlev
cl2<-contlevels(x2,y2,xmin,xmax,ymin,ymax,clev)
xxx <- cl2$x
yyy <- cl2$y
zzz <- cl2$z
cumlev2 <- cl2$cumlev
Plotting the distribution
plot(x1,y1,pch=20,cex=1,xlim=c(xmin,xmax),ylim=c(ymin,ymax),xlab="X",ylab="Y")
for (ilev in length(clev):2){
.filled.contour(xx,yy,zz,levels=c(cumlev1[ilev],cumlev1[ilev-1]),col="red")
.filled.contour(xxx,yyy,zzz,levels=c(cumlev2[ilev],cumlev2[ilev-1]),col="white")
}
Contour plots
contour(xx,yy,zz,add=T,col="black",lwd=2,levels=cumlev1,labels=clev)
contour(xxx,yyy,zzz,add=T,col="grey",lwd=1,levels=cumlev2,drawlabels=F)
Running this code will result in the above graph. where the 2nd distribution(white color i.e. x2 vs y2) is overlayed over the 1st distribution (red color, x1,y1). The red color will only pop up if the 2nd distribution is less that 1st. However, I also need the other way around. If the 2nd distribution is greater than the 1st distribution, I want it to be colored blue. Could anyone of you help me with this?
来源:https://stackoverflow.com/questions/44903690/2d-kde2d-contour-plots-with-conditions