I have a huge data, please bear with me.
df <- structure(list(W = c(5216400.4123, 5399804.7349, 5595563.3087,
5792353.9932, 5993467.7466, 6189404.9279, 6380940.454, 6566630.3544,
6747453.6816, 6917820.9796, 7086201.8275, 7248213.5225, 5402700.4252,
5592654.9057, 5795404.8549, 5999223.7818, 6207520.1695, 6410455.1037,
6608831.1825, 6801152.8706, 6988434.1695, 7164886.0132, 7339280.46,
7507078.2886, 5589000.4397, 5785505.0748, 5995246.3993, 6206093.5662,
6421572.589, 6631505.2817, 6836721.9131, 7035675.3828, 7229414.6578,
7411951.0459, 7592359.0997, 7765943.0584, 5775300.4552, 5978355.2455,
6195087.9484, 6412963.3504, 6635625.0103, 6852555.4544, 7064612.6447,
7270197.897, 7470395.1463, 7659016.0859, 7845437.7356, 8024807.8293,
5961600.4694, 6171205.4145, 6394929.4952, 6619833.1358, 6849677.4283,
7073605.633, 7292503.372, 7504720.4062, 7711375.6354, 7906081.1161,
8098516.3724, 8283672.5987, 6147900.4816, 6364055.583, 6594771.0412,
6826702.9212, 7063729.8478, 7294655.8114, 7520394.1023, 7739242.9195,
7952356.1253, 8153146.1541, 8351595.0092, 8542537.3639, 6334200.5005,
6556905.7553, 6794612.5881, 7033572.7089, 7277782.2661, 7515705.9882,
7748284.8337, 7973765.4322, 8193336.6123, 8400211.1873, 8604673.6477,
8801402.1321, 6520500.5136, 6749755.9226, 6994454.1327, 7240442.494,
7491834.6868, 7736756.1617, 7976175.5621, 8208287.9459, 8434317.1037,
8647276.2228, 8857752.2827, 9060266.9029, 6706800.528, 6942606.0899,
7194295.6809, 7447312.2777, 7705887.1079, 7957806.3409, 8204066.2961,
8442810.4584, 8675297.5886, 8894341.2573, 9110830.9206, 9319131.6718,
6893100.5418, 7135456.2602, 7394137.2288, 7654182.0653, 7919939.5232,
8178856.5136, 8431957.0234, 8677332.969, 8916278.0804, 9141406.2918,
9363909.5543, 9577996.4404, 7079400.5588, 7328306.4296, 7593978.7729,
7861051.8503, 8133991.9462, 8399906.6912, 8659847.7579, 8911855.4821,
9157258.5684, 9388471.3277, 9616988.1903, 9836861.209, 7265700.5699,
7521156.5994, 7793820.3185, 8067921.639, 8348044.3652, 8620956.868,
8887738.4844, 9146377.9962, 9398239.0552, 9635536.362, 9870066.8286,
10095725.9764, 7377480.5806, 7636866.7018, 7913725.2471, 8192043.5082,
8476475.8166, 8753586.9712, 9024472.9255, 9287091.5011, 9542827.3494,
9783775.3823, 10021914.0099, 10251044.8357, 7452000.5846, 7714006.7682,
7993661.8669, 8274791.4235, 8562096.7846, 8842007.041, 9115629.2162,
9380900.5049, 9639219.5459, 9882601.398, 10123145.4642, 10354590.7474,
7526520.5952, 7791146.8356, 8073598.4841, 8357539.3348, 8647717.7534,
8930427.113, 9206785.5068, 9474709.5143, 9735611.7438, 9981427.4094,
10224376.9194, 10458136.6527, 7638300.6025, 7906856.9365, 8193503.4143,
8481661.2074, 8776149.2063, 9063057.2172, 9343519.9463, 9615423.0209,
9880200.0383, 10129666.4312, 10376224.1032, 10613455.5131, 7824600.6172,
8099707.1074, 8393344.9601, 8688530.993, 8990201.627, 9284107.395,
9571410.6772, 9849945.5329, 10121180.5245, 10376731.4677, 10629302.7393,
10872320.2844, 8010900.6306, 8292557.2766, 8593186.5077, 8895400.7788,
9204254.0428, 9505157.5709, 9799301.4089, 10084468.0455, 10362161.0109,
10623796.504, 10882381.3729, 11131185.0534, 8197200.6444, 8485407.4453,
8793028.0567, 9102270.5614, 9418306.465, 9726207.7477, 10027192.1371,
10318990.5604, 10603141.4991, 10870861.5368, 11135460.012, 11390049.8203,
8383500.6593, 8678257.6128, 8992869.6005, 9309140.3471, 9632358.8856,
9947257.9225, 10255082.8657, 10553513.0731, 10844121.988, 11117926.5692,
11388538.6484, 11648914.5899, 8569800.6735, 8871107.7799, 9192711.1487,
9516010.1349, 9846411.3046, 10168308.0987, 10482973.5998, 10788035.5824,
11085102.4738, 11364991.6051, 11641617.2821, 11907779.3576, 8756100.6891,
9063957.9534, 9392552.6936, 9722879.9204, 10060463.7195, 10389358.2746,
10710864.3279, 11022558.0956, 11326082.9668, 11612056.6477, 11894695.9209,
12166644.1258, 8942400.705, 9256808.1204, 9592394.2396, 9929749.7037,
10274516.1413, 10610408.4494, 10938755.0613, 11257080.6099, 11567063.455,
11859121.6771, 12147774.5594, 12425508.8956, 9128700.7203, 9449658.2901,
9792235.786, 10136619.4908, 10488568.5635, 10831458.6249, 11166645.792,
11491603.1215, 11808043.9427, 12106186.7106, 12400853.1962, 12684373.664,
9315000.7353, 9642508.4595, 9992077.3349, 10343489.2758, 10702620.9802,
11052508.7998, 11394536.5185, 11726125.6341, 12049024.431, 12353251.7462,
12653931.8307, 12943238.4324, 9501300.7465, 9835358.6307, 10191918.8807,
10550359.0623, 10916673.4022, 11273558.9758, 11622427.2509, 11960648.1479,
12290004.92, 12600316.7835, 12907010.4688, 13202103.2002, 9687600.7612,
10028208.7979, 10391760.4269, 10757228.8467, 11130725.8198, 11494609.1532,
11850317.9787, 12195170.6632, 12530985.4111, 12847381.8161, 13160089.1051,
13460967.9711), t = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L,
9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L,
12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L,
3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L,
6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L,
9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L,
12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L,
3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L,
6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L,
9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L,
12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L,
3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L,
6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L,
9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L,
12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L,
3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L,
6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L,
9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L,
12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L,
3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L,
6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L,
9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L,
12L), .Label = c("t_m75_tc", "t_m5_tc", "t_m25_tc", "t_p0_tc",
"t_p25_tc", "t_p5_tc", "t_p75_tc", "t_p10_tc", "t_p125_tc", "t_p15_tc",
"t_p175_tc", "t_p20_tc"), class = "factor"), p = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L,
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L,
6L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L,
7L, 7L, 7L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 9L,
9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 10L, 10L, 10L, 10L,
10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 11L, 11L, 11L, 11L, 11L,
11L, 11L, 11L, 11L, 11L, 11L, 11L, 12L, 12L, 12L, 12L, 12L, 12L,
12L, 12L, 12L, 12L, 12L, 12L, 13L, 13L, 13L, 13L, 13L, 13L, 13L,
13L, 13L, 13L, 13L, 13L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L,
14L, 14L, 14L, 14L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L,
15L, 15L, 15L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L,
16L, 16L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L,
17L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L,
19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 20L,
20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 21L, 21L,
21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 22L, 22L, 22L,
22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 23L, 23L, 23L, 23L,
23L, 23L, 23L, 23L, 23L, 23L, 23L, 23L, 24L, 24L, 24L, 24L, 24L,
24L, 24L, 24L, 24L, 24L, 24L, 24L, 25L, 25L, 25L, 25L, 25L, 25L,
25L, 25L, 25L, 25L, 25L, 25L, 26L, 26L, 26L, 26L, 26L, 26L, 26L,
26L, 26L, 26L, 26L, 26L, 27L, 27L, 27L, 27L, 27L, 27L, 27L, 27L,
27L, 27L, 27L, 27L), .Label = c("h_30", "h_27.5", "h_25", "h_22.5",
"h_20", "h_17.5", "h_15", "h_12.5", "h_10", "h_7.5", "h_5", "h_2.5",
"h_1", "h0", "h1", "h2.5", "h5", "h7.5", "h10", "h12.5", "h15",
"h17.5", "h20", "h22.5", "h25", "h27.5", "h30"), class = "factor"),
tt = c(-7.5, -5, -2.5, 0, 2.5, 5, 7.5, 10, 12.5, 15, 17.5,
20, -7.5, -5, -2.5, 0, 2.5, 5, 7.5, 10, 12.5, 15, 17.5, 20,
-7.5, -5, -2.5, 0, 2.5, 5, 7.5, 10, 12.5, 15, 17.5, 20, -7.5,
-5, -2.5, 0, 2.5, 5, 7.5, 10, 12.5, 15, 17.5, 20, -7.5, -5,
-2.5, 0, 2.5, 5, 7.5, 10, 12.5, 15, 17.5, 20, -7.5, -5, -2.5,
0, 2.5, 5, 7.5, 10, 12.5, 15, 17.5, 20, -7.5, -5, -2.5, 0,
2.5, 5, 7.5, 10, 12.5, 15, 17.5, 20, -7.5, -5, -2.5, 0, 2.5,
5, 7.5, 10, 12.5, 15, 17.5, 20, -7.5, -5, -2.5, 0, 2.5, 5,
7.5, 10, 12.5, 15, 17.5, 20, -7.5, -5, -2.5, 0, 2.5, 5, 7.5,
10, 12.5, 15, 17.5, 20, -7.5, -5, -2.5, 0, 2.5, 5, 7.5, 10,
12.5, 15, 17.5, 20, -7.5, -5, -2.5, 0, 2.5, 5, 7.5, 10, 12.5,
15, 17.5, 20, -7.5, -5, -2.5, 0, 2.5, 5, 7.5, 10, 12.5, 15,
17.5, 20, -7.5, -5, -2.5, 0, 2.5, 5, 7.5, 10, 12.5, 15, 17.5,
20, -7.5, -5, -2.5, 0, 2.5, 5, 7.5, 10, 12.5, 15, 17.5, 20,
-7.5, -5, -2.5, 0, 2.5, 5, 7.5, 10, 12.5, 15, 17.5, 20, -7.5,
-5, -2.5, 0, 2.5, 5, 7.5, 10, 12.5, 15, 17.5, 20, -7.5, -5,
-2.5, 0, 2.5, 5, 7.5, 10, 12.5, 15, 17.5, 20, -7.5, -5, -2.5,
0, 2.5, 5, 7.5, 10, 12.5, 15, 17.5, 20, -7.5, -5, -2.5, 0,
2.5, 5, 7.5, 10, 12.5, 15, 17.5, 20, -7.5, -5, -2.5, 0, 2.5,
5, 7.5, 10, 12.5, 15, 17.5, 20, -7.5, -5, -2.5, 0, 2.5, 5,
7.5, 10, 12.5, 15, 17.5, 20, -7.5, -5, -2.5, 0, 2.5, 5, 7.5,
10, 12.5, 15, 17.5, 20, -7.5, -5, -2.5, 0, 2.5, 5, 7.5, 10,
12.5, 15, 17.5, 20, -7.5, -5, -2.5, 0, 2.5, 5, 7.5, 10, 12.5,
15, 17.5, 20, -7.5, -5, -2.5, 0, 2.5, 5, 7.5, 10, 12.5, 15,
17.5, 20, -7.5, -5, -2.5, 0, 2.5, 5, 7.5, 10, 12.5, 15, 17.5,
20), hh = c(-30, -30, -30, -30, -30, -30, -30, -30, -30,
-30, -30, -30, -27.5, -27.5, -27.5, -27.5, -27.5, -27.5,
-27.5, -27.5, -27.5, -27.5, -27.5, -27.5, -25, -25, -25,
-25, -25, -25, -25, -25, -25, -25, -25, -25, -22.5, -22.5,
-22.5, -22.5, -22.5, -22.5, -22.5, -22.5, -22.5, -22.5, -22.5,
-22.5, -20, -20, -20, -20, -20, -20, -20, -20, -20, -20,
-20, -20, -17.5, -17.5, -17.5, -17.5, -17.5, -17.5, -17.5,
-17.5, -17.5, -17.5, -17.5, -17.5, -15, -15, -15, -15, -15,
-15, -15, -15, -15, -15, -15, -15, -12.5, -12.5, -12.5, -12.5,
-12.5, -12.5, -12.5, -12.5, -12.5, -12.5, -12.5, -12.5, -10,
-10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -7.5,
-7.5, -7.5, -7.5, -7.5, -7.5, -7.5, -7.5, -7.5, -7.5, -7.5,
-7.5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -2.5,
-2.5, -2.5, -2.5, -2.5, -2.5, -2.5, -2.5, -2.5, -2.5, -2.5,
-2.5, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 2.5, 2.5, 2.5, 2.5, 2.5, 2.5, 2.5, 2.5, 2.5,
2.5, 2.5, 2.5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 7.5, 7.5,
7.5, 7.5, 7.5, 7.5, 7.5, 7.5, 7.5, 7.5, 7.5, 7.5, 10, 10,
10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 12.5, 12.5, 12.5,
12.5, 12.5, 12.5, 12.5, 12.5, 12.5, 12.5, 12.5, 12.5, 15,
15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 17.5, 17.5, 17.5,
17.5, 17.5, 17.5, 17.5, 17.5, 17.5, 17.5, 17.5, 17.5, 20,
20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 22.5, 22.5, 22.5,
22.5, 22.5, 22.5, 22.5, 22.5, 22.5, 22.5, 22.5, 22.5, 25,
25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 27.5, 27.5, 27.5,
27.5, 27.5, 27.5, 27.5, 27.5, 27.5, 27.5, 27.5, 27.5, 30,
30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30), pChange = c(-36.9603395985828,
-34.7439172960321, -32.3781951432773, -30.0000000392759,
-27.5695610939649, -25.2016804880133, -22.8869934306931,
-20.6429501564101, -18.4577189167867, -16.3988477104845,
-14.3639825485445, -12.4060879418008, -34.7089231777299,
-32.4133428932464, -29.9631306906258, -27.5000000028702,
-24.982759663634, -22.5303119363876, -20.1329575059591, -17.8087697620382,
-15.5454946011909, -13.4130922883195, -11.3055534045631,
-9.27773397066822, -32.4575067375413, -30.082768511005, -27.5480662597272,
-25.000000017221, -22.3959582743917, -19.8589433581751, -17.3789215558468,
-14.974589416006, -12.6332702807612, -10.4273368770308, -8.24712417357041,
-6.14937995482153, -30.2060902852677, -27.7521941094277,
-25.1330017720295, -22.5000000339888, -19.8091568633966,
-17.1875748440126, -14.6248855936495, -12.1404090458039,
-9.72104595791446, -7.44158137752243, -5.18869498850033,
-3.02102592568144, -27.9546738487045, -25.4216197283948,
-22.7179373121271, -20.0000000362547, -17.2223554922816,
-14.5162062585492, -11.8708496834174, -9.30622873602633,
-6.80882162781682, -4.45582599644603, -2.13026579255384,
0.107328085331291, -25.7032574363111, -23.0910453534044,
-20.3028728618926, -17.5000000385206, -14.6355541030393,
-11.8448376755028, -9.11681373693055, -6.47204837670069,
-3.89659728805126, -1.47007052110743, 0.928163403392649,
3.23568204558746, -23.4518409429489, -20.7604709324913, -17.8878084007817,
-15.0000000129913, -12.0487527282989, -9.17346911179217,
-6.36277777715033, -3.63786802462598, -0.984372983331903,
1.51568489622367, 3.98659261988347, 6.36403604209831, -21.2004245196791,
-18.4298965720027, -15.4727439674661, -12.5000000188826,
-9.46195132455473, -6.50210058796172, -3.6087418536248, -0.803687660466381,
1.92785137456101, 4.50144034135001, 7.04502179407714, 9.4923900700299,
-18.949008080699, -16.0993222115141, -13.0576794906449, -10.0000000416929,
-6.8751499159766, -3.83073199524737, -0.85470586242384, 2.03049268919135,
4.84007565390208, 7.48719577439149, 10.103451003317, 12.6207440750002,
-16.6975916489697, -13.7687478147708, -10.6426150174491,
-7.50000001737204, -4.28834857749088, -1.15936348108484,
1.8993300478083, 4.86467301588778, 7.75230001662893, 10.4729512074329,
13.1618801618003, 15.749098076345, -14.4461751785688, -11.4381734289039,
-8.2275505901759, -5.00000002447191, -1.70154714595145, 1.51200509229367,
4.65336604505171, 7.69885337279645, 10.6645243334332, 13.4587066573933,
16.2203093480789, 18.8774520776898, -12.1947587794688, -9.10759903820312,
-5.81248614477539, -2.49999998685766, 0.885254237248392,
4.18337365600429, 7.40740194561595, 10.53303374179, 13.5767486357356,
16.4444620880177, 19.2787385621527, 22.0058060645328, -10.8439088911858,
-7.70925439749847, -4.3634474625498, -1.00000001287041, 2.43733506716831,
5.78619475942614, 9.05982354879595, 12.2335419201639, 15.324083242737,
18.2359153430087, 21.1137960702944, 23.8828184428618, -9.94334233686319,
-6.77702465958718, -3.39742166553716, 0, 3.47205562528221,
6.85474217379226, 10.1614379102301, 13.3672140455251, 16.4889729851691,
19.4302175391865, 22.3371677435973, 25.1341600948813, -9.04277570278022,
-5.84479490959099, -2.43139589994524, 0.999999964530837,
4.50677619306401, 7.92328961474518, 11.2630522704558, 14.5008862385618,
17.65386274452, 20.6245196833994, 23.5605394277767, 26.3855016695576,
-7.69192585558588, -4.44645028701369, -0.982357198383821,
2.49999997960674, 6.05885704111126, 9.52611073025193, 12.9154738543,
16.20139443748, 19.4011973551469, 22.4159729565176, 25.3955969661306,
28.26251406118, -5.44050941298024, -2.11587588301946, 1.43270724943368,
4.99999997975781, 8.64565844485543, 12.1974793060474, 15.6695098080378,
19.0355747810953, 22.3134216501983, 25.4017284137289, 28.4540261536177,
31.390868095154, -3.18909298608498, 0.214698500430437, 3.84777171900399,
7.49999998232584, 11.2324597893836, 14.8688478588816, 18.4235457714435,
21.8697551319615, 25.2256459476667, 28.3874838685232, 31.5124553108925,
34.5192221013328, -0.93767655435575, 2.54527287783788, 6.26283620549316,
9.99999994622222, 13.8192612112551, 17.5402164225922, 21.177581692552,
24.703935510623, 28.1378702668879, 31.3732392810203, 34.5708845346342,
37.6475760821333, 1.31373989066688, 4.87584724074345, 8.67790062914087,
12.4999999475818, 16.4060626137908, 20.211584962133, 23.9316176184945,
27.5381158626977, 31.0500945945686, 34.3589946886835, 37.6293137257467,
40.775930095563, 3.56515632723005, 7.20642159881506, 11.0929651059621,
14.9999999743196, 18.9928639969906, 22.8829535185927, 26.685653610904,
30.3722961736837, 33.9623188847861, 37.3447501386438, 40.6877428842301,
43.9042840860314, 5.81657278071209, 9.53699603423, 13.5080295429032,
17.4999999732622, 21.5796653306424, 25.5543220714269, 29.439689530804,
33.2064765318009, 36.8745432620148, 40.3305056695729, 43.7461721043463,
47.0326380825422, 8.06798923781962, 11.8675703910931, 15.9230939931377,
19.999999945618, 24.16646674768, 28.2256906109677, 32.1937255147541,
36.0406569032114, 39.7867675812361, 43.3162610409814, 46.8046013208372,
50.1609920983889, 10.3194056876762, 14.1981447805855, 18.3381584482061,
22.4999999638964, 26.7532681695515, 30.8970591589679, 34.9477614660749,
38.8748372419928, 42.6989918944149, 46.302016461938, 49.8630305167836,
53.2893460973168, 12.5708221339073, 16.5287191664524, 20.7532229334868,
24.9999999567965, 29.3400695249561, 33.5684276997172, 37.7017973666391,
41.709017592859, 45.6112162148446, 49.2877719082728, 52.9214596849349,
56.4177000962446, 14.8222385342158, 18.859293574072, 23.1682873813043,
27.499999967824, 31.9268709444106, 36.2397962537599, 40.4558333385042,
44.5431979582271, 48.5234405437337, 52.273527375152, 55.9798888965917,
59.5460540879215, 17.0736549768214, 21.1898679333522, 25.5833518339557,
29.9999999534732, 34.5136723106916, 38.9111648247215, 43.2098692547788,
47.3773783417225, 51.4356648980012, 55.2592827852321, 59.0383180864958,
62.6744081170616)), class = "data.frame", .Names = c("W",
"t", "p", "tt", "hh", "pChange"), row.names = c(NA, -324L))
I am trying to plot a colored contour map with the columns tt, hh and W, W being the z axis.
This is the code I am using::
ggplot(df, aes(x = tt, y = hh, z = W)) +
stat_contour(geom = "polygon", aes(fill = ..level..) ) +
geom_tile(aes(fill = W)) +
stat_contour(bins = 10) +
xlab("% change in temperature") +
ylab("% change in ppt") +
guides(fill = guide_colorbar(title = "W"))
This is the result I got:
What I want is the color to be continuous, as seen here I am following this example for my work.
What is going wrong over here?
The grid is not evenly spaced. One way to make an evenly spaced grid is to use interpolate using loess on an evenly spaced grid:
model <- loess(W ~ tt + hh, data = df)
create an evenly spaced grid using expand.grid:
new.data <- expand.grid(tt = seq(from = min(df$tt), to = max(df$tt), length.out = 500),
hh = seq(from = min(df$hh), to = max(df$hh), length.out = 500))
predict on new data using the model:
gg <- predict(model, newdata = new.data)
combine prediction and new data:
new.data = data.frame(W = as.vector(gg),
new.data)
and now the plot looks like:
ggplot(new.data, aes(x = tt, y = hh, z = W)) +
stat_contour(geom = "polygon", aes(fill = ..level..) ) +
geom_tile(aes(fill = W)) +
stat_contour(bins = 10) +
xlab("% change in temperature") +
ylab("% change in ppt") +
guides(fill = guide_colorbar(title = "W"))
You might also want to check some goodness of fit metric for loess
caret::RMSE(model$fitted, df$W)
#output
7498.393
using a narrower span could provide a better fit, especially if the data is not smooth:
model2 <- loess(W ~ tt + hh, data = df, span = 0.1)
caret::RMSE(model2$fitted, df$W)
#output
964.7582
ggplot(new.data2, aes(x = tt, y = hh, z = W)) +
stat_contour(geom = "polygon", aes(fill = ..level..) ) +
geom_tile(aes(fill = W)) +
stat_contour(bins = 10) +
xlab("% change in temperature") +
ylab("% change in ppt") +
guides(fill = guide_colorbar(title = "W"))
The difference is ever so slight
ggplot(new.data, aes(x = tt, y = hh, z = W)) +
geom_tile(aes(fill = W)) +
geom_contour(aes(x = tt, y = hh, z = W),
color = "red")+
geom_contour(data = new.data2,
aes(x = tt, y = hh, z = W),
color = "white", inherit.aes = FALSE)
EDIT: also check the great post by #Henrik which is linked by him in the comment. Especially the ?akima::interp function.
EDIT2: answer to the questions in comments:
To specify a different fill one can use
scale_fill_gradient
scale_fill_gradient2
scale_fill_gradientn
Here is an example of using scale_fill_gradientn with 5 colors based on quantiles:
v <- ggplot(new.data2, aes(x = tt, y = hh, z = floor(W))) +
geom_tile(aes(fill = W), show.legend = FALSE) +
stat_contour(bins = 10, aes(colour = ..level..)) +
xlab("% change in temperature") +
ylab("% change in ppt") +
guides(fill = guide_colorbar(title = "W")) +
scale_fill_gradientn(values = scales::rescale(quantile(new.data2$W)),
colors = rainbow(5))
I removed the polygon thing since it was below the geom_tile layer and was not visible.
To add direct labels:
library(directlabels)
direct.label(v, list("far.from.others.borders", "calc.boxes", "enlarge.box",
box.color = NA, fill = "transparent", "draw.rects"))
I have the following data frame
df<-structure(list(DATE = c("30/06/15", "23/06/15", "22/06/15", "21/06/15",
"18/06/15", "12/06/15", "09/06/15", "08/06/15", "02/06/15", "08/04/15",
"06/04/15", "05/04/15", "07/03/15", "06/03/15", "04/03/15", "03/03/15",
"02/03/15", "26/01/15", "25/01/15", "20/01/15", "19/01/15", "18/01/15",
"17/01/15", "16/01/15", "15/01/15", "14/01/15", "13/01/15", "12/01/15",
"11/01/15", "10/01/15", "09/01/15", "08/01/15", "07/01/15", "06/01/15",
"05/01/15", "04/01/15", "03/01/15", "02/01/15", "01/01/15", "31/12/14",
"30/12/14", "29/12/14", "28/12/14", "27/12/14", "26/12/14", "25/12/14",
"27/08/14", "26/08/14", "25/08/14"), TICKETS = c(17L, 15L, 22L,
16L, 15L, 10L, 18L, 12L, 20L, 20L, 19L, 12L, 16L, 9L, 20L, 18L,
15L, 19L, 13L, 18L, 21L, 27L, 17L, 17L, 18L, 18L, 21L, 18L, 22L,
20L, 16L, 15L, 23L, 15L, 17L, 12L, 20L, 16L, 9L, 13L, 21L, 16L,
16L, 14L, 10L, 7L, 15L, 12L, 14L), COLUMN = c(17L, 15L, 22L,
16L, 15L, 10L, 18L, 12L, 20L, 20L, 19L, 12L, 16L, 9L, 20L, 18L,
15L, 19L, 13L, 18L, 21L, 27L, 17L, 17L, 18L, 18L, 21L, 18L, 22L,
20L, 16L, 15L, 23L, 15L, 17L, 12L, 20L, 16L, 9L, 13L, 21L, 16L,
16L, 14L, 10L, 7L, 15L, 12L, 14L), FLAG = c(1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1), MA = c(0.318405243036592, 0.163298743855817, 0.706171490988531,
0.240851993446204, 0.163298743855817, 0.153604587657018, 0.168145821955216,
0.158451665756417, 0.153604587657018, 0.163298743855817, 0.182687056253413,
0.172992900054615, 0.172992900054615, 0.158451665756417, 0.182687056253413,
0.177839978154014, 0.187534134352813, 0.153604587657018, 0.172992900054615,
0.168145821955216, 0.20207536865101, 0.226310759148007, 0.187534134352813,
0.221463681048607, 0.240851993446204, 0.303864008738394, 0.318405243036592,
0.33294647733479, 0.391111414527581, 0.39595849262698, 0.410499726925178,
0.420193883123976, 0.463817586018569, 0.478358820316767, 0.468664664117968,
0.439582195521573, 0.434735117422174, 0.381417258328782, 0.342640633533588,
0.323252321135991, 0.337793555434189, 0.328099399235391, 0.303864008738394,
0.284475696340797, 0.226310759148007, 0.163298743855817, 0.158451665756417,
0.177839978154014, 0.168145821955216)), .Names = c("DATE", "TICKETS",
"COLUMN", "FLAG", "MA"), row.names = c(412L, 316L, 302L, 288L,
246L, 162L, 120L, 106L, 22L, 102L, 74L, 60L, 87L, 73L, 45L, 31L,
17L, 351L, 337L, 267L, 253L, 239L, 225L, 211L, 197L, 183L, 169L,
155L, 141L, 127L, 113L, 99L, 85L, 71L, 57L, 43L, 29L, 15L, 1L,
426L, 418L, 405L, 392L, 378L, 364L, 350L, 374L, 360L, 346L), class = "data.frame")
Now, as can be seen, there are some dates where there is clearly a tendency to "group" together, for example december and january (date format is: dd/mm/yy).
Plainly, what I want is to find these groups of adjoining dates (i.e. 25- dec -2014 to 20 - jan - 2015 and june 21st to june 23rd 2015).
As long as there are two days adjoining each other I would count it as a "group". Once "groups" are named, I could filter the longest one, for example.
Idealy I would get something like:
structure(list(DATE = structure(c(47L, 36L, 35L, 34L, 31L, 24L,
20L, 18L, 4L, 17L, 13L, 10L, 15L, 12L, 8L, 6L, 3L, 40L, 37L,
33L, 32L, 30L, 29L, 28L, 27L, 26L, 25L, 23L, 22L, 21L, 19L, 16L,
14L, 11L, 9L, 7L, 5L, 2L, 1L, 49L, 48L, 46L, 45L, 44L, 42L, 39L,
43L, 41L, 38L), .Label = c("01/01/15", "02/01/15", "02/03/15",
"02/06/15", "03/01/15", "03/03/15", "04/01/15", "04/03/15", "05/01/15",
"05/04/15", "06/01/15", "06/03/15", "06/04/15", "07/01/15", "07/03/15",
"08/01/15", "08/04/15", "08/06/15", "09/01/15", "09/06/15", "10/01/15",
"11/01/15", "12/01/15", "12/06/15", "13/01/15", "14/01/15", "15/01/15",
"16/01/15", "17/01/15", "18/01/15", "18/06/15", "19/01/15", "20/01/15",
"21/06/15", "22/06/15", "23/06/15", "25/01/15", "25/08/14", "25/12/14",
"26/01/15", "26/08/14", "26/12/14", "27/08/14", "27/12/14", "28/12/14",
"29/12/14", "30/06/15", "30/12/14", "31/12/14"), class = "factor"),
TICKETS = c(17, 15, 22, 16, 15, 10, 18, 12, 20, 20, 19, 12,
16, 9, 20, 18, 15, 19, 13, 18, 21, 27, 17, 17, 18, 18, 21,
18, 22, 20, 16, 15, 23, 15, 17, 12, 20, 16, 9, 13, 21, 16,
16, 14, 10, 7, 15, 12, 14), COLUMN = c(17, 15, 22, 16, 15,
10, 18, 12, 20, 20, 19, 12, 16, 9, 20, 18, 15, 19, 13, 18,
21, 27, 17, 17, 18, 18, 21, 18, 22, 20, 16, 15, 23, 15, 17,
12, 20, 16, 9, 13, 21, 16, 16, 14, 10, 7, 15, 12, 14), FLAG = c(1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1), MA = c(0.318405243036592,
0.163298743855817, 0.706171490988531, 0.240851993446204,
0.163298743855817, 0.153604587657018, 0.168145821955216,
0.158451665756417, 0.153604587657018, 0.163298743855817,
0.182687056253413, 0.172992900054615, 0.172992900054615,
0.158451665756417, 0.182687056253413, 0.177839978154014,
0.187534134352813, 0.153604587657018, 0.172992900054615,
0.168145821955216, 0.20207536865101, 0.226310759148007, 0.187534134352813,
0.221463681048607, 0.240851993446204, 0.303864008738394,
0.318405243036592, 0.33294647733479, 0.391111414527581, 0.39595849262698,
0.410499726925178, 0.420193883123976, 0.463817586018569,
0.478358820316767, 0.468664664117968, 0.439582195521573,
0.434735117422174, 0.381417258328782, 0.342640633533588,
0.323252321135991, 0.337793555434189, 0.328099399235391,
0.303864008738394, 0.284475696340797, 0.226310759148007,
0.163298743855817, 0.158451665756417, 0.177839978154014,
0.168145821955216), GROUP = c(0, 1, 1, 1, 0, 0, 2, 2, 0,
0, 3, 3, 4, 4, 5, 5, 5, 6, 6, 0, 7, 7, 7, 7, 7, 7, 7, 7,
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 8,
8, 8)), .Names = c("DATE", "TICKETS", "COLUMN", "FLAG", "MA",
"GROUP"), row.names = c(NA, -49L), class = "data.frame")
Any ideas?
You can use diff and cumsum to generate a group number for any chosen length of gap that would separate a group. (Since the diff will be negative as your proceed backward in time as in this case, you need to use a negative threshold. Here I used -2, which then only allows gaps less than two days to be in same group, but you could use any (lower) number.
df$dt <- as.Date(df$DATE, format="%d/%m/%y")
df$grp <- cumsum( c(0, diff(as.numeric(df$dt)) < -2) )
> df$grp
[1] 0 1 1 1 2 3 4 4 5 6 6 6 7 7 7 7 7 8 8 9
[21] 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9
[41] 9 9 9 9 9 9 10 10 10