R: Filled Contour plot will not plot all of matrix - r

So my colleague and I made a matrix of my data in R (Using pipes: Package dplyr, magrittr) and plotted it in a filled.contour plot. But for some reason it will not plot my final two points.
Does anyone have a fix for this?
Here is my dputfor my data set/matrix :
structure(list(AvgDepth = c(5L, 5L, 15L, 5L, 15L, 25L, 5L, 15L,
25L, 35L, 5L, 15L, 25L, 35L, 5L, 15L, 25L, 35L, 45L, 5L, 15L,
25L, 35L, 45L, 55L, 5L, 15L, 25L, 35L, 45L, 55L, 65L, 5L, 15L,
25L, 35L, 45L, 55L, 65L, 75L, 5L, 15L, 25L, 35L, 45L, 55L, 65L,
75L, 85L, 95L), AvgMaxDepth = c(5L, 15L, 15L, 25L, 25L, 25L,
35L, 35L, 35L, 35L, 45L, 45L, 45L, 45L, 55L, 55L, 55L, 55L, 55L,
65L, 65L, 65L, 65L, 65L, 65L, 75L, 75L, 75L, 75L, 75L, 75L, 75L,
85L, 85L, 85L, 85L, 85L, 85L, 85L, 85L, 95L, 95L, 95L, 95L, 95L,
95L, 95L, 95L, 95L, 95L), MiddleOldG = c(-0.010339548,
-0.019087115, -0.013710817, -0.018843724, -0.016954714, -0.009270159,
-0.021369483, -0.020470118, -0.020793118, -0.038538718, -0.014960888,
-0.010406261, -0.014440231, -0.018794958, -0.020203279, -0.015599607,
-0.017050242, -0.024199387, -0.025290379, -0.026409047, -0.023564188,
-0.027233605, -0.031364833, -0.033557383, -0.036140136, -0.022472455,
-0.011018529, -0.01468673, -0.022262797, -0.027657312, -0.026680228,
-0.02738715, -0.025351831, -0.026051226, -0.021691668, -0.025787225,
-0.018350888, -0.019350486, -0.018076008, -0.011645933, -0.005070788,
-0.006843451, -0.001764863, -0.001371702, -0.002248032, -0.002476992,
0.00325166, 0.004070139, 0.033053363, 0.022264692), x = c(1,
1, 2, 1, 2, 3, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 5, 1, 2, 3,
4, 5, 6, 1, 2, 3, 4, 5, 6, 7, 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3,
4, 5, 6, 7, 8, 9, 10), y = c(1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5,
5, 5, 5, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8,
9, 9, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10
)), class = "data.frame", .Names = c("AvgDepth", "AvgMaxDepth",
"MiddleOldG", "x", "y"), row.names = c(NA,
-50L))
Here's what the matrix looks like:
http://i.stack.imgur.com/x6ngn.png
Here is the code
library(colorspace)
library(dplyr)
library(magrittr)
Descent<-read.csv(file.choose())
attach(Descent)
summary(Descent)
Descent %<>% mutate(x = (AvgDepth + 5)/10, y = (AvgMaxDepth + 5)/10)
z = matrix(NA, ncol = 10, nrow = 10)
z.middle = Descent %>% select(x, y, MiddleOldG)
for (i in 1:nrow(z.middle)) z[z.middle[i,1],z.middle[i,2]] = z.middle[i,3]
filled.contour(x = seq(5, 95, by = 10), y = seq(5, 95, by = 10), z, color.palette=diverge_hsv, xlab="Depth (m)", ylab="Maximum Depth (m)", levels = pretty(c(-0.04,0.04),24))
But it won't plot the 9,10 and the 10,10 data points in the matrix
They should be in the top right corner of the plot, but they aren't.
Any ideas why it isn't plotting those last two points?
Here is the data set: https://docs.google.com/spreadsheets/d/13BJQ9-EXr2wu5tZNqXSzdbg_cLUjVw65W7yAAwydf_8/edit?usp=sharing
Thanks

The filled.contour function needs 4 points to form a filled area. If you set adjacent points in the 9th column you then get the appropriate color in the upper right corner corresponding to the lower right "corner" of the matrix:
z[9:10,9] <- 0
filled.contour(x = seq(5, 95, by = 10), y = seq(5, 95, by = 10), z,
color.palette=diverge_hsv, xlab="Depth (m)", ylab="Maximum Depth (m)",
levels = pretty(c(-0.04,0.04),24))

Related

r summarize data by specific date for each year

I have a daily precipitation data for 50 years:
year
month
day
daily_prec
date
1960
1
1
3
1960-01-01
1960
1
2
0
1960-02-01
For each year, I want to get a sum of precipitation for some specific days of vegetation season in a new df like for example: from 1975-05-22 to 1975-07-15, from 1976-05-22 to 1976-06-15 etc.
year
start
end
sum
mean
1975
1975-22-05
1975-15-07
5
3
1976
1976-22-05
1976-15-07
2
1
Any suggestions how to do this?
You can write a function that filters the daily precipitation dataframe for the period delimited by the start and end dates, and apply that rowwise. In this example the dataframe is separate, but you can use the same da
daily_prec_df <- data.frame(
date = seq(as.Date("1960-01-01"), as.Date("1965-01-01"), by=1)
)
library(lubridate)
daily_prec_df <- daily_prec_df %>%
mutate(
year = year(date),
month = month(date),
day = day(date),
daily_prec = sample(1:10, replace = T, size = nrow(daily_prec_df))
)
get_period_aggregates <- function(start, end) {
res_df <- daily_prec_df %>%
filter(date >= start, date <= end) %>%
summarize(sum = sum(daily_prec), mean=mean(daily_prec))
}
aggregate_df <- data.frame(
year = c(1961, 1962),
start = as.Date(c("1961-05-22", "1962-05-22")),
end = as.Date(c("1961-06-22", "1962-06-22"))
)
aggregate_df %>%
rowwise() %>%
mutate(
sum = get_period_aggregates(start, end)$sum,
mean = get_period_aggregates(start, end)$mean
)
# A tibble: 2 x 5
# Rowwise:
year start end sum mean
<dbl> <date> <date> <int> <dbl>
1 1961 1961-05-22 1961-06-22 169 5.28
2 1962 1962-05-22 1962-06-22 206 6.44
You could paste an arbitrary year (like the current as shown here) to day-month and subset the data until "YYYY-07-15" using <=, then aggregate().
u <- as.POSIXct(substring(as.character(dat$date), 6), '%m-%d', tz='UTC') <=
as.POSIXct(paste0(strftime(Sys.Date(), '%Y'), '-07-15'), tz='UTC')
aggregate(prec ~ year, dat[u, ], function(x) c(sum=sum(x), mean=mean(x)))
# year prec.sum prec.mean
# 1 75 1014.000000 5.173469
# 2 76 926.000000 4.724490
# 3 77 968.000000 4.938776
Check:
sum(dat[dat$date <= as.Date('1975-07-15'), 'prec'])
# [1] 1014
mean(dat[dat$date <= as.Date('1975-07-15'), 'prec'])
# [1] 5.173469
Note: In aggregate(), instead of prec ~ year you also could use prec ~ substr(dat$date, 1, 4), so you even would not need to break the date into its individual components beforehand.
Edit
Four a time window, you consequently could do this:
u1 <- as.POSIXct(substring(as.character(dat$date), 6), '%m-%d', tz='UTC')
u <- u1 <= as.POSIXct(paste0(strftime(Sys.Date(), '%Y'), '-07-15'), tz='UTC') &
u1 >= as.POSIXct(paste0(strftime(Sys.Date(), '%Y'), '-06-15'), tz='UTC')
aggregate(prec ~ year, dat[u, ], function(x) c(sum=sum(x), mean=mean(x)))
# year prec.sum prec.mean
# 1 75 141.000000 4.548387
# 2 76 146.000000 4.709677
# 3 77 155.000000 5.000000
Data:
dat <- structure(list(date = structure(c(1826, 1827, 1828, 1829, 1830,
1831, 1832, 1833, 1834, 1835, 1836, 1837, 1838, 1839, 1840, 1841,
1842, 1843, 1844, 1845, 1846, 1847, 1848, 1849, 1850, 1851, 1852,
1853, 1854, 1855, 1856, 1857, 1858, 1859, 1860, 1861, 1862, 1863,
1864, 1865, 1866, 1867, 1868, 1869, 1870, 1871, 1872, 1873, 1874,
1875, 1876, 1877, 1878, 1879, 1880, 1881, 1882, 1883, 1884, 1885,
1886, 1887, 1888, 1889, 1890, 1891, 1892, 1893, 1894, 1895, 1896,
1897, 1898, 1899, 1900, 1901, 1902, 1903, 1904, 1905, 1906, 1907,
1908, 1909, 1910, 1911, 1912, 1913, 1914, 1915, 1916, 1917, 1918,
1919, 1920, 1921, 1922, 1923, 1924, 1925, 1926, 1927, 1928, 1929,
1930, 1931, 1932, 1933, 1934, 1935, 1936, 1937, 1938, 1939, 1940,
1941, 1942, 1943, 1944, 1945, 1946, 1947, 1948, 1949, 1950, 1951,
1952, 1953, 1954, 1955, 1956, 1957, 1958, 1959, 1960, 1961, 1962,
1963, 1964, 1965, 1966, 1967, 1968, 1969, 1970, 1971, 1972, 1973,
1974, 1975, 1976, 1977, 1978, 1979, 1980, 1981, 1982, 1983, 1984,
1985, 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995,
1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017,
2018, 2019, 2020, 2021, 2022, 2023, 2024, 2025, 2026, 2027, 2028,
2029, 2030, 2031, 2032, 2033, 2034, 2035, 2036, 2037, 2038, 2039,
2040, 2041, 2042, 2043, 2044, 2045, 2046, 2047, 2048, 2049, 2050,
2051, 2052, 2053, 2054, 2055, 2056, 2057, 2058, 2059, 2060, 2061,
2062, 2063, 2064, 2065, 2066, 2067, 2068, 2069, 2070, 2071, 2072,
2073, 2074, 2075, 2076, 2077, 2078, 2079, 2080, 2081, 2082, 2083,
2084, 2085, 2086, 2087, 2088, 2089, 2090, 2091, 2092, 2093, 2094,
2095, 2096, 2097, 2098, 2099, 2100, 2101, 2102, 2103, 2104, 2105,
2106, 2107, 2108, 2109, 2110, 2111, 2112, 2113, 2114, 2115, 2116,
2117, 2118, 2119, 2120, 2121, 2122, 2123, 2124, 2125, 2126, 2127,
2128, 2129, 2130, 2131, 2132, 2133, 2134, 2135, 2136, 2137, 2138,
2139, 2140, 2141, 2142, 2143, 2144, 2145, 2146, 2147, 2148, 2149,
2150, 2151, 2152, 2153, 2154, 2155, 2156, 2157, 2158, 2159, 2160,
2161, 2162, 2163, 2164, 2165, 2166, 2167, 2168, 2169, 2170, 2171,
2172, 2173, 2174, 2175, 2176, 2177, 2178, 2179, 2180, 2181, 2182,
2183, 2184, 2185, 2186, 2187, 2188, 2189, 2190, 2191, 2192, 2193,
2194, 2195, 2196, 2197, 2198, 2199, 2200, 2201, 2202, 2203, 2204,
2205, 2206, 2207, 2208, 2209, 2210, 2211, 2212, 2213, 2214, 2215,
2216, 2217, 2218, 2219, 2220, 2221, 2222, 2223, 2224, 2225, 2226,
2227, 2228, 2229, 2230, 2231, 2232, 2233, 2234, 2235, 2236, 2237,
2238, 2239, 2240, 2241, 2242, 2243, 2244, 2245, 2246, 2247, 2248,
2249, 2250, 2251, 2252, 2253, 2254, 2255, 2256, 2257, 2258, 2259,
2260, 2261, 2262, 2263, 2264, 2265, 2266, 2267, 2268, 2269, 2270,
2271, 2272, 2273, 2274, 2275, 2276, 2277, 2278, 2279, 2280, 2281,
2282, 2283, 2284, 2285, 2286, 2287, 2288, 2289, 2290, 2291, 2292,
2293, 2294, 2295, 2296, 2297, 2298, 2299, 2300, 2301, 2302, 2303,
2304, 2305, 2306, 2307, 2308, 2309, 2310, 2311, 2312, 2313, 2314,
2315, 2316, 2317, 2318, 2319, 2320, 2321, 2322, 2323, 2324, 2325,
2326, 2327, 2328, 2329, 2330, 2331, 2332, 2333, 2334, 2335, 2336,
2337, 2338, 2339, 2340, 2341, 2342, 2343, 2344, 2345, 2346, 2347,
2348, 2349, 2350, 2351, 2352, 2353, 2354, 2355, 2356, 2357, 2358,
2359, 2360, 2361, 2362, 2363, 2364, 2365, 2366, 2367, 2368, 2369,
2370, 2371, 2372, 2373, 2374, 2375, 2376, 2377, 2378, 2379, 2380,
2381, 2382, 2383, 2384, 2385, 2386, 2387, 2388, 2389, 2390, 2391,
2392, 2393, 2394, 2395, 2396, 2397, 2398, 2399, 2400, 2401, 2402,
2403, 2404, 2405, 2406, 2407, 2408, 2409, 2410, 2411, 2412, 2413,
2414, 2415, 2416, 2417, 2418, 2419, 2420, 2421, 2422, 2423, 2424,
2425, 2426, 2427, 2428, 2429, 2430, 2431, 2432, 2433, 2434, 2435,
2436, 2437, 2438, 2439, 2440, 2441, 2442, 2443, 2444, 2445, 2446,
2447, 2448, 2449, 2450, 2451, 2452, 2453, 2454, 2455, 2456, 2457,
2458, 2459, 2460, 2461, 2462, 2463, 2464, 2465, 2466, 2467, 2468,
2469, 2470, 2471, 2472, 2473, 2474, 2475, 2476, 2477, 2478, 2479,
2480, 2481, 2482, 2483, 2484, 2485, 2486, 2487, 2488, 2489, 2490,
2491, 2492, 2493, 2494, 2495, 2496, 2497, 2498, 2499, 2500, 2501,
2502, 2503, 2504, 2505, 2506, 2507, 2508, 2509, 2510, 2511, 2512,
2513, 2514, 2515, 2516, 2517, 2518, 2519, 2520, 2521, 2522, 2523,
2524, 2525, 2526, 2527, 2528, 2529, 2530, 2531, 2532, 2533, 2534,
2535, 2536, 2537, 2538, 2539, 2540, 2541, 2542, 2543, 2544, 2545,
2546, 2547, 2548, 2549, 2550, 2551, 2552, 2553, 2554, 2555, 2556,
2557, 2558, 2559, 2560, 2561, 2562, 2563, 2564, 2565, 2566, 2567,
2568, 2569, 2570, 2571, 2572, 2573, 2574, 2575, 2576, 2577, 2578,
2579, 2580, 2581, 2582, 2583, 2584, 2585, 2586, 2587, 2588, 2589,
2590, 2591, 2592, 2593, 2594, 2595, 2596, 2597, 2598, 2599, 2600,
2601, 2602, 2603, 2604, 2605, 2606, 2607, 2608, 2609, 2610, 2611,
2612, 2613, 2614, 2615, 2616, 2617, 2618, 2619, 2620, 2621, 2622,
2623, 2624, 2625, 2626, 2627, 2628, 2629, 2630, 2631, 2632, 2633,
2634, 2635, 2636, 2637, 2638, 2639, 2640, 2641, 2642, 2643, 2644,
2645, 2646, 2647, 2648, 2649, 2650, 2651, 2652, 2653, 2654, 2655,
2656, 2657, 2658, 2659, 2660, 2661, 2662, 2663, 2664, 2665, 2666,
2667, 2668, 2669, 2670, 2671, 2672, 2673, 2674, 2675, 2676, 2677,
2678, 2679, 2680, 2681, 2682, 2683, 2684, 2685, 2686, 2687, 2688,
2689, 2690, 2691, 2692, 2693, 2694, 2695, 2696, 2697, 2698, 2699,
2700, 2701, 2702, 2703, 2704, 2705, 2706, 2707, 2708, 2709, 2710,
2711, 2712, 2713, 2714, 2715, 2716, 2717, 2718, 2719, 2720, 2721,
2722, 2723, 2724, 2725, 2726, 2727, 2728, 2729, 2730, 2731, 2732,
2733, 2734, 2735, 2736, 2737, 2738, 2739, 2740, 2741, 2742, 2743,
2744, 2745, 2746, 2747, 2748, 2749, 2750, 2751, 2752, 2753, 2754,
2755, 2756, 2757, 2758, 2759, 2760, 2761, 2762, 2763, 2764, 2765,
2766, 2767, 2768, 2769, 2770, 2771, 2772, 2773, 2774, 2775, 2776,
2777, 2778, 2779, 2780, 2781, 2782, 2783, 2784, 2785, 2786, 2787,
2788, 2789, 2790, 2791, 2792, 2793, 2794, 2795, 2796, 2797, 2798,
2799, 2800, 2801, 2802, 2803, 2804, 2805, 2806, 2807, 2808, 2809,
2810, 2811, 2812, 2813, 2814, 2815, 2816, 2817, 2818, 2819, 2820,
2821, 2822, 2823, 2824, 2825, 2826, 2827, 2828, 2829, 2830, 2831,
2832, 2833, 2834, 2835, 2836, 2837, 2838, 2839, 2840, 2841, 2842,
2843, 2844, 2845, 2846, 2847, 2848, 2849, 2850, 2851, 2852, 2853,
2854, 2855, 2856, 2857, 2858, 2859, 2860, 2861, 2862, 2863, 2864,
2865, 2866, 2867, 2868, 2869, 2870, 2871, 2872, 2873, 2874, 2875,
2876, 2877, 2878, 2879, 2880, 2881, 2882, 2883, 2884, 2885, 2886,
2887, 2888, 2889, 2890, 2891, 2892, 2893, 2894, 2895, 2896, 2897,
2898, 2899, 2900, 2901, 2902, 2903, 2904, 2905, 2906, 2907, 2908,
2909, 2910, 2911, 2912, 2913, 2914, 2915, 2916, 2917, 2918, 2919,
2920, 2921), class = "Date"), month = 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, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4,
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
5, 5, 5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3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4,
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 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, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 9, 9,
9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
9, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11,
11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11,
11, 11, 11, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
12, 12), day = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L,
12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L,
25L, 26L, 27L, 28L, 29L, 30L, 31L, 1L, 2L, 3L, 4L, 5L, 6L, 7L,
8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L,
21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 1L, 2L, 3L, 4L, 5L, 6L,
7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L,
20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L, 1L,
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L,
16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L,
29L, 30L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L,
13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L,
26L, 27L, 28L, 29L, 30L, 31L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L,
9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L,
22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 1L, 2L, 3L, 4L,
5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L,
19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L,
1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L,
15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L,
28L, 29L, 30L, 31L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L,
11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L,
24L, 25L, 26L, 27L, 28L, 29L, 30L, 1L, 2L, 3L, 4L, 5L, 6L, 7L,
8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L,
21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L, 1L, 2L,
3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L,
17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L,
30L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L,
14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L,
27L, 28L, 29L, 30L, 31L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L,
10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L,
23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L, 1L, 2L, 3L, 4L,
5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L,
19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 1L, 2L,
3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L,
17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L,
30L, 31L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L,
13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L,
26L, 27L, 28L, 29L, 30L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L,
10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L,
23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L, 1L, 2L, 3L, 4L,
5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L,
19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 1L,
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L,
16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L,
29L, 30L, 31L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L,
12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L,
25L, 26L, 27L, 28L, 29L, 30L, 31L, 1L, 2L, 3L, 4L, 5L, 6L, 7L,
8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L,
21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 1L, 2L, 3L,
4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L,
18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L,
31L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L,
14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L,
27L, 28L, 29L, 30L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L,
11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L,
24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L, 1L, 2L, 3L, 4L, 5L, 6L,
7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L,
20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L, 1L,
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L,
16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L,
1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L,
15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L,
28L, 29L, 30L, 31L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L,
11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L,
24L, 25L, 26L, 27L, 28L, 29L, 30L, 1L, 2L, 3L, 4L, 5L, 6L, 7L,
8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L,
21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L, 1L, 2L,
3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L,
17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L,
30L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L,
14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L,
27L, 28L, 29L, 30L, 31L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L,
10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L,
23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L, 1L, 2L, 3L, 4L,
5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L,
19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 1L,
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L,
16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L,
29L, 30L, 31L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L,
12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L,
25L, 26L, 27L, 28L, 29L, 30L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L,
9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L,
22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L), year = c(75L,
75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L,
75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L,
75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L,
75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L,
75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L,
75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L,
75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L,
75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L,
75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L,
75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L,
75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L,
75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L,
75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L,
75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L,
75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L,
75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L,
75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L,
75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L,
75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L,
75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L,
75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L,
75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L,
75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L,
75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L,
75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L,
75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L,
75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L,
75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L,
76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L,
76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L,
76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L,
76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L,
76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L,
76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L,
76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L,
76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L,
76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L,
76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L,
76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L,
76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L,
76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L,
76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L,
76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L,
76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L,
76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L,
76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L,
76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L,
76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L,
76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L,
76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L,
76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L,
76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L,
76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L,
76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L,
76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L,
76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L,
76L, 76L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L,
77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L,
77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L,
77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L,
77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L,
77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L,
77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L,
77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L,
77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L,
77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L,
77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L,
77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L,
77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L,
77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L,
77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L,
77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L,
77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L,
77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L,
77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L,
77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L,
77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L,
77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L,
77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L,
77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L,
77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L,
77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L,
77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L,
77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L,
77L, 77L, 77L), prec = c(9, 9, 3, 8, 6, 5, 7, 1, 7, 7, 5, 7,
9, 3, 5, 9, 10, 1, 5, 6, 9, 1, 10, 9, 1, 5, 4, 9, 4, 8, 7, 8,
4, 7, 0, 8, 0, 2, 9, 6, 4, 4, 0, 10, 4, 10, 9, 6, 10, 6, 3, 3,
4, 8, 0, 7, 7, 2, 3, 5, 7, 10, 8, 6, 8, 2, 3, 8, 7, 2, 0, 1,
2, 5, 2, 7, 0, 4, 5, 0, 6, 2, 4, 6, 8, 6, 2, 1, 1, 3, 7, 0, 2,
9, 9, 7, 3, 5, 7, 6, 6, 2, 2, 4, 9, 10, 7, 7, 5, 0, 6, 8, 8,
5, 5, 5, 0, 4, 6, 8, 4, 4, 6, 6, 7, 4, 9, 10, 2, 7, 9, 6, 6,
9, 9, 6, 8, 1, 8, 6, 1, 1, 5, 8, 7, 8, 2, 9, 3, 1, 7, 3, 8, 4,
7, 8, 2, 0, 1, 7, 9, 6, 6, 2, 5, 2, 5, 3, 1, 2, 7, 4, 4, 5, 4,
1, 8, 6, 8, 8, 9, 9, 3, 3, 7, 7, 9, 8, 1, 3, 2, 8, 1, 1, 1, 1,
5, 1, 7, 7, 9, 5, 9, 4, 2, 4, 10, 5, 3, 3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6, 4, 4, 9, 4, 1, 7, 7, 1, 0, 4, 0, 6, 9, 3, 2, 5, 1,
4, 3, 9, 9, 8, 8, 10, 6, 2, 1, 8, 10, 10, 1, 6, 9, 1, 4, 2, 4,
5, 10, 9, 9, 8, 1, 0, 3, 2, 8, 5, 5, 1, 3, 8, 4, 0, 7, 6, 1,
3, 4, 3, 7, 4, 6, 6, 8, 7, 7, 4, 7, 0, 8, 1, 9, 1, 2, 8, 2, 4,
4, 6, 6, 1, 2, 3, 4, 7, 1, 4, 0, 5, 8, 5, 2, 9, 1, 3, 9, 8, 1,
8, 9, 1, 1, 5, 1, 5, 0, 8, 1, 8, 5, 5, 0, 6, 7, 2, 8, 4, 6, 2,
2, 2, 8, 1, 3, 8, 3, 9, 0, 3, 3, 1, 4, 5, 0, 10, 9, 10, 2, 0,
1, 4, 5, 9, 6, 5, 1, 9, 3, 4, 6, 7, 2, 6, 8, 10, 0, 8, 2, 1,
7, 3, 8, 6, 5, 0, 3, 0, 9, 1, 1, 4, 7, 9, 2, 4, 6, 6, 0, 5, 9,
2, 7, 9, 0, 1, 1, 4, 10, 4, 7, 7, 0, 9, 5, 7, 0, 9, 0, 8, 5,
3, 5)), row.names = c(NA, -1096L), class = "data.frame")

Place a background image in a specific position in ggplot2 and take measurements

My question is quite challenging for me so far. Hope with your help we can get what I need.
I want to place an image as background of a ggplot.
Data to reproduce : Two objects are necessary: a data frame and an image.
DATA at the end of this question.
Background image:
And this is the image to place as background (molar.png):
What I got so far...
I ggplotted:
library(png)
library(grid)
library(ggplot2)
img <- readPNG("images/molar.png")
g <- rasterGrob(img, interpolate = TRUE)
ggplot(df), aes(x = X, y = Y)) +
coord_fixed(ratio = .75) +
annotation_custom(g, xmin=-Inf, xmax=Inf, ymin=-Inf, ymax=Inf) +
geom_point(alpha = .20, size = 1, shape = 19, color = "red") +
stat_smooth(method = 'lm', formula = y ~ poly(x, 3)) +
xlim(0, 100) + ylim(0, 100) +
theme_minimal() +
geom_hline(aes(yintercept = 0), color = "red", lty = 2) +
geom_vline(aes(xintercept = 0), color = "red", lty = 2)
I got this result:
... and what I really want
1. Adapt the regression to this line
Take this red horizontal line as reference:
The X axis (from 0 to 100) must be fitted to this line, as shown now:
This is really what I want: to adapt the background image (without modifying its proportion) to that horizontal line, from the cusp tip (gray) to the ending of the white area to the right.
This would require to select two landmarks in the image, but don't know how to make it automatic by simply selecting one or two points, so that the regression automatically fits that distance.
2. Take measurements
I would like to measure the distance between the highest point of the regression and the point [100, 0], as shown below in green line.
The point here, how can we reference if we are having a scale in the image?
DATA
df <- structure(list(tooth_num = 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, 1, 1, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4,
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5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10L,
11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L,
23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L, 32L, 33L, 34L,
35L, 36L, 37L, 38L, 39L, 40L, 41L, 42L, 43L, 44L, 45L, 46L,
47L, 48L, 49L, 50L, 51L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L,
9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L,
21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L, 32L,
33L, 34L, 35L, 36L, 37L, 38L, 39L, 40L, 41L, 42L, 43L, 44L,
45L, 46L, 47L, 48L, 49L, 50L, 51L, 1L, 2L, 3L, 4L, 5L, 6L,
7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L,
19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L,
31L, 32L, 33L, 34L, 35L, 36L, 37L, 38L, 39L, 40L, 41L, 42L,
43L, 44L, 45L, 46L, 47L, 48L, 49L, 50L, 51L, 1L, 2L, 3L,
4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L,
17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L,
29L, 30L, 31L, 32L, 33L, 34L, 35L, 36L, 37L, 38L, 39L, 40L,
41L, 42L, 43L, 44L, 45L, 46L, 47L, 48L, 49L, 50L, 51L, 1L,
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L,
15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L,
27L, 28L, 29L, 30L, 31L, 32L, 33L, 34L, 35L, 36L, 37L, 38L,
39L, 40L, 41L, 42L, 43L, 44L, 45L, 46L, 47L, 48L, 49L, 50L,
51L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L,
14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L,
26L, 27L, 28L, 29L, 30L, 31L, 32L, 33L, 34L, 35L, 36L, 37L,
38L, 39L, 40L, 41L, 42L, 43L, 44L, 45L, 46L, 47L, 48L, 49L,
50L, 51L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L,
13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L,
25L, 26L, 27L, 28L, 29L, 30L, 31L, 32L, 33L, 34L, 35L, 36L,
37L, 38L, 39L, 40L, 41L, 42L, 43L, 44L, 45L, 46L, 47L, 48L,
49L, 50L, 51L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L,
12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L,
24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L, 32L, 33L, 34L, 35L,
36L, 37L, 38L, 39L, 40L, 41L, 42L, 43L, 44L, 45L, 46L, 47L,
48L, 49L, 50L, 51L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L,
11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L,
23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L, 32L, 33L, 34L,
35L, 36L, 37L, 38L, 39L, 40L, 41L), X = c(0, 1.96207644325269,
3.96448989844662, 6.02285180886395, 8.15165005779206, 10.3635651674945,
12.6674140636077, 15.0640931778694, 17.5406465345979, 20.0647832611694,
22.5857329433686, 25.0458992379799, 27.3978582481916, 29.6291517474813,
31.8764771189544, 34.0520372099874, 36.1615029782313, 38.2134690535817,
40.2184171634735, 42.1879211839819, 44.1341445581471, 46.0695527612659,
48.0067272490216, 49.9582712234201, 51.9366936524031, 53.9541918066236,
56.0223057345439, 58.1471219902002, 60.015986446532, 62.0418690161973,
64.172643764213, 66.3646706538791, 68.5820610043302, 70.7926665021678,
72.9630123083219, 75.0533364802678, 77.0143642675254, 78.7889946597798,
80.3221949636097, 81.6792186440018, 83.1246290439824, 84.6572566946537,
86.2607918541562, 87.9199855179157, 89.6207074088368, 91.349834829432,
93.0950207010492, 94.844403612905, 96.5863082060859, 98.3089189787115,
100, 0, 2.07242714587787, 4.28691224245265, 6.60638277527078,
8.99634804343432, 11.4282251674021, 13.8802080085279, 16.3366019674999,
18.7865642588865, 21.2228067680933, 23.6405691464549, 26.0268342309397,
28.2980527055826, 30.4950596311624, 32.6499592979795, 34.7798224410736,
36.8956207286784, 39.0058437957066, 41.1183659656274, 43.2421092543673,
45.3894713560051, 47.5991212687389, 49.7770452245596, 51.9171025815641,
54.0147450240845, 56.0668835223753, 58.0722874535502, 60.0316719092865,
61.9474399302204, 63.8232002348162, 65.6632101130593, 67.4719472029522,
69.2537602596947, 71.1020020072457, 73.0197406878018, 74.8895017058263,
76.7134344938606, 78.4953847484249, 80.2407780197449, 81.9562954259909,
83.649499477401, 85.3283767502077, 87.0008805605653, 88.6745443161437,
90.3562154355033, 92.0029343703826, 93.595173297479, 95.1912911811735,
96.7911009335273, 98.3943073713479, 100, 0, 2.49538787860394,
4.98986333706054, 7.48094091039875, 9.95158514589746, 12.3765352721187,
14.7826711324534, 17.1741487059107, 19.5379450224354, 21.8376445004796,
24.2011429944043, 26.5151298348149, 28.7199538624058, 31.0167238490571,
33.2844073975993, 35.4322146421557, 37.6300939882099, 39.8422715265431,
41.8385466322099, 43.9606111742119, 46.1772777113709, 48.0735169131695,
50.1458450809683, 52.0775711992983, 54.0389983532847, 56.0114855556445,
57.9507899054059, 59.8445463497116, 61.7655941740918, 63.7919980969677,
65.7952774486913, 67.8300149894114, 69.6945929413499, 71.6911766995294,
73.5625774964864, 75.3913842466844, 77.1888383995349, 78.9539192357634,
80.672317733178, 82.2770969086854, 83.9169027294331, 85.5602445166866,
87.1949684008868, 88.8468983089335, 90.5308901736114, 92.1034422363185,
93.6509365884513, 95.3521507218448, 96.9695007185512, 98.5200274462478,
100, 0, 1.93071662500976, 3.82909585694513, 5.83503737371176,
7.96719181032991, 10.2303413170764, 12.617316402646, 15.1102082752161,
17.6402520534245, 20.1564846032436, 22.6509074609361, 25.0393836587477,
27.3129712478007, 29.4988321446333, 31.6158908190034, 33.5547876344398,
35.4063709991687, 37.2999307397267, 39.3177890463815, 41.2304325787261,
43.1170170017018, 45.1909193214654, 47.1049295380901, 49.1859515610248,
51.13271739105, 53.1310164292379, 55.0797239964915, 57.1128373874235,
58.9454191107869, 60.9254683084337, 62.9947826516433, 65.0902258076096,
67.1539265387035, 69.1321933211508, 71.0068236131378, 72.9528127482717,
74.868864278393, 76.7358744964615, 78.5828026706554, 80.4923369340278,
82.44389985077, 84.3433495275878, 86.0966137745744, 87.9057900961145,
89.7809605893561, 91.5773662545089, 93.2971230872706, 94.8659471916182,
96.5480144068937, 98.2832055612151, 100, 0, 2.45904597118919,
4.9478077699383, 7.4437174468872, 9.93045045419645, 12.3969081092835,
14.8359017068762, 17.2430125015322, 19.6157029336795, 21.9526351315638,
24.2532312350846, 26.5173310764965, 28.744978623416, 30.9362680921638,
33.0912151854599, 35.2096628761033, 37.2911746284744, 39.334940182215,
41.3396279466586, 43.3031840060133, 45.2225306271349, 47.2687426646706,
49.3178157378126, 51.3497697054769, 53.3645511784153, 55.3621224700994,
57.3424678778091, 59.3055999637205, 61.2515438521856, 63.1803529324531,
65.0920931559475, 66.9868618795333, 68.8647627411635, 70.7259245031432,
72.5704916304973, 74.3986274315152, 76.2105077766622, 78.0063242391239,
79.7862840948059, 81.5506040412458, 83.2995196192455, 85.0332663696068,
86.7521175196599, 88.4563337345588, 90.1461348523852, 91.8219291438623,
93.4838736361913, 95.1323766000954, 96.7676892790966, 98.3900943221571,
100, 0, 2.20427029931411, 4.42577825043187, 6.67629529636218,
8.96662563154085, 11.3064974908959, 13.704119943729, 16.165246307201,
18.6916215311874, 21.2788860284595, 23.9143289820342, 26.5756256258544,
29.2320665145903, 31.8490369716412, 34.3727083595587, 36.7961343283599,
39.1127253229244, 41.3113092041144, 43.3888560030568, 45.3501991751572,
47.2064997097139, 48.9732280089765, 50.6683743389121, 52.3111665976677,
53.9212675271295, 55.5183816810814, 57.1220463657154, 58.7515758904346,
60.4258971077205, 62.1633013292058, 63.9808505967227, 65.8934996324456,
67.912799670054, 70.0333597621695, 71.9831740002692, 73.9087652121441,
75.8101919344515, 77.6875628781259, 79.5410090537812, 81.3706865591696,
83.1767905164818, 84.9595299852075, 86.7191558367347, 88.4559245173709,
90.170123135482, 91.862066674033, 93.5320896282075, 95.1805376430287,
96.8077452136806, 98.414102584544, 100, 0, 2.50973310686108,
4.98257000649406, 7.40768627831619, 9.78047063985944, 12.100599944502,
14.3704650807463, 16.5940944776443, 18.776452301705, 20.9230915203372,
23.0399794423239, 25.1335076302947, 27.2106366228331, 29.2791814137006,
31.3594526601995, 33.4580064720123, 35.556233172214, 37.6533199380137,
39.7484638590935, 41.8408600426407, 43.9297035958428, 46.0141896258872,
48.0934993624991, 50.1668041229306, 52.2332633294661, 54.2920126144546,
56.3421658028046, 58.3828149119835, 60.4130261870291, 62.4318202756028,
64.4382019654095, 66.4311244992937, 68.4095034527027, 70.3722187161805,
72.3180946704221, 74.2459160462301, 76.1544160295474, 78.0422683314784,
79.9081070132353, 81.7505086436864, 83.5679982468404, 85.3590473193511,
87.1220777955072, 88.8554739421999, 90.5575664989655, 92.2266544854268,
93.8610012363035, 95.458856208853, 97.0184430879027, 98.537991505764,
100, 0, 2.49186292873155, 4.96628290858449, 7.40387310725186,
9.79133340002794, 12.1468199057625, 14.5011896071325, 16.8431655039685,
19.1723761535772, 21.4884872575346, 23.7911793751244, 26.0801603047613,
28.355162607706, 30.6159386554963, 32.8622655825162, 35.0939428097114,
37.310792044589, 39.5126572812178, 41.6993973713744, 43.8708934533968,
46.0270489521849, 48.1677771977769, 50.2930162830572, 52.4027142060487,
54.4968362987667, 56.5753602746497, 58.6382811811283, 60.6856064470565,
62.7173459775728, 64.7335369169464, 66.7342089805929, 68.7194141704892,
70.6892069648972, 72.6436517472169, 74.582822805987, 76.5067919534617,
78.4156508121718, 80.30948109951, 82.1883843431457, 83.8953343826177,
85.318702783443, 86.7569115580089, 88.2078484355329, 89.6694432420707,
91.1396480902398, 92.6164522369269, 94.0978597967267, 95.5818971707953,
97.0665932365733, 98.5499867766401, 100, 0, 2.61369623570294,
5.25019204546923, 7.89758672867171, 10.4854413123113, 13.0066240676632,
15.4846710718853, 17.9159547041448, 20.2976684759407, 22.6276875072264,
24.9044541625748, 27.1268476764071, 29.2940720764344, 31.4055784162507,
33.4609732842652, 35.4599616217844, 37.4022781047122, 39.2876574089524,
41.115772453938, 42.8862115298645, 44.5984417012627, 46.5824735364946,
48.8579364441288, 51.0689873528466, 53.2187049370753, 55.31212578009,
57.3558486751465, 59.3576503629965, 61.3261515894915, 63.2705517814855,
65.2004117555501, 67.125505044989, 69.0556829505137, 71.0007693255153,
72.9704141903561, 74.9739450593852, 77.0201427878221, 79.1169465130639,
80.9682934274075, 82.7378404654242, 84.4621062867744, 86.1441466931183,
87.7873262684694, 89.3952520481705, 90.9717460715737, 92.5207813382925,
94.0464474990516, 95.5529302701972, 97.0444725499921, 98.5253927168307,
100, 0, 2.19489342240264, 4.47470248895734, 6.80636996136228,
9.16189235515265, 11.5204119968896, 13.8679818386464, 16.1962336516669,
18.5008000956494, 20.7800403414259, 23.0340446346007, 25.2640080457511,
27.4717901812673, 29.6596690589774, 31.8301824946609, 33.9860569994518,
36.1301804326855, 38.2656102060449, 40.395611569574, 42.5237505919975,
44.6539625286031, 46.790726843019, 48.9392449637073, 51.1057000819157,
53.0838372365286, 55.0005198693747, 56.9159691456296, 58.8292579968134,
60.7394374767241, 62.645542230868, 64.5465987006055, 66.4416169190052,
68.3295795719832, 70.2094638760246, 72.080216965747, 73.9407777716216,
75.7900606116825, 77.6269798039636, 79.4504168499154, 81.2592587204191,
83.0523732433488), Y = c(84.5741422911491, 87.0989688246201,
89.5436034157802, 91.8694746551154, 94.0314174040144, 95.9740138468311,
97.628391911438, 98.9112343041615, 99.7302288176452, 100,
99.6682935009841, 98.7381965579207, 97.2664228638507, 95.3825982722905,
93.5434433368078, 91.50049262398, 89.2915552625009, 86.9514674858642,
84.511940176201, 82.0015588642778, 79.4462792120302, 76.8706085478357,
74.2982919192514, 71.7531887159568, 69.2602636348217, 66.8466919870944,
64.5423551809367, 62.3722941413001, 59.6800326256192, 57.2857466826492,
55.128758759083, 53.1294867372666, 51.2007256913408, 49.2527551687403,
47.1957784888164, 44.9429337520794, 42.418488359019, 39.5725891439776,
36.3973083864229, 33.0253591771941, 29.7461795438129, 26.5669349259918,
23.4758099710143, 20.4586262556193, 17.4994139966193, 14.5814411300049,
11.6872895390296, 8.79946487886403, 5.90043469063777, 2.97289519972717,
0, 93.0321380957158, 95.1946109521693, 96.937054277536, 98.2619255824812,
99.1910694062447, 99.7577986711693, 100, 99.9550821661862,
99.6575185832176, 99.1377550776607, 98.4215955114931, 97.4779796804826,
95.9441517048638, 94.1389735933651, 92.2010899059795, 90.1898222195824,
88.1388247774415, 86.0724678936469, 84.0123913148843, 81.9837162612796,
80.0234419211725, 78.2597001700458, 76.3926747068224, 74.4111019409554,
72.3108177571526, 70.0927095898208, 67.7621020453996, 65.3282107888812,
62.802504203367, 60.1968602570691, 57.5234982391254, 54.7931356266002,
52.0153976695973, 49.3678395136313, 46.8461605150396, 44.2290481856401,
41.5261960397215, 38.748799403644, 35.9094871516561, 33.021434271488,
30.098088807609, 27.1525574835635, 24.19719611686, 21.2436096189696,
18.3024472027724, 15.3103597044707, 12.2378569960892, 9.17074715833242,
6.10882539864554, 3.05175039610287, 0, 100, 99.9740960784806,
99.8730270144876, 99.6594688780895, 99.1648671200451, 98.300448086114,
97.3281159413371, 96.2806584450564, 95.1069839916948, 93.6992988433163,
92.5227992310794, 91.1519207300211, 89.4389990501897, 88.0315526476314,
86.5030621141021, 84.637382900149, 82.9008651750625, 81.2041782110219,
79.064856495856, 77.1370635776142, 75.4555767641426, 73.1766285349799,
71.1390523302985, 68.8176471360649, 66.5503178701644, 64.3037992816136,
61.9957638533255, 59.6077485751849, 57.270625947771, 55.1882053198138,
53.005988938906, 50.8933669646611, 48.4803231663425, 46.2904669191802,
43.8645308465569, 41.369278904061, 38.8252432322521, 36.2327421588907,
33.5735116287118, 30.7665372266409, 28.0047653665761, 25.244744309195,
22.4736613621324, 19.7247419854022, 17.0183591556834, 14.1736629139801,
11.3015506938947, 8.6188037004008, 5.82611425658707, 2.95264914351926,
0, 86.7221127164863, 89.2450865945842, 91.8256626160176,
94.2050712850442, 96.3082958568556, 98.0567601186487, 99.356578317864,
100, 99.8266960099512, 99.3672567218446, 98.6837623193952,
97.4546359200075, 95.8095875992727, 93.8391732848531, 91.6986699837336,
89.1914649315853, 86.5315472203614, 83.9427805951582, 81.5889388575256,
79.0374892354799, 76.4382738493923, 74.2129556980193, 71.6847000287053,
69.5197780116735, 67.026504640704, 64.6334322074442, 62.1466653908716,
59.8252416036741, 57.210640130131, 54.8147737058654, 52.5672567218449,
50.3760405702802, 48.1148980958759, 45.6822122284949, 43.0613338436512,
40.5655726724713, 38.0157305521002, 35.3812266768728, 32.7135011003732,
30.1526743852262, 27.6670940579847, 25.0888527413645, 22.2776767773419,
19.7712372021815, 17.1514304851209, 14.4034446464453, 11.542359582815,
8.48492967180167, 5.57098842216043, 2.73145153573813, 0,
99.1041781658029, 99.7254865136605, 100, 99.9614316109632,
99.6447768919466, 99.0834281892782, 98.3070217828948, 97.341621109093,
96.2090754809024, 94.9278903961505, 93.5136855943403, 91.9791492509625,
90.3350457026248, 88.5899864186136, 86.7511628918963, 84.8242550277467,
82.8134769494328, 80.7219434437174, 78.5514867381084, 76.302473278107,
73.9737121158334, 71.8911107192181, 69.810524772861, 67.694531029918,
65.5438623813919, 63.3593433296612, 61.1416609600414, 58.8917313862862,
56.6101958880234, 54.2979247733192, 51.9556967388641, 49.5842904813491,
47.1845305031526, 44.757241306653, 42.303109977166, 39.8229610170695,
37.317527317367, 34.7876333804369, 32.2340120972821, 29.657396358906,
27.058564861999, 24.438158886189, 21.7969571281666, 19.1356924789353,
16.4549604124345, 13.7555396253553, 11.0379797859501, 8.30305959090951,
5.55132870848586, 2.78342841830685, 0, 84.1515543172144,
86.2722836260793, 88.3566153916604, 90.3773896250419, 92.3062620354674,
94.111690717205, 95.7577913244362, 97.203231722867, 98.4009161759049,
99.2995644137824, 99.8474619227218, 100, 99.7284790643066,
99.0275302806241, 97.8277140842702, 96.2611985608363, 94.3983312397508,
92.2676143186827, 89.9054453409169, 87.3514589414394, 84.6443817904904,
81.8193876527848, 78.9076236667722, 75.9361708679116, 72.9289916301439,
69.9079560608225, 66.8938683956418, 63.9077697306659, 60.9718854638001,
58.1105727362662, 55.3511099671633, 52.7239337138365, 50.2623228580521,
47.9794231502588, 45.4070011187705, 42.7981815439979, 40.1541092510553,
37.4757316814164, 34.7641541834675, 32.0204031521378, 29.2455049823579,
26.4404465923295, 23.6062938537105, 20.7439547312464, 17.8544950965959,
14.9388623912317, 11.9979251031726, 9.0326701506196, 6.04400549831871,
3.03283911101569, 0, 100, 99.5848739044175, 98.8739209129315,
97.9105346461211, 96.7351022995956, 95.3831954679074, 93.885250878182,
92.2670226841415, 90.5504870541479, 88.7545871260625, 86.8960843842295,
84.9905164585802, 83.0528356573708, 81.0986594271243, 79.1673905588146,
77.2717198905873, 75.3754106896234, 73.4768134130185, 71.5743849399925,
69.6665023331719, 67.7516224717761, 65.8282022350241, 63.8947517131965,
61.9497011799818, 59.9915873311911, 58.0188670460438, 56.0301302314122,
54.0238603720464, 51.9985941637575, 49.9528949078877, 47.8852726947172,
45.794397247711, 43.6787786571497, 41.5370334354364, 39.3678047005055,
37.1697621758216, 34.941522373788, 32.6818614399924, 30.3895023089609,
28.063247731812, 25.7019536707251, 23.304582510003, 20.8700966339479,
18.3975648489857, 15.8861889891947, 13.3352240997155, 10.7440582533417,
8.11223915605116, 5.43939433041456, 2.72533753771655, 0,
100, 99.9134879636606, 99.4737726511386, 98.7119319033914,
97.6645725261045, 96.4711111592938, 95.2721931018034, 94.0226109361477,
92.7232680879358, 91.374959571704, 89.9785165390125, 88.5348785524954,
87.0449851747857, 85.5095952833957, 83.9297207150073, 82.3061926211812,
80.6400228385995, 78.9320063817987, 77.1831189504364, 75.3942278330975,
73.5662725924159, 71.7000843799518, 69.7965666213147, 67.8565504680649,
65.8809393458113, 63.8706005431396, 61.8262568005362, 59.7488476806353,
57.6390236498756, 55.4977604079147, 53.3256361471427, 51.123554293169,
48.8921653124316, 46.6322280824431, 44.344429206666, 42.0294914255877,
39.6880290686225, 37.3207287392334, 34.9282409038592, 32.3093546108134,
29.3214008878868, 26.3486608522194, 23.3891108304469, 20.4407632862286,
17.5015222721514, 14.5693279778263, 11.6421205928639, 8.71769575877807,
5.79402980220384, 2.86895450167932, 0, 99.0881176828924,
99.616812993443, 99.9245775334157, 100, 99.4144184933504,
98.4080380989905, 97.2482296158507, 95.9447863261935, 94.5072176490275,
92.9445788221548, 91.2653289705444, 89.4773594926569, 87.5879656741198,
85.6037331424258, 83.5306797985592, 81.3743977486237, 79.139712667937,
76.8311379822374, 74.4527045497319, 72.0079322747694, 69.5001707437473,
67.4173241176181, 65.7355478830473, 63.9261751158092, 62.0062376111431,
59.9926536189868, 57.9018588217461, 55.7494373120646, 53.5502067518011,
51.3181332130511, 49.0667285867045, 46.8089938097941, 44.5579865920031,
42.3268498019911, 40.129152103299, 37.979256976578, 35.89220917429,
33.8840753566102, 31.5408977402496, 29.095103557573, 26.6000023276786,
24.0608455207656, 21.4827994480562, 18.8707181711938, 16.2292854338693,
13.56295788917, 10.8759367132551, 8.1722811506567, 5.45590851427953,
2.73059418540176, 0, 96.4662652426023, 98.0232502530471,
99.1147667554613, 99.7607331141328, 100, 99.8803419056255,
99.4511603186742, 98.7585534337238, 97.843294734107, 96.7405371746194,
95.4801583016932, 94.0874997966255, 92.5842056245715, 90.989010880653,
89.3184813331895, 87.587358543869, 85.8094473196242, 83.9978129241588,
82.1654220154131, 80.3260794003192, 78.4945266405658, 76.6880690477,
74.9274631350027, 73.2399733961649, 70.9563625122581, 68.4738638027609,
65.9881111030584, 63.4970829949925, 60.9982157287041, 58.4893399773897,
55.9680398998363, 53.4319489577125, 50.8788978242146, 48.3064706581295,
45.7125474355354, 43.0948602238647, 40.4513876049598, 37.7800095548987,
35.0788032612873, 32.3458952146129, 29.5794612082463)), .Names = c("tooth_num",
"sample", "X", "Y"), row.names = c(NA, 500L), class = "data.frame")

How to add text below legend manually in ggplot?

I have noted that different versions of this question have been asked throughout the years. However, I cannot find a solution that fits my script. I have seen several functions, but they do not quite do the thing.
Please find my data w1 and w2 below.
I want to add text manually below the legend as demonstrated below.
I use the following:
# My script
df <- data.frame(x = as.factor(c(w1$ny_stadie, w2$ny_stadie)),
y = c(w1$n.fjernet, w2$n.fjernet),
f = rep(c("N+", "N0"), c(nrow(w1), nrow(w2))))
df <- df[!is.na(df$x),]
ggplot(df) +
geom_boxplot(aes(x, y, fill = f, colour = f), outlier.alpha = 0, position = position_dodge(width = 0.78)) +
scale_x_discrete(name = "", label=c("X I\nn=113","X II\nn=102","X III\nn=115","X IV\nn=302")) +
scale_y_continuous(name="X", breaks=seq(0,130,10), limits=c(-5,130)) +
stat_boxplot(aes(x, y, colour = f), geom = "errorbar", width = 0.3,position = position_dodge(0.7753), size=1) +
geom_point(aes(x, y, fill = f, colour = f), size = 3, shape = 21, position = position_jitterdodge(), alpha=0.7) +
scale_fill_manual(values = c("#edf1f9", "#fcebeb"), name = "X",
labels = c("X", "X")) +
scale_colour_manual(values = c("#1C73C2", "red"), name = "X",
labels = c("X", "X"))
theme(axis.text.x = element_text(color = "grey20", size = 14), axis.title.x = element_text(color = "grey20", size = 14, face="bold", margin=margin(t=12))) +
theme(axis.text.y = element_text(color = "grey20", size = 11), axis.title.y = element_text(color = "grey20", size = 14, face="bold", margin=margin(r=12))) +
theme(legend.key = element_rect(fill = "white")) +
theme(legend.text=element_text(size=12)) + theme(legend.title=element_text(size=14))
And my data
# My Data
w1 <- structure(list(ny_stadie = structure(c(1, 1, 4, 4, 1, 3, 1, 4,
3, 2, 2, 3, 4, 4, 4, 2, 4, 4, 3, 4, 2, 4, 4, 4, 4, 1, 4, 4, 4,
2, 1, 2, 2, 3, 1, 4, 3, 1, 3, 1, 4, 4, 2, 1, 4, 1, 4, 2, 4, 2
), class = "AsIs"), n.fjernet = c(25L, 3L, 27L, 22L, 18L, 9L,
6L, 5L, 25L, 13L, 5L, 56L, 56L, 30L, 27L, 27L, 26L, 24L, 22L,
22L, 20L, 19L, 18L, 17L, 15L, 15L, 13L, 12L, 12L, 11L, 11L, 10L,
9L, 8L, 8L, 8L, 8L, 7L, 7L, 6L, 5L, 4L, 2L, 2L, 2L, 34L, 30L,
28L, 25L, 22L)), row.names = c(1L, 3L, 4L, 33L, 41L, 65L, 74L,
81L, 82L, 84L, 86L, 88L, 89L, 97L, 100L, 101L, 102L, 104L, 107L,
108L, 112L, 114L, 116L, 117L, 119L, 120L, 126L, 128L, 129L, 135L,
136L, 137L, 143L, 144L, 145L, 147L, 148L, 150L, 154L, 157L, 160L,
162L, 167L, 168L, 169L, 170L, 171L, 172L, 173L, 174L), class = "data.frame")
AND
w2 <- structure(list(ny_stadie = structure(c(4, 3, 4, 4, 4, 4, 4, 4,
3, 1, 3, 4, 3, 1, 1, 1, 4, 4, 3, 4, 4, 2, 2, 2, 2, 4, 3, 2, 1,
1, 4, 3, 2, 1, 1, 1, 4, 3, 4, 2, 4, 4, 4, 4, 3, 4, 2, 2, 4, 4
), class = "AsIs"), n.fjernet = c(10L, 13L, 9L, 7L, 7L, 7L, 6L,
6L, 5L, 4L, 3L, 37L, 26L, 19L, 17L, 15L, 9L, 57L, 55L, 33L, 33L,
33L, 28L, 27L, 27L, 26L, 23L, 23L, 23L, 22L, 21L, 21L, 20L, 20L,
19L, 18L, 18L, 18L, 17L, 17L, 16L, 16L, 16L, 15L, 15L, 15L, 14L,
14L, 13L, 13L)), row.names = c(2L, 5L, 6L, 7L, 8L, 9L, 10L, 11L,
12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L,
25L, 26L, 27L, 28L, 29L, 30L, 31L, 32L, 34L, 35L, 36L, 37L, 38L,
39L, 40L, 42L, 43L, 44L, 45L, 46L, 47L, 48L, 49L, 50L, 51L, 52L,
53L, 54L, 55L), class = "data.frame")
You can use grobs (graphical objects) from grid package.
See this following code:
library(grid)
library(gridExtra)
vp = viewport(x=.8725, y=.4, width=.15, height=.3, just="left", clip="off", angle=0)
pushViewport(vp)
tbl <- textGrob("Text here", gp = gpar(fontsize=12, col="blue"))
grid.draw(tbl)
upViewport()

Group by adjoining dates in r

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

vcov.polr is giving negative diagonal elements (mwe included)

I don't know too much about ordered logits so perhaps I'm doing something silly, but the following seems straightforward and I don't know why vcov.polr gives me negative diagonals. I tried to follow the code in vcov.polr but got lost so I don't know what is wrong. Thanks for your help!
temp_fc <-
structure(list(the_index = c(4, 3, 3, 4, 4, 1, 3, 2, 3, 3, 3,
4, 4, 4, 1, 4, 4, 1, 4, 3, 4, 3, 3, 1, 4, 2, 4, 4, 4, 4, 3, 4,
1, 4, 4, 3, 4, 4, 2, 4, 1, 4, 4, 2, 3, 1, 4, 2, 1, 1, 4, 1, 4,
4, 4, 4, 1, 4, 3, 1, 4, 3, 4, 1, 4, 2, 3, 1, 4, 4, 1, 3, 2, 3,
2, 3, 3, 4, 1, 1, 4, 4, 4, 4, 1, 4, 4, 2, 4, 4, 4, 1, 1, 4, 3,
2, 4, 4, 1, 1), age = c(39L, 40L, 23L, 33L, 24L, 22L, 15L, 43L,
59L, 58L, 20L, 39L, 33L, 21L, 34L, 44L, 27L, 53L, 60L, 34L, 17L,
20L, 19L, 21L, 57L, 56L, 36L, 30L, 16L, 17L, 16L, 71L, 49L, 42L,
72L, 36L, 18L, 19L, 62L, 20L, 55L, 79L, 69L, 46L, 28L, 50L, 40L,
21L, 57L, 31L, 38L, 60L, 56L, 59L, 21L, 26L, 53L, 49L, 51L, 59L,
32L, 56L, 25L, 46L, 42L, 70L, 65L, 50L, 42L, 53L, 29L, 63L, 34L,
29L, 65L, 31L, 70L, 52L, 76L, 37L, 24L, 28L, 45L, 47L, 48L, 55L,
65L, 28L, 28L, 31L, 24L, 41L, 43L, 34L, 69L, 65L, 48L, 24L, 32L,
18L)), .Names = c("the_index", "age"), row.names = c("1006",
"1040", "1085", "1115", "1130", "1144", "1162", "1164", "1176",
"1178", "1181", "1192", "1223", "1258", "1259", "1307", "1311",
"1330", "1338", "1368", "1401", "1411", "1443", "1444", "1515",
"1538", "1600", "1628", "1632", "1683", "1692", "1701", "1710",
"1784", "1790", "1793", "1799", "1873", "1897", "1904", "1908",
"1982", "1988", "2015", "2029", "2052", "2065", "2077", "2088",
"2095", "2163", "10345", "10355", "10372", "10388", "10397",
"10414", "10415", "10417", "10421", "10428", "10430", "10456",
"10480", "10490", "10492", "10509", "10587", "10600", "10607",
"10609", "10617", "10625", "10626", "10630", "10640", "10674",
"10684", "10686", "10687", "10700", "10703", "10713", "10730",
"10740", "10747", "10750", "10762", "10792", "10794", "10803",
"10820", "10824", "10830", "10833", "10835", "10857", "10888",
"10917", "10933"), class = "data.frame")
library(MASS)
model_temp <-factor(the_index) ~ age + I(age^2)
the_OL <- polr(model_temp,temp_fc,method="logistic", Hess=TRUE)
diag(vcov(the_OL)) #this gives negative values
#so of course the following will have an error:
summary(the_OL)
I think this is due to your use of non-orthogonal polynomials in your model formulae; this appears to be causing the computational issues. With your code I get:
> the_OL2 <- polr(model_temp,temp_fc,method="logistic", Hess=TRUE)
> AIC(the_OL2)
[1] 254.6934
If I refit using orthogonal polynomials via poly(), I seem to get the same fit but without the numerical issues of negative variances:
> the_OL <- polr(factor(the_index) ~ poly(age, 2), temp_fc, method="logistic",
+ Hess=TRUE)
> AIC(the_OL)
[1] 254.6934
> diag(vcov(the_OL))
poly(age, 2)1 poly(age, 2)2 1|2 2|3
3.38608675 3.65981581 0.05958267 0.04704916
3|4
0.04103139
The residual deviances reported for the two models are the same and the fitted values from the two models are almost the same (near as matters).

Resources