Using mob() trees (partykit package) with nls() model - r

I'm trying to use model-based recursive partitioning (MOB) with the mob() function (from the partykit package) to separate several curves that were derived using the nls() function. I had to define my model and determine the starting values. I've been trying to see if this could be used with the mob() function to no avail.
I tried following this example on page 7:
https://cran.r-project.org/web/packages/partykit/vignettes/mob.pdf
I created a fit function that estimates the starting values and would return the estimates etc. of the nls(). But I can't seem to get anything going after that. I'd like to know if it is at all possible to use a custom model, with coefficients and both dependent and independent variables and to include them in mob() and get it to work. I tried the lmtree() function but of course this will only give a straight line.
My code is below. Basically I use a segmented linear regression to get the starting values of a double exponential curve that I am using. This is the furthest I got basically. The parameter estimates give an error etc, if you even get past that it just won't run. I just need to know if it is at possible for the mob() function to run nls().
I loaded sample data, but if it is possible to use the nls()
photo.try <- function(y, x,start = NULL, weights = NULL, offset = NULL, estfun = FALSE, object = TRUE)
{
lin.mod1 <- lm(y ~ x)
segmented.mod.2 <- segmented(lin.mod1, seg.Z = ~x, psi=1)
segmented.mod1 <- segmented(lin.mod1, seg.Z = ~x, psi = segmented.mod.2$psi[1,2])
nls(y ~ (a*exp(-b * x) - c* exp(-d* x)), start = list(a = -1*(intercept(segmented.mod1)[[1]][1,1]) , b = slope(segmented.mod1)[[1]][1,1],
c = -1*(intercept(segmented.mod1)[[1]][2,1]),
d = -1*slope(segmented.mod1)[[1]][2,1]))
}
photo_form <- Pn ~ (a*exp(-b * PAR) - c* exp(-d* PAR))| Species
photo_tree <- mob(photo_form, data = eco, fit = (photo.try))
Here is my sample data:
eco <- structure(list(Species = structure(c(1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L,
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L,
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L,
5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L,
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L,
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L,
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L,
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L,
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L,
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L,
6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L,
7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L,
7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L,
7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L,
7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L,
7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L,
7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L), .Label = c("Bogum",
"Clethra", "Eugene", "Guarria", "Melo", "Santa", "Sapium"), class = "factor"),
PAR = c(0, 58.6, 101.4, 228.6, 462.4, 904.7, 1565.8, 1992.1,
2395.9, 0, 72.8, 125.9, 232.8, 411, 841.1, 1669.6, 2394.5,
2394.9, 0, 53.5, 122.1, 231.6, 451, 808.5, 1575, 2394.6,
2395.1, 0, 70.9, 104.8, 251.1, 474.6, 858.3, 1612.3, 2393.3,
2395.1, 0, 63.1, 124.6, 277.1, 417.7, 824.4, 1649.6, 2377.7,
2381.9, 0, 31, 46.5, 115.7, 228.1, 424.3, 822.5, 1644.2,
2380.7, 2381.2, 0, 50.1, 118.1, 203.3, 413.2, 804.5, 1587.3,
2385.3, 0, 28.8, 36.9, 101.2, 211.7, 423.1, 793, 0, 43.6,
106.7, 200.8, 468.6, 808.4, 1567, 2367.1, 2376.5, 0.1, 40.4,
104.1, 202.2, 447.3, 794.7, 1546, 2391.8, 2393.3, 0.1, 44.1,
107.5, 227.4, 429.6, 802.5, 1668.4, 2391, 0, 42.2, 125.3,
126.2, 127.3, 240.3, 433.4, 791, 1600, 2396.8, 2397, 2399.3,
0, 72.7, 118.1, 236.9, 425, 828.4, 1613.3, 1615.4, 2396.1,
2396.5, 2397.2, 2397.5, 0, 62, 116.2, 235.5, 401.7, 879,
879.8, 1552.2, 1553.9, 2394.3, 2394.4, 2394.7, 2396.6, 0,
84.8, 135, 209.8, 425.3, 859.1, 1597.6, 2377.3, 2379.5, 2385.1,
0.1, 62, 106.3, 226.2, 442.9, 822.5, 1462.3, 2389.8, 2392.1,
0.1, 0.1, 73.9, 126, 249.8, 428.5, 846.5, 1555.3, 2390.1,
2390.7, 2390.8, 0, 68.7, 121.5, 209.7, 426.2, 803, 1525.9,
2389.8, 0, 52.8, 96.9, 211.1, 441.3, 787.9, 1566.5, 2415.2,
2415.3, 2415.5, 2417.5, 2417.7, 2418.5, 0.1, 46.5, 108.4,
233.5, 461.7, 792.3, 1635.7, 2415.1, 2415.6, 2415.6, 2416.5,
2416.6, 2417.8, 0.1, 68.3, 110, 239.5, 531.7, 847.2, 1591.4,
2387.3, 2387.6, 2389.7, 0, 49.7, 114.6, 230.6, 397.7, 398.2,
817.7, 1596.4, 2376.2, 2376.4, 2380.9, 0, 62.9, 65.5, 117,
209, 431.2, 854.5, 1611.3, 2387.3, 2388.5, 2390.3, 0, 49.1,
108.9, 200.3, 408.8, 842.2, 1630.2, 2386.5, 2386.8, 2388.2,
0, 64.8, 122.9, 226, 422.9, 801.6, 1635.7, 2383.6, 2383.6,
2384.3, 2386.1, 0, 36.7, 143.2, 213.7, 444.9, 814.9, 816.2,
1496.5, 2384.7, 2386.5, 2388.6, 0.1, 45.6, 105.2, 206.7,
494.8, 901.2, 1610.9, 2388, 2388.1, 2388.3, 2388.6, 0, 0.1,
45.9, 48.5, 100.2, 209.4, 432.4, 778, 1600.3, 2408.8, 2408.8,
0, 71.8, 121.6, 216.4, 404.3, 815.2, 1622, 2414.9, 2415.1,
2416.1, 2416.1, 0, 36.2, 97.5, 186.7, 417.9, 840.4, 1597.5,
2390.7, 2390.9, 2391.2, 2391.2, 2391.5, 2392.1, 2392.5, 0,
53.8, 138.2, 227, 403.6, 800.8, 1642.3, 2396.9, 2397.1, 0,
57.9, 95.1, 246.6, 466.8, 796.2, 1574.2, 2395.5, 2397.3,
0, 54.9, 94.9, 201.7, 408.1, 822.6, 1596, 2384.1, 0, 55.6,
131, 202.5, 419.8, 798.5, 1614, 2387.4, 2387.8, 0, 39.1,
109.6, 197.1, 403.3, 835.4, 836.9, 1725.9, 1727.4, 1729.3,
1730.6, 54.5, 58.6, 125.4, 226.9, 409, 806.8, 1578.8, 2377.2,
2380.1, 2388.3, 0, 68, 127.4, 206.9, 510.5, 814.9, 1561,
2404.1, 2404.8, 0, 58.4, 95.3, 229.6, 457.2, 781.5, 1634.4,
2399.8, 2401, 2403, 0.1, 56.5, 101.9, 221.8, 394.3, 815.1,
1655.4, 2411.8, 2411.9, 0, 50.2, 107.3, 220.5, 434.4, 819.8,
1630.6, 2412.4, 2412.6, 0, 48.4, 117.7, 195.3, 403.2, 801,
1632.7, 2388.9, 2389.3, 2390.7, 0, 50.4, 120.3, 234.7, 460.3,
829.1, 1581.7, 2398.5, 2402.3, 0, 60.8, 105.8, 215.8, 466.6,
826, 828.3, 1570.8, 2405.6, 2406.1, 2408.8, 0, 52.6, 106.9,
206.5, 414.3, 868.4, 1629.9, 1655.1, 2409.1, 2413, 0, 49.5,
100.6, 232.9, 389.4, 808.2, 1588.2, 2412.4, 2413.3, 2415.9,
0.1, 70.9, 110.5, 208.4, 409, 807.5, 1579.9, 2382.2, 2382.5,
2383.6, 2383.8, 0, 61.5, 106.5, 213.9, 473.8, 814.2, 1561.9,
2390.7, 2391.9, 2393.1, 0, 59.9, 64, 112, 216, 397.6, 807.4,
1625, 2392.3, 2395.1, 0, 74, 108.8, 109.7, 236.1, 433.6,
794.7, 1590.3, 2381.9, 2382.5, 0.1, 56.3, 114.5, 254.1, 487.7,
864.3, 1593.5, 2369.3, 2369.3, 2372.3, 2373.9, 0.2, 57.1,
110, 201.4, 402.7, 807.2, 1572.9, 2392.8, 2393.5, 0.1, 56.4,
122.5, 224.5, 420.2, 853.7, 1502.1, 2390.3, 2392.9, 0, 50.5,
53.7, 118.2, 230, 462.8, 794.3, 1513.4, 2391.4, 2392.3, 2393.4,
2393.4, 2394.1, 0.1, 49.7, 98.3, 208.3, 383.2, 850.7, 1653.5,
2395.3, 2396, 2397.1, 0, 48.4, 121.2, 228.8, 423.9, 817,
1708.5, 2389.9, 2389.9, 0, 66.4, 129.7, 209.4, 431.5, 794.1,
1673.7, 2383.7, 2384.2, 0, 57, 122.6, 215, 434.1, 838.5,
1657.5, 2386.4, 0.1, 22.6, 127.8, 220.4, 404.3, 810.9, 1592.3,
2386.7, 2388.7, 0, 49.8, 119.7, 200.5, 463.8, 828.7, 1560.7,
2384.5, 2385.7, 2391.2, 0, 73.1, 138.2, 226.6, 408.5, 815.3,
1627.3, 2390.2, 2395.4, 0, 61.2, 108.8, 233.8, 417.7, 824.5,
1502.7, 2395, 2396.2, 0, 56, 101.4, 226.3, 282.1, 412.9,
873.8, 1672.6, 2380.4, 2380.9, 2381.5, 0.1, 70.7, 138, 246,
444.4, 817.1, 1643.2, 2391.5, 2391.8, 2392), Pn = c(-0.95,
0.75, 0.94, 1.27, 1.5, 1.9, 2.14, 2.35, 2.38, 1.48, 3.51,
3.7, 3.99, 4.4, 4.32, 4.52, 4.73, 4.72, 1.97, 3.24, 4.23,
4.35, 4.41, 4.66, 4.57, 4.68, 4.88, 1.16, 3.64, 4.05, 4.75,
5.42, 5.57, 5.55, 5.89, 5.8, 1.48, 3.89, 4.7, 5.34, 5.47,
5.62, 5.71, 5.7, 6.08, 1.26, 0.59, 2.96, 4.34, 5, 4.82, 5.22,
5.2, 5.33, 5.51, 1.2, 2.95, 3.67, 3.9, 4.06, 4.59, 4.6, 4.62,
2.01, 1.92, 2.41, 2.19, 2.22, 2.41, 2.21, 1.6, 3.29, 3.97,
4.39, 4.89, 5.12, 4.93, 5.12, 5.1, 2.39, 3.84, 4.45, 4.63,
4.43, 4.93, 4.78, 4.73, 5.04, 3.09, 3.74, 4.03, 3.89, 4.52,
4.43, 4.24, 4.26, 1.5, 2.73, 2.83, 3.14, 2.89, 3.39, 2.89,
2.84, 3.34, 3.11, 3.16, 3.31, 0.1, 1.17, 1.72, 1.61, 1.64,
2.06, 2.17, 1.99, 2.31, 2.14, 2.27, 2.08, 0.17, 1.17, 1.32,
1.33, 1.4, 1.8, 1.48, 2, 1.81, 1.95, 2.09, 1.73, 1.85, 2.95,
4.33, 4.82, 4.98, 4.97, 5.03, 5.08, 5.22, 5.32, 4.88, 2.17,
3.08, 3.32, 3.42, 3.45, 3.67, 3.64, 3.71, 3.71, 2.85, 2.33,
3.15, 2.81, 3.22, 2.99, 3.16, 3.33, 3.56, 3.61, 3.63, 2.52,
3.55, 4.07, 4.1, 4.17, 4.41, 4.53, 4.56, 2.06, 2.57, 2.91,
2.61, 3.08, 3.29, 3.99, 6.49, 5.23, 6.08, 5.74, 4.41, 6.5,
1.59, 3.22, 3.59, 3.75, 3.84, 4.5, 4.93, 6.87, 6.75, 6.97,
6.53, 6.04, 6.82, 1.28, 3.56, 4.39, 5.27, 5.51, 6.38, 7.05,
7.46, 7.16, 7.24, 0.87, 2.45, 3.86, 4.32, 4.57, 4.43, 4.68,
4.71, 4.86, 4.36, 4.68, 1.06, 2.79, 4.05, 4.86, 5.48, 5.9,
6.38, 6.79, 7.46, 7.12, 7.03, 2.76, 3.92, 3.96, 4.07, 4.2,
4.5, 4.91, 5.52, 5.49, 5.33, 2.84, 4.78, 4.83, 4.76, 4.74,
4.84, 5.19, 5.59, 5.74, 5.7, 5.65, 3.02, 3.61, 4.14, 4.23,
4.45, 4.37, 4.5, 4.6, 4.78, 4.79, 4.85, 2.71, 4.26, 5.42,
6.24, 6.58, 6.63, 6.55, 7.29, 7.43, 7.24, 7, 3.36, 2.19,
2.86, 2.87, 2.37, 3.16, 2.68, 3, 3.4, 3.6, 4.35, 1.28, 2.62,
2.92, 3.3, 3.35, 3.58, 3.73, 4.02, 4, 3.7, 3.75, 1.61, 2.26,
2.5, 2.52, 2.71, 2.61, 2.75, 3.19, 2.92, 3.99, 4.36, 3.67,
4.14, 4.37, -0.28, 1.91, 2.78, 2.84, 2.96, 3.04, 3.24, 3.44,
3.58, 1.78, 4.12, 4.58, 4.33, 4.8, 4.7, 5.02, 5.09, 5.22,
2.79, 4.71, 4.89, 4.93, 4.87, 4.92, 4.83, 4.81, 1.66, 3,
4.04, 4.35, 4.56, 4.75, 4.75, 4.66, 4.89, 1.56, 2.77, 3.86,
3.58, 3.7, 3.76, 3.58, 4.55, 4.63, 4.05, 3.73, 1.76, 2.71,
2.98, 3.01, 3.06, 3.22, 2.99, 3.15, 3.32, 3.34, 1.58, 3.76,
4.97, 5.21, 5.29, 5.5, 5.59, 5.71, 5.74, 1.89, 2.67, 3.01,
3.14, 3.39, 3.57, 3.45, 3.91, 4.11, 3.94, 1.15, 2.88, 3.63,
4.32, 4.09, 4.43, 4.58, 4.61, 4.63, 1.23, 2.26, 3.15, 3.33,
3.3, 3.61, 3.46, 3.65, 3.67, 0.19, 2.23, 3.43, 4.1, 4.85,
5.21, 5.8, 6.27, 6.34, 6.08, 1.94, 3.72, 4.88, 5.51, 6.71,
6.51, 6.96, 7.01, 7.4, 0.48, 2.29, 2.5, 2.87, 3.18, 3.51,
3.13, 3.86, 4.13, 4.34, 4.03, 1.63, 3.64, 5.15, 5.95, 6.43,
6.57, 6.61, 6.51, 6.65, 6.56, 1.93, 3.95, 4.63, 5.66, 6.03,
6.28, 6.67, 6.69, 6.95, 6.75, 0.93, 3.14, 3.46, 3.9, 4.19,
4.27, 4.77, 5.39, 5.36, 5.24, 5.02, 1.71, 3.31, 3.86, 4.02,
4.02, 4.29, 4.36, 4.73, 4.88, 4.59, 1.63, 2.65, 2.63, 2.48,
2.93, 3.45, 4.01, 4.67, 5.02, 5.08, 1.93, 3.54, 3.8, 3.81,
4.04, 4.17, 4.38, 4.55, 4.99, 4.99, 1.29, 2.73, 3.32, 3.66,
3.77, 3.79, 4.14, 4.37, 4.22, 4.1, 4.14, 1.06, 2.89, 3.65,
4.01, 4.11, 4.19, 4.66, 5.03, 5.12, 0.97, 2.45, 2.99, 3.32,
3.34, 3.35, 3.47, 3.12, 3.38, 2.29, 1.72, 4.33, 5.49, 6.44,
6.96, 7.91, 7.49, 8.45, 8.21, 8.17, 8.71, 8.35, 0.29, 2.99,
3.93, 4.52, 5.69, 6.23, 6.23, 6.81, 6.96, 6.68, 0.99, 3.67,
4.62, 5.52, 5.86, 6.23, 5.91, 6.64, 6.29, -0.08, 3.34, 4.89,
6.02, 6.37, 6.59, 6.99, 6.95, 7.2, 0.99, 2.28, 2.72, 2.67,
2.99, 3.18, 3.55, 3.58, 1.31, 2.18, 5.55, 7.37, 8.42, 9.14,
9.44, 9.26, 9.5, 1.23, 3.11, 5.01, 6.21, 7.14, 7.44, 7.79,
7.73, 8.1, 7.96, 1.35, 3.33, 5.67, 6.58, 7.05, 7.36, 7.73,
7.75, 7.99, 0.4, 2.25, 2.83, 3.31, 3.55, 3.66, 3.96, 3.54,
3.77, 1.46, 2.91, 3.51, 3.64, 4.5, 3.83, 3.96, 4.17, 4.66,
4.09, 4.44, 2.41, 4.77, 5.49, 6.05, 6.15, 6.28, 6.6, 6.76,
6.75, 6.78)), .Names = c("Species", "PAR", "Pn"), class = "data.frame", row.names = c(NA,
-628L))

Yes, we can! ;-)
In principle, you were attempting to do the right thing but a few aspects were not quite correct. The main issue is how you pass around the data and the formula: As mob() does not know anything about the way nls() specifies its formulas, a plain formula Pn ~ PAR | Species needs to be used and then the fit function needs to know what to do with the data. The pre-processing offered by mob() can either set up a model matrix (with intercept, dummy/contrast codings, etc.) or a model frame (where factors are still factors etc.). In this case it is easiest to use the default model matrix and then to omit the intercept in the fitting function.
The second problem with your code was that you used the extended specification of the fit function (with estfun and object arguments) but only supplied the fitted model object. With that specification mob() expects that the fit function sets up a suitable list with coefficients and objfun etc.
In combination, this means that your fit function should look like this:
photofit <- function(y, x = NULL, start = NULL, weights = NULL, offset = NULL, ...,
estfun = FALSE, object = FALSE)
{
## only use first real regressor (without intercept)
x <- x[, 2]
## obtain starting values if necessary
if(is.null(start)) {
aux_lm <- lm(y ~ x)
aux_seg_2 <- segmented::segmented(aux_lm, seg.Z = ~ x, psi = 1)
aux_seg_1 <- segmented::segmented(aux_lm, seg.Z = ~ x, psi = aux_seg_2$psi[1, 2])
start <- list(
a = -1 * (segmented::intercept(aux_seg_1)[[1]][1, 1]),
b = segmented::slope(aux_seg_1)[[1]][1, 1],
c = -1 * (segmented::intercept(aux_seg_1)[[1]][2, 1]),
d = -1 * segmented::slope(aux_seg_1)[[1]][2, 1]
)
} else {
start <- as.list(start)
}
## estimate NLS model
rval <- nls(y ~ (a * exp(-b * x) - c * exp(-d * x)), start = start)
## return processed information for mob()
list(
coefficients = coef(rval),
objfun = deviance(rval),
estfun = if(estfun) sandwich::estfun(rval) else NULL,
object = if(object) rval else NULL
)
}
And then you can grow the MOB tree. Specifying the verbose = TRUE control option will give you a little bit of progress information while you wait:
photomob <- mob(Pn ~ PAR | Species, data = eco, fit = photofit,
control = mob_control(verbose = TRUE))
coef(photomob)
## a b c d
## 4 2.967680 -3.216708e-05 1.519680 1.076879e+01
## 5 -1.811596 1.967366e-02 -3.573079 -4.877852e-05
## 6 -2.772783 1.438087e-02 -4.177953 -7.821814e-05
## 8 -2.427253 1.757744e-02 -4.449105 -1.328930e-04
## 9 -4.579248 1.020021e-02 -5.714575 -7.502393e-05
You can then also visualize the tree. By default a numeric summary is shown in each node but you can also easily display the fitted curves:
plot(photomob)
plot(photomob, terminal_panel = node_bivplot, tnex = 2)
As you see the tree selected five terminal nodes with different parameters. I would recommend that you do some more diagnostics on the model fits in the different nodes because I'm not sure how well all parameters are identified. I'm not very familiar with NLS and might be completely wrong but it seems that not always all parameters can be reliably determined.
As one illustration I do the following: I extract all nine fitted nls objects from the tree. For the model from the root node (node 1) I compute the gradient by summing over all observation-wise gradient contributions (as computed by the estfun() method):
photonls <- refit.modelparty(photomob)
library("sandwich")
colSums(estfun(photonls[[1]]))
## a b c d
## 2.010552e-05 5.753230e-02 -1.166331e-04 6.771585e+00
The gradients of parameters a-c are reasonably close to zero but for d it isn't. This may also affect the inference in mob() which is based on the observation-wise gradient contributions (aka model scores or estimating functions).
In short: What you want to do, can be done! But I would recommend considering a simpler model. If you do, you just need to modify the photofit() function accordingly and run it through mob() again.

Related

Weighted proportions and confidence intervals

I have tried to follow this post to calculate a weighted proportion and standard error. However, the answer provided did not have a lot of explanation so I was unsure if my calculations were correct.
I would love confirmation that what I've done is indeed correct, or alternate ways to achieve my desired outcome if incorrect?
# Test data
test <- structure(list(koala = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "Gendry", class = "factor"),
koala.pres = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 3L, 2L, 2L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 3L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 3L, 3L, 2L, 3L,
2L, 2L, 3L, 2L, 1L, 1L, 1L, 3L, 3L, 2L, 3L, 2L, 2L, 1L, 1L,
1L, 1L), .Label = c("Absent", "Day", "Night"), class = "factor"),
habitat = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L), .Label = c("Exposed Sandstone Scribbly Gum", "Sheltered sandstone Blue leafed stringybark forest",
"Transitional Shale Dry Ironbark Forest"), class = "factor"),
tree.sp = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 7L, 7L, 7L, 9L, 9L,
9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 12L, 12L, 13L, 13L,
13L, 13L, 13L, 14L, 14L, 14L, 14L, 2L, 2L, 2L, 7L, 7L, 7L,
9L, 13L, 1L, 1L, 1L, 2L, 2L, 10L, 11L, 11L, 13L, 13L, 1L,
1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L,
5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L,
6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L,
7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 8L, 8L, 8L,
8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L,
8L, 8L, 8L, 8L, 8L, 8L, 8L, 11L, 11L, 11L, 11L, 11L, 11L,
11L, 11L, 11L, 11L, 12L, 12L, 12L, 12L), .Label = c("A. littoralis",
"C. gummifera", "E. amplifolia", "E. beyeriana", "E. crebra",
"E. fibrosa", "E. globoidea", "E. longifolia", "E. oblonga",
"E. piperita", "E. punctata", "E. resinifera", "E. sclerophylla",
"E. sieberi"), class = "factor"), cbh = c(0.76, 0.98, 0.42,
0.34, 0.4, 0.44, 0.45, 0.47, 0.66, 0.59, 0.99, 0.43, 0.35,
0.36, 0.4, 0.46, 0.52, 0.49, 0.4, 1.56, 1.26, 0.83, 1.1,
1.22, 1.04, 1.04, 1.08, 1.7, 1.35, 1.89, 0.88, 0.63, 1.26,
0.45, 1.2, 1.33, 0.41, 1.22, 0.75, 0.32, 0.52, 0.6, 1.37,
1.51, 1.29, 0.51, 0.46, 0.44, 2.35, 1.68, 1.24, 0.58, 0.53,
0.69, 0.45, 0.5, 0.5, 0.51, 1.46, 1.23, 0.32, 1.47, 2.27,
0.41, 0.59, 0.61, 0.83, 0.56, 0.41, 0.47, 0.6, 0.35, 1.91,
0.65, 0.52, 1.41, 0.95, 0.91, 1.51, 1.08, 0.95, 0.52, 1.7,
0.76, 1.03, 0.88, 1.45, 1.81, 0.4, 0.39, 0.34, 0.35, 0.89,
0.8, 1.1, 1.77, 0.52, 1.23, 0.49, 0.46, 2.27, 0.41, 1.4,
0.58, 0.66, 0.41, 0.44, 0.87, 0.51, 0.57, 0.78, 1.18, 1.41,
1.13, 1, 1.48, 1.48, 0.4, 1.8, 0.78, 0.82, 1.23, 1.51, 3.82,
0.51, 1.59, 0.95, 1.04, 1.98, 1.3, 0.88, 0.52, 1, 1.27, NA,
1.07, 0.35, 1.33, 0.45, 0.63, 0.45, 0.32, 0.56, 0.68, 1.67,
1.3, 1.83, 0.58, 0.56, 0.44, 0.9, 0.99, 0.59, 0.63, 2.53,
1.33, 2.1, 0.91, 1.24, 1.13, 1.22, 1.64, 2.35, 1.07, 1.27,
1.4, 1.88, 0.56, 1.86, 1.3, 1.97, 0.92, 1.23, 0.34, 0.8),
dbh = c(0.2419, 0.3119, 0.1337, 0.1082, 0.1273, 0.1401, 0.1432,
0.1496, 0.2101, 0.1878, 0.3151, 0.1369, 0.1114, 0.1146, 0.1273,
0.1464, 0.1655, 0.156, 0.1273, 0.4966, 0.4011, 0.2642, 0.3501,
0.3883, 0.331, 0.331, 0.3438, 0.5411, 0.4297, 0.6016, 0.2801,
0.2005, 0.4011, 0.1432, 0.382, 0.4234, 0.1305, 0.3883, 0.2387,
0.1019, 0.1655, 0.191, 0.4361, 0.4806, 0.4106, 0.1623, 0.1464,
0.1401, 0.748, 0.5348, 0.3947, 0.1846, 0.1687, 0.2196, 0.1432,
0.1592, 0.1592, 0.1623, 0.4647, 0.3915, 0.1019, 0.4679, 0.7226,
0.1305, 0.1878, 0.1942, 0.2642, 0.1783, 0.1305, 0.1496, 0.191,
0.1114, 0.608, 0.2069, 0.1655, 0.4488, 0.3024, 0.2897, 0.4806,
0.3438, 0.3024, 0.1655, 0.5411, 0.2419, 0.3279, 0.2801, 0.4615,
0.5761, 0.1273, 0.1241, 0.1082, 0.1114, 0.2833, 0.2546, 0.3501,
0.5634, 0.1655, 0.3915, 0.156, 0.1464, 0.7226, 0.1305, 0.4456,
0.1846, 0.2101, 0.1305, 0.1401, 0.2769, 0.1623, 0.1814, 0.2483,
0.3756, 0.4488, 0.3597, 0.3183, 0.4711, 0.4711, 0.1273, 0.573,
0.2483, 0.261, 0.3915, 0.4806, 1.2159, 0.1623, 0.5061, 0.3024,
0.331, 0.6303, 0.4138, 0.2801, 0.1655, 0.3183, 0.4043, NA,
0.3406, 0.1114, 0.4234, 0.1432, 0.2005, 0.1432, 0.1019, 0.1783,
0.2165, 0.5316, 0.4138, 0.5825, 0.1846, 0.1783, 0.1401, 0.2865,
0.3151, 0.1878, 0.2005, 0.8053, 0.4234, 0.6685, 0.2897, 0.3947,
0.3597, 0.3883, 0.522, 0.748, 0.3406, 0.4043, 0.4456, 0.5984,
0.1783, 0.5921, 0.4138, 0.6271, 0.2928, 0.3915, 0.1082, 0.2546
), tree.hgt = c(11.2, 9, 9.2, 6.8, 6.2, 6, 6, 6.3, 12.2,
12, 16.5, 7.4, 6.2, 9.8, 9.7, 6, 9, 7.8, 9.2, 16.6, 16.6,
13.8, 14.5, 8.4, 14.2, 15.6, 15.8, 17.8, 14.2, 17.2, 11.6,
11, 16.2, 10.6, 16.2, 14.2, 7.2, 10.2, 12.4, 9.2, 8, 16,
16.8, 15.4, 15.2, 6.6, 6.8, 7.8, 16.3, 17, 12.4, 10.8, 11,
12, 8, 9, 11.2, 14.4, 14.4, 10, 7, 15.6, 18, 6.8, 9, 6, 9.4,
10, 8.2, 8.4, 9, 6, 18.8, 12.2, 7.2, 9.4, 19.2, 14.8, 21.4,
17.4, 17.8, 11.8, 17.8, 13, 14, 14.4, 16.7, 18, 7, 7.2, 5.5,
9.2, 9.6, 14, 16, 19.2, 11, 15.5, 7.2, 9, 19.5, 7.2, 23,
17.6, 11.8, 7.2, 7.5, 14, 11.6, 9.3, 16.8, 16.6, 15, 18.6,
22.8, 20, 19.8, 9, 18.2, 14, 19.2, 16.4, 19.8, 5.8, 11.8,
17.6, 17.8, 14.6, 17.6, 16.9, 16.3, 10.8, 17.8, 17, 20, 15,
8.4, 20.6, 9.2, 14, 8.5, 8.2, 11.2, 6.6, 18.4, 18.4, 21,
9.8, 9.2, 9, 15.2, 17.2, 10.4, 8.8, 19.2, 19, 25, 14.9, 19,
17.8, 11.3, 20, 23, 12, 17.9, 17.9, 15.2, 8, 17, 13, 14,
18, 19.4, 5.4, 16), rel.abu.tree.in.hr = c(18.7, 18.7, 18.7,
18.7, 18.7, 18.7, 18.7, 18.7, 18.7, 18.7, 18.7, 18.7, 18.7,
18.7, 18.7, 18.7, 18.7, 18.7, 18.7, 17.6, 17.6, 17.6, 4.78,
4.78, 4.78, 4.78, 4.78, 4.78, 4.78, 4.78, 4.78, 4.78, 4.78,
4.78, 0.74, 0.74, 2.7, 2.7, 2.7, 2.7, 2.7, 1.47, 1.47, 1.47,
1.47, 18.7, 18.7, 18.7, 17.6, 17.6, 17.6, 4.78, 2.7, 0.78,
0.78, 0.78, 18.7, 18.7, 0.26, 3.4, 3.4, 2.7, 2.7, 0.78, 0.78,
18.7, 18.7, 18.7, 18.7, 18.7, 18.7, 18.7, 18.7, 18.7, 18.7,
18.7, 0.004, 9.19, 9.19, 9.19, 9.19, 9.19, 9.19, 9.19, 9.19,
9.19, 9.19, 9.19, 9.19, 9.19, 9.19, 9.19, 9.19, 9.19, 9.19,
9.19, 9.19, 9.19, 14.7, 14.7, 14.7, 14.7, 14.7, 14.7, 14.7,
14.7, 14.7, 14.7, 14.7, 14.7, 14.7, 14.7, 14.7, 17.6, 17.6,
17.6, 17.6, 17.6, 17.6, 17.6, 17.6, 17.6, 17.6, 17.6, 17.6,
17.6, 17.6, 17.6, 17.6, 17.6, 17.6, 17.6, 17.6, 17.6, 17.6,
17.6, 16.53, 16.53, 16.53, 16.53, 16.53, 16.53, 16.53, 16.53,
16.53, 16.53, 16.53, 16.53, 16.53, 16.53, 16.53, 16.53, 16.53,
16.53, 16.53, 16.53, 16.53, 16.53, 16.53, 16.53, 16.53, 3.4,
3.4, 3.4, 3.4, 3.4, 3.4, 3.4, 3.4, 3.4, 3.4, 0.74, 0.74,
0.74, 0.74), prop.hab.class.in.hr = c(18.42105263, 18.42105263,
18.42105263, 18.42105263, 18.42105263, 18.42105263, 18.42105263,
18.42105263, 18.42105263, 18.42105263, 18.42105263, 18.42105263,
18.42105263, 18.42105263, 18.42105263, 18.42105263, 18.42105263,
18.42105263, 18.42105263, 18.42105263, 18.42105263, 18.42105263,
18.42105263, 18.42105263, 18.42105263, 18.42105263, 18.42105263,
18.42105263, 18.42105263, 18.42105263, 18.42105263, 18.42105263,
18.42105263, 18.42105263, 18.42105263, 18.42105263, 18.42105263,
18.42105263, 18.42105263, 18.42105263, 18.42105263, 18.42105263,
18.42105263, 18.42105263, 18.42105263, 18.42105263, 18.42105263,
18.42105263, 18.42105263, 18.42105263, 18.42105263, 18.42105263,
18.42105263, 2.631578947, 2.631578947, 2.631578947, 2.631578947,
2.631578947, 2.631578947, 2.631578947, 2.631578947, 2.631578947,
2.631578947, 78.94736842, 78.94736842, 78.94736842, 78.94736842,
78.94736842, 78.94736842, 78.94736842, 78.94736842, 78.94736842,
78.94736842, 78.94736842, 78.94736842, 78.94736842, 78.94736842,
78.94736842, 78.94736842, 78.94736842, 78.94736842, 78.94736842,
78.94736842, 78.94736842, 78.94736842, 78.94736842, 78.94736842,
78.94736842, 78.94736842, 78.94736842, 78.94736842, 78.94736842,
78.94736842, 78.94736842, 78.94736842, 78.94736842, 78.94736842,
78.94736842, 78.94736842, 78.94736842, 78.94736842, 78.94736842,
78.94736842, 78.94736842, 78.94736842, 78.94736842, 78.94736842,
78.94736842, 78.94736842, 78.94736842, 78.94736842, 78.94736842,
78.94736842, 78.94736842, 78.94736842, 78.94736842, 78.94736842,
78.94736842, 78.94736842, 78.94736842, 78.94736842, 78.94736842,
78.94736842, 78.94736842, 78.94736842, 78.94736842, 78.94736842,
78.94736842, 78.94736842, 78.94736842, 78.94736842, 78.94736842,
78.94736842, 78.94736842, 78.94736842, 78.94736842, 78.94736842,
78.94736842, 78.94736842, 78.94736842, 78.94736842, 78.94736842,
78.94736842, 78.94736842, 78.94736842, 78.94736842, 78.94736842,
78.94736842, 78.94736842, 78.94736842, 78.94736842, 78.94736842,
78.94736842, 78.94736842, 78.94736842, 78.94736842, 78.94736842,
78.94736842, 78.94736842, 78.94736842, 78.94736842, 78.94736842,
78.94736842, 78.94736842, 78.94736842, 78.94736842, 78.94736842,
78.94736842, 78.94736842, 78.94736842, 78.94736842, 78.94736842,
78.94736842, 78.94736842, 78.94736842), k.pres = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L,
1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 2L,
2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L), .Label = c("0", "1"), class = "factor"),
rel_abun = c(344.473684181, 344.473684181, 344.473684181,
344.473684181, 344.473684181, 344.473684181, 344.473684181,
344.473684181, 344.473684181, 344.473684181, 344.473684181,
344.473684181, 344.473684181, 344.473684181, 344.473684181,
344.473684181, 344.473684181, 344.473684181, 344.473684181,
324.210526288, 324.210526288, 324.210526288, 88.0526315714,
88.0526315714, 88.0526315714, 88.0526315714, 88.0526315714,
88.0526315714, 88.0526315714, 88.0526315714, 88.0526315714,
88.0526315714, 88.0526315714, 88.0526315714, 13.6315789462,
13.6315789462, 49.736842101, 49.736842101, 49.736842101,
49.736842101, 49.736842101, 27.0789473661, 27.0789473661,
27.0789473661, 27.0789473661, 344.473684181, 344.473684181,
344.473684181, 324.210526288, 324.210526288, 324.210526288,
88.0526315714, 49.736842101, 2.05263157866, 2.05263157866,
2.05263157866, 49.2105263089, 49.2105263089, 0.68421052622,
8.9473684198, 8.9473684198, 7.1052631569, 7.1052631569, 61.5789473676,
61.5789473676, 1476.315789454, 1476.315789454, 1476.315789454,
1476.315789454, 1476.315789454, 1476.315789454, 1476.315789454,
1476.315789454, 1476.315789454, 1476.315789454, 1476.315789454,
0.31578947368, 725.5263157798, 725.5263157798, 725.5263157798,
725.5263157798, 725.5263157798, 725.5263157798, 725.5263157798,
725.5263157798, 725.5263157798, 725.5263157798, 725.5263157798,
725.5263157798, 725.5263157798, 725.5263157798, 725.5263157798,
725.5263157798, 725.5263157798, 725.5263157798, 725.5263157798,
725.5263157798, 725.5263157798, 1160.526315774, 1160.526315774,
1160.526315774, 1160.526315774, 1160.526315774, 1160.526315774,
1160.526315774, 1160.526315774, 1160.526315774, 1160.526315774,
1160.526315774, 1160.526315774, 1160.526315774, 1160.526315774,
1160.526315774, 1389.473684192, 1389.473684192, 1389.473684192,
1389.473684192, 1389.473684192, 1389.473684192, 1389.473684192,
1389.473684192, 1389.473684192, 1389.473684192, 1389.473684192,
1389.473684192, 1389.473684192, 1389.473684192, 1389.473684192,
1389.473684192, 1389.473684192, 1389.473684192, 1389.473684192,
1389.473684192, 1389.473684192, 1389.473684192, 1389.473684192,
1304.9999999826, 1304.9999999826, 1304.9999999826, 1304.9999999826,
1304.9999999826, 1304.9999999826, 1304.9999999826, 1304.9999999826,
1304.9999999826, 1304.9999999826, 1304.9999999826, 1304.9999999826,
1304.9999999826, 1304.9999999826, 1304.9999999826, 1304.9999999826,
1304.9999999826, 1304.9999999826, 1304.9999999826, 1304.9999999826,
1304.9999999826, 1304.9999999826, 1304.9999999826, 1304.9999999826,
1304.9999999826, 268.421052628, 268.421052628, 268.421052628,
268.421052628, 268.421052628, 268.421052628, 268.421052628,
268.421052628, 268.421052628, 268.421052628, 58.4210526308,
58.4210526308, 58.4210526308, 58.4210526308)), row.names = c(NA,
-175L), class = "data.frame")
# Calculate a weighted proportion for test$tree.sp
# Weighting variable is test$rel.abu.tree.in.hr
# Calculate weighted proportion
library(survey)
dsurvey <- svydesign(ids = ~1, data = test, weights = ~rel.abu.tree.in.hr)
wpct <- data.frame(svymean(~tree.sp, design = dsurvey))
Outcome of above
wpct
mean SE
tree.spA. littoralis 1.830415e-03 8.345005e-04
tree.spC. gummifera 3.071812e-01 4.180415e-02
tree.spE. amplifolia 1.877349e-06 1.889682e-06
tree.spE. beyeriana 4.744530e-02 1.424895e-02
tree.spE. crebra 4.313209e-02 1.359431e-02
tree.spE. fibrosa 1.034889e-01 2.547820e-02
tree.spE. globoidea 2.395497e-01 3.825613e-02
tree.spE. longifolia 1.939536e-01 3.472975e-02
tree.spE. oblonga 2.916462e-02 8.264006e-03
tree.spE. piperita 1.220277e-04 1.228147e-04
tree.spE. punctata 1.914896e-02 5.681831e-03
tree.spE. resinifera 2.083857e-03 8.701564e-04
tree.spE. sclerophylla 1.013768e-02 3.663735e-03
tree.spE. sieberi 2.759703e-03 1.400363e-03

Create boxplots with groups spread across multiple columns

I'm using the weightloss dataset:
structure(list(id = 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), .Label = c("1", "2",
"3", "4", "5", "6", "7", "8", "9", "10", "11", "12"), class = "factor"),
diet = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("no", "yes"), class = "factor"),
exercises = 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,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("no", "yes"
), class = "factor"), t1 = c(10.43, 11.59, 11.35, 11.12,
9.5, 9.5, 11.12, 12.51, 11.35, 11.12, 11.12, 10.2, 11.12,
9.96, 12.05, 8.11, 12.05, 12.05, 12.28, 10.66, 11.35, 10.2,
10.2, 9.5, 10.2, 12.98, 13.21, 10.2, 11.59, 12.05, 11.59,
12.05, 11.82, 11.12, 12.51, 11.59, 10.43, 11.35, 11.82, 10.2,
13.67, 10.66, 10.2, 12.05, 11.82, 10.43, 12.74, 11.35), t2 = c(13.21,
10.66, 11.12, 9.5, 9.73, 12.74, 12.51, 12.28, 11.59, 10.66,
13.44, 11.35, 12.51, 12.74, 13.67, 14.37, 14.6, 12.98, 12.05,
14.37, 14.6, 11.82, 14.13, 13.21, 12.51, 12.98, 11.12, 9.73,
13.44, 13.67, 12.98, 11.35, 12.05, 15.29, 11.82, 12.05, 12.51,
14.83, 13.9, 13.21, 14.13, 15.06, 12.98, 11.35, 12.51, 14.13,
12.74, 11.35), t3 = c(11.59, 13.21, 11.35, 11.12, 12.28,
10.43, 11.59, 12.74, 9.96, 11.35, 10.66, 11.12, 15.76, 16.68,
17.84, 14.6, 17.84, 17.61, 18.54, 16.91, 15.52, 17.38, 19,
14.13, 14.6, 14.6, 12.05, 15.52, 13.9, 12.74, 13.21, 14.83,
14.6, 10.89, 15.52, 12.98, 14.37, 15.06, 13.44, 14.13, 15.29,
14.6, 15.06, 15.52, 13.9, 14.37, 15.06, 15.06)), class = c("tbl_df",
"tbl", "data.frame"), row.names = c(NA, -48L))
So far it looks like at least here I can separate the scores:
weight <- weightloss
summary <- weight %>%
get_summary_stats(type = "mean_sd")
summary
Which gives me this:
A tibble: 3 x 4
variable n mean sd
<chr> <dbl> <dbl> <dbl>
1 t1 48 11.2 1.09
2 t2 48 12.7 1.42
3 t3 48 14.2 2.26
I'm trying to run a RMANOVA on this, but I would like to get a boxplot for every one of the three groups, all in a single plot. However, I'm not sure how to plot the x and y in this case. I tried using this for the x:
trial_type <- c("t1","t2","t3")
factor(trial_type)
But thats where I'm stuck...I'm not sure how you get the y in this case. The y is clearly the scores from each trial. I tried grouping by this factor to see if that would sort out the scores in some way, but I haven't figured that out either.
I'm just not sure how you plot this into ggplot. Any help would be great! I can imagine this is a very useful skill to learn for any data that uses trials.
You may have to pivot_longer first, then the grouping gets easier.
After pivoting, all values will be on the same column ('values'), and there will be a grouping column ('trial')
library(dplyr)
df<-df %>% pivot_longer(names_to = 'trial', values_to = 'value', cols = matches('t\\d'))
with(df, boxplot(value ~ trial))
If you prefer ggplot:
ggplot(df, aes(x=trial, y=value))+
geom_boxplot()
a ggplot and reshape2 way;
library(reshape2)
df %>%
melt(id.vars='id',measure.vars = c('t1','t2','t3')) %>%
ggplot(aes(x=variable,y=value))+
geom_boxplot(aes(color=variable))

Flexdashboard checkboxGroupInput losing ggplot data

I have an issue that is related to this one, but was unable to come to a solution for mine.
I have a reactive ggplot that I would like to update using a check box based on group data.
Currently, when I have ONE box selected, the data displays correctly. If I select more than one check box, I lose data points. See pictures below. I think I have to change the way I'm filtering my data and use droplevels somewhere but not sure how to integrate that (I'm new to shiny!). Any suggestions are appreciated!
WHOC_Sum_CMJ <- structure(list(Athlete = structure(c(1L, 1L, 1L, 7L, 7L, 7L,
7L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L, 10L, 10L, 10L, 10L, 11L,
11L, 11L, 11L, 12L, 12L, 12L, 12L, 13L, 13L, 13L, 13L, 14L, 14L,
14L, 14L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 5L,
5L, 5L, 5L, 6L, 6L, 6L, 6L), .Label = c("Athlete 1", "Athlete 10",
"Athlete 11", "Athlete 12", "Athlete 13", "Athlete 14", "Athlete 2",
"Athlete 3", "Athlete 4", "Athlete 5", "Athlete 6", "Athlete 7",
"Athlete 8", "Athlete 9"), class = "factor"), Date = structure(c(1L,
4L, 5L, 1L, 3L, 5L, 7L, 2L, 3L, 5L, 7L, 1L, 3L, 5L, 7L, 1L, 3L,
5L, 7L, 1L, 3L, 6L, 7L, 2L, 4L, 5L, 8L, 1L, 3L, 5L, 7L, 1L, 3L,
5L, 7L, 1L, 3L, 5L, 7L, 1L, 3L, 5L, 7L, 1L, 3L, 5L, 7L, 1L, 3L,
6L, 7L, 1L, 3L, 5L, 7L), .Label = c("2020-01-06", "2020-01-07",
"2020-01-13", "2020-01-14", "2020-01-21", "2020-01-23", "2020-01-27",
"2020-01-28"), class = "factor"), Position = structure(c(2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L), .Label = c("DEF", "FWD", "GOALIE"), class = "factor"),
Program = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 1L, 1L, 1L, 1L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 1L,
1L, 1L, 1L, 2L, 2L, 2L, 2L, 4L, 4L, 4L, 4L, 1L, 1L, 1L, 1L,
4L, 4L, 4L, 4L, 1L, 1L, 1L, 1L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L), .Label = c("Navy", "Red", "RTP", "White"), class = "factor"),
mRSI = c(0.36, 0.38, 0.42, 0.46, 0.46, 0.47, 0.48, 0.31,
0.3, 0.24, 0.3, 0.29, 0.26, 0.28, 0.28, 0.36, 0.35, 0.43,
0.43, 0.28, 0.31, 0.28, 0.3, 0.33, 0.36, 0.35, 0.37, 0.37,
0.36, 0.37, 0.36, 0.3, 0.36, 0.34, 0.37, 0.26, 0.28, 0.34,
0.3, 0.39, 0.4, 0.43, 0.43, 0.43, 0.47, 0.46, 0.48, 0.34,
0.36, 0.33, 0.37, 0.28, 0.28, 0.34, 0.33), SystemWeight = c(617.21,
612.4, 620.45, 672.08, 682.23, 670.5, 663.41, 517.33, 515.23,
511.62, 517.85, 697.55, 703.92, 689.43, 691.33, 859.06, 845.9,
850.97, 851.84, 655.79, 665.09, 673.91, 667.92, 626.78, 632.92,
634.52, 624.88, 637.55, 645.6, 648.78, 646.64, 558.03, 563.23,
569.58, 560.95, 693.63, 695.54, 684.37, 684.58, 641.18, 660.8,
663.95, 660, 594.92, 596.97, 591.36, 585.64, 522.35, 518.17,
530.95, 523.5, 780.65, 789.81, 775.84, 775.48), FTCT = c(0.61,
0.62, 0.67, 0.74, 0.75, 0.77, 0.77, 0.54, 0.55, 0.44, 0.53,
0.53, 0.49, 0.53, 0.56, 0.6, 0.58, 0.68, 0.68, 0.53, 0.57,
0.54, 0.55, 0.61, 0.63, 0.64, 0.65, 0.59, 0.58, 0.59, 0.59,
0.51, 0.59, 0.59, 0.59, 0.53, 0.57, 0.63, 0.59, 0.76, 0.76,
0.79, 0.78, 0.67, 0.72, 0.72, 0.74, 0.63, 0.65, 0.61, 0.63,
0.49, 0.5, 0.53, 0.57), JumpHeight_cm = c(28.97, 29.78, 31.43,
35.83, 35.41, 36.59, 36.92, 27.56, 26.11, 26.15, 26.82, 26.15,
25.08, 24.98, 24.62, 29.39, 30.17, 32.42, 32.56, 26.6, 27.25,
25.58, 27.88, 29.17, 31.58, 28.48, 31.24, 33.73, 32.78, 33.09,
33.43, 29.73, 31.91, 30.65, 32.98, 24.15, 24.24, 27.57, 25.44,
26.68, 26.39, 27.43, 28.87, 35.44, 36.29, 35.71, 36.06, 26.79,
27.76, 26.82, 29.71, 28.69, 26.9, 31.12, 29.77), EJH = c(17.6,
18.58, 21.11, 26.66, 26.69, 28.08, 28.38, 14.99, 14.39, 11.41,
14.33, 13.8, 12.34, 13.29, 13.67, 17.58, 17.5, 22.03, 22.19,
14.03, 15.59, 13.92, 15.39, 17.7, 19.75, 18.37, 20.3, 19.99,
18.9, 19.62, 19.61, 15.09, 18.8, 18.18, 19.6, 12.78, 13.87,
17.28, 15.06, 20.44, 20.12, 21.74, 22.52, 23.8, 26.25, 25.68,
26.73, 16.99, 18.13, 16.42, 18.82, 14.09, 13.43, 16.61, 16.9
), Weight = c(62.94, 62.45, 63.27, 68.54, 69.57, 68.38, 67.65,
52.76, 52.54, 52.17, 52.81, 71.13, 71.78, 70.31, 70.5, 87.61,
86.26, 86.78, 86.87, 66.88, 67.82, 68.72, 68.11, 63.92, 64.54,
64.71, 63.72, 65.02, 65.84, 66.16, 65.94, 56.91, 57.44, 58.09,
57.2, 70.74, 70.93, 69.79, 69.81, 65.39, 67.39, 67.71, 67.31,
60.67, 60.88, 60.31, 59.72, 53.27, 52.84, 54.15, 53.39, 79.61,
80.54, 79.12, 79.08)), class = "data.frame", row.names = c(NA,
-55L))
```
checkboxGroupInput("Program", label = "Program", choices = unique(WHOC_Sum_CMJ$Program), selected = "Red", inline = TRUE)
# (Note: for the code I cut out some of the styling to make it more readable. That's why it looks different than the pictures).
```
```
renderPlot({
f <- WHOC_Sum_CMJ %>%
select(Date, Athlete, JumpHeight_cm, Program)%>%
filter(Program == input$Program)
p <- ggplot(f)+
geom_line(aes(x=Date, y=JumpHeight_cm, colour = Athlete))+
geom_point(aes(x=Date, y=JumpHeight_cm, colour = Athlete))+
theme_bw() +
labs(title = "Team Jump Height",
x = "Date",
y = "Jump Height (cm)")+
scale_x_date(limits = c(min = min(WHOC_Sum_CMJ$Date), max = max(WHOC_Sum_CMJ$Date)), labels = date_format("%m/%d"),
date_breaks = "2 weeks", expand = c(.08,0))+
guides(col = guide_legend(nrow = 3))+
geom_text_repel(data= subset(f, Date == min(Date)), aes(x=Date, y=JumpHeight_cm,label = unique(Athlete)),
force = .1,
nudge_x = -2,
direction = "y",
hjust = 1,
)
p
})
The issue in your code indeed is based on the filter call. You'll need to use %in%instead of ==, when filtering a vector of statements. Please see the following:
---
title: "Test"
output: flexdashboard::flex_dashboard
runtime: shiny
---
```{r global, include=FALSE}
library(ggplot2)
library(dplyr)
library(scales)
library(ggrepel)
WHOC_Sum_CMJ <- structure(list(Athlete = structure(c(1L, 1L, 1L, 7L, 7L, 7L,
7L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L, 10L, 10L, 10L, 10L, 11L,
11L, 11L, 11L, 12L, 12L, 12L, 12L, 13L, 13L, 13L, 13L, 14L, 14L,
14L, 14L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 5L,
5L, 5L, 5L, 6L, 6L, 6L, 6L), .Label = c("Athlete 1", "Athlete 10",
"Athlete 11", "Athlete 12", "Athlete 13", "Athlete 14", "Athlete 2",
"Athlete 3", "Athlete 4", "Athlete 5", "Athlete 6", "Athlete 7",
"Athlete 8", "Athlete 9"), class = "factor"), Date = structure(c(1L,
4L, 5L, 1L, 3L, 5L, 7L, 2L, 3L, 5L, 7L, 1L, 3L, 5L, 7L, 1L, 3L,
5L, 7L, 1L, 3L, 6L, 7L, 2L, 4L, 5L, 8L, 1L, 3L, 5L, 7L, 1L, 3L,
5L, 7L, 1L, 3L, 5L, 7L, 1L, 3L, 5L, 7L, 1L, 3L, 5L, 7L, 1L, 3L,
6L, 7L, 1L, 3L, 5L, 7L), .Label = c("2020-01-06", "2020-01-07",
"2020-01-13", "2020-01-14", "2020-01-21", "2020-01-23", "2020-01-27",
"2020-01-28"), class = "factor"), Position = structure(c(2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L), .Label = c("DEF", "FWD", "GOALIE"), class = "factor"),
Program = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 1L, 1L, 1L, 1L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 1L,
1L, 1L, 1L, 2L, 2L, 2L, 2L, 4L, 4L, 4L, 4L, 1L, 1L, 1L, 1L,
4L, 4L, 4L, 4L, 1L, 1L, 1L, 1L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L), .Label = c("Navy", "Red", "RTP", "White"), class = "factor"),
mRSI = c(0.36, 0.38, 0.42, 0.46, 0.46, 0.47, 0.48, 0.31,
0.3, 0.24, 0.3, 0.29, 0.26, 0.28, 0.28, 0.36, 0.35, 0.43,
0.43, 0.28, 0.31, 0.28, 0.3, 0.33, 0.36, 0.35, 0.37, 0.37,
0.36, 0.37, 0.36, 0.3, 0.36, 0.34, 0.37, 0.26, 0.28, 0.34,
0.3, 0.39, 0.4, 0.43, 0.43, 0.43, 0.47, 0.46, 0.48, 0.34,
0.36, 0.33, 0.37, 0.28, 0.28, 0.34, 0.33), SystemWeight = c(617.21,
612.4, 620.45, 672.08, 682.23, 670.5, 663.41, 517.33, 515.23,
511.62, 517.85, 697.55, 703.92, 689.43, 691.33, 859.06, 845.9,
850.97, 851.84, 655.79, 665.09, 673.91, 667.92, 626.78, 632.92,
634.52, 624.88, 637.55, 645.6, 648.78, 646.64, 558.03, 563.23,
569.58, 560.95, 693.63, 695.54, 684.37, 684.58, 641.18, 660.8,
663.95, 660, 594.92, 596.97, 591.36, 585.64, 522.35, 518.17,
530.95, 523.5, 780.65, 789.81, 775.84, 775.48), FTCT = c(0.61,
0.62, 0.67, 0.74, 0.75, 0.77, 0.77, 0.54, 0.55, 0.44, 0.53,
0.53, 0.49, 0.53, 0.56, 0.6, 0.58, 0.68, 0.68, 0.53, 0.57,
0.54, 0.55, 0.61, 0.63, 0.64, 0.65, 0.59, 0.58, 0.59, 0.59,
0.51, 0.59, 0.59, 0.59, 0.53, 0.57, 0.63, 0.59, 0.76, 0.76,
0.79, 0.78, 0.67, 0.72, 0.72, 0.74, 0.63, 0.65, 0.61, 0.63,
0.49, 0.5, 0.53, 0.57), JumpHeight_cm = c(28.97, 29.78, 31.43,
35.83, 35.41, 36.59, 36.92, 27.56, 26.11, 26.15, 26.82, 26.15,
25.08, 24.98, 24.62, 29.39, 30.17, 32.42, 32.56, 26.6, 27.25,
25.58, 27.88, 29.17, 31.58, 28.48, 31.24, 33.73, 32.78, 33.09,
33.43, 29.73, 31.91, 30.65, 32.98, 24.15, 24.24, 27.57, 25.44,
26.68, 26.39, 27.43, 28.87, 35.44, 36.29, 35.71, 36.06, 26.79,
27.76, 26.82, 29.71, 28.69, 26.9, 31.12, 29.77), EJH = c(17.6,
18.58, 21.11, 26.66, 26.69, 28.08, 28.38, 14.99, 14.39, 11.41,
14.33, 13.8, 12.34, 13.29, 13.67, 17.58, 17.5, 22.03, 22.19,
14.03, 15.59, 13.92, 15.39, 17.7, 19.75, 18.37, 20.3, 19.99,
18.9, 19.62, 19.61, 15.09, 18.8, 18.18, 19.6, 12.78, 13.87,
17.28, 15.06, 20.44, 20.12, 21.74, 22.52, 23.8, 26.25, 25.68,
26.73, 16.99, 18.13, 16.42, 18.82, 14.09, 13.43, 16.61, 16.9
), Weight = c(62.94, 62.45, 63.27, 68.54, 69.57, 68.38, 67.65,
52.76, 52.54, 52.17, 52.81, 71.13, 71.78, 70.31, 70.5, 87.61,
86.26, 86.78, 86.87, 66.88, 67.82, 68.72, 68.11, 63.92, 64.54,
64.71, 63.72, 65.02, 65.84, 66.16, 65.94, 56.91, 57.44, 58.09,
57.2, 70.74, 70.93, 69.79, 69.81, 65.39, 67.39, 67.71, 67.31,
60.67, 60.88, 60.31, 59.72, 53.27, 52.84, 54.15, 53.39, 79.61,
80.54, 79.12, 79.08)), class = "data.frame", row.names = c(NA,
-55L))
WHOC_Sum_CMJ$Date <- as.Date(WHOC_Sum_CMJ$Date)
```
Column {.sidebar}
-----------------------------------------------------------------------
```{r}
checkboxGroupInput("Program", label = "Program", choices = unique(WHOC_Sum_CMJ$Program), selected = "Red", inline = TRUE)
# (Note: for the code I cut out some of the styling to make it more readable. That's why it looks different than the pictures).
```
Column
-----------------------------------------------------------------------
```{r}
renderPlot({
f <- WHOC_Sum_CMJ %>%
dplyr::select(Date, Athlete, JumpHeight_cm, Program) %>%
filter(Program %in% input$Program)
p <- ggplot(f) +
geom_line(aes(x=Date, y=JumpHeight_cm, colour = Athlete)) +
geom_point(aes(x=Date, y=JumpHeight_cm, colour = Athlete)) +
theme_bw() +
labs(title = "Team Jump Height",
x = "Date",
y = "Jump Height (cm)") +
scale_x_date(limits = c(min = min(WHOC_Sum_CMJ$Date), max = max(WHOC_Sum_CMJ$Date)), labels = date_format("%m/%d"),
date_breaks = "2 weeks", expand = c(.08,0)) +
guides(col = guide_legend(nrow = 3)) +
geom_text_repel(data= subset(f, Date == min(Date)), aes(x=Date, y=JumpHeight_cm,label = unique(Athlete)),
force = .1,
nudge_x = -2,
direction = "y",
hjust = 1,
)
p
})
```

Barplot: Why do i get a variable spacing between bars in ggplot2 for identical script?

I'm running into something bizarre with ggplot2. I have a loop script that produces barplots and save each one of them as a tiff file. I don't want any spacing between the bars, and I think I have the script written correctly to do just that, but some of my graphs show hair lines between bars and some don't. Because it is a loop, the script and data structure is exactly the same for both of them. Here is a reproducible example:
require(gridExtra); require(ggplot2); require(grid)
prod.data1 = structure(list(date.time = structure(c(1465362000, 1465365600,
1465369200, 1465372800, 1465376400, 1465380000, 1465383600, 1465387200,
1465390800, 1465394400, 1465398000, 1465401600, 1465405200, 1465408800,
1465412400, 1465416000, 1465419600, 1465423200, 1465426800, 1465430400,
1465434000, 1465437600, 1465441200, 1465444800, 1465448400, 1465452000,
1465455600, 1465459200, 1465462800, 1465466400, 1465470000, 1465473600,
1465477200, 1465480800, 1465484400, 1465488000, 1465491600, 1465495200,
1465498800, 1465502400, 1465506000, 1465509600, 1465513200, 1465516800,
1465520400, 1465524000, 1465527600, 1465531200, 1465534800, 1465538400,
1465542000, 1465545600, 1465549200, 1465552800, 1465556400, 1465560000,
1465563600, 1465567200, 1465570800, 1465574400, 1465578000, 1465581600,
1465585200, 1465588800, 1465592400, 1465596000, 1465599600, 1465603200,
1465606800, 1465610400, 1465614000, 1465617600, 1465621200, 1465624800,
1465628400, 1465632000, 1465635600, 1465639200, 1465642800, 1465646400,
1465650000, 1465653600, 1465657200, 1465660800, 1465664400, 1465668000,
1465671600, 1465675200, 1465678800, 1465682400, 1465686000, 1465689600,
1465693200, 1465696800, 1465700400, 1465704000, 1465707600, 1465711200,
1465714800, 1465718400, 1465722000, 1465725600, 1465729200, 1465732800,
1465736400, 1465740000, 1465743600, 1465747200, 1465750800, 1465754400,
1465758000, 1465761600, 1465765200, 1465768800, 1465772400, 1465776000,
1465779600, 1465783200, 1465786800, 1465790400, 1465794000, 1465797600,
1465801200, 1465804800, 1465808400, 1465812000, 1465815600, 1465819200,
1465822800, 1465826400, 1465830000, 1465833600, 1465837200, 1465840800,
1465844400, 1465848000, 1465851600, 1465855200, 1465858800, 1465862400,
1465866000, 1465869600, 1465873200, 1465876800, 1465880400, 1465884000,
1465887600, 1465891200, 1465894800, 1465898400, 1465902000, 1465905600,
1465909200, 1465912800, 1465916400, 1465920000, 1465923600, 1465927200,
1465930800, 1465934400, 1465938000, 1465941600, 1465945200, 1465948800,
1465952400, 1465956000, 1465959600, 1465963200, 1465966800, 1465970400,
1465974000, 1465977600, 1465981200, 1465984800, 1465988400, 1465992000,
1465995600, 1465999200, 1466002800, 1466006400, 1466010000, 1466013600,
1466017200, 1466020800, 1466024400, 1466028000, 1466031600, 1466035200,
1466038800, 1466042400, 1466046000, 1466049600, 1466053200, 1466056800,
1466060400, 1466064000, 1466067600, 1466071200, 1466074800, 1466078400,
1466082000, 1466085600, 1466089200, 1466092800, 1466096400, 1466100000,
1466103600, 1466107200, 1466110800, 1466114400, 1466118000, 1466121600,
1466125200, 1466128800, 1466132400, 1466136000), class = c("POSIXct",
"POSIXt")), production = c(5.9, 5.9, 5.9, 5.9, 5.9, 5.9, 9.9,
9.9, 9.9, 9.9, 9.9, 9.9, 9.9, 9.9, 9.9, 13.4, 13.4, 13.4, 13.4,
13.4, 13.4, 9.9, 5.9, 5.9, 5.9, 5.9, 5.9, 5.9, 5.9, 5.9, 9.9,
9.9, 9.9, 9.9, 9.9, 9.9, 9.9, 9.9, 9.9, 13.4, 13.4, 13.4, 13.4,
13.4, 13.4, 9.9, 5.9, 5.9, 5.9, 5.9, 5.9, 5.9, 5.9, 5.9, 9.9,
9.9, 9.9, 9.9, 9.9, 9.9, 9.9, 9.9, 9.9, 13.4, 13.4, 13.4, 13.4,
13.4, 13.4, 9.9, 5.9, 5.9, 5.9, 5.9, 5.9, 5.9, 5.9, 5.9, 9.9,
9.9, 9.9, 9.9, 9.9, 9.9, 9.9, 9.9, 9.9, 13.4, 13.4, 13.4, 13.4,
13.4, 13.4, 9.9, 5.9, 5.9, 3.1, 3.1, 3.1, 3.1, 3.1, 3.1, 7.1,
7.1, 7.1, 7.1, 7.1, 7.1, 7.1, 7.1, 7.1, 7.1, 7.1, 7.1, 7.1, 7.1,
7.1, 3.1, 3.1, 3.1, 3.1, 3.1, 3.1, 3.1, 3.1, 3.1, 7.1, 7.1, 7.1,
7.1, 7.1, 7.1, 7.1, 7.1, 7.1, 7.1, 7.1, 7.1, 7.1, 7.1, 7.1, 3.1,
3.1, 3.1, 3.1, 3.1, 3.1, 3.1, 3.1, 3.1, 7.1, 7.1, 7.1, 7.1, 7.1,
7.1, 7.1, 7.1, 7.1, 7.1, 7.1, 7.1, 7.1, 7.1, 7.1, 3.1, 3.1, 3.1,
3.1, 3.1, 3.1, 3.1, 3.1, 3.1, 7.1, 7.1, 7.1, 7.1, 7.1, 7.1, 7.1,
7.1, 7.1, 7.1, 7.1, 7.1, 7.1, 7.1, 7.1, 3.1, 3.1, 3.1, 3.1, 3.1,
3.1, 3.1, 3.1, 3.1, 7.1, 7.1, 7.1, 7.1, 7.1, 7.1, 7.1, 7.1, 7.1,
7.1, 7.1, 7.1, 7.1, 7.1, 7.1, 3.1, 3.1, 3.1), ID = structure(c(15L,
15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L,
15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L,
15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L,
15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L,
15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L,
15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L,
15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L,
15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L,
15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L,
15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L,
15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L,
15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L,
15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L,
15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L,
15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L,
15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L,
15L, 15L, 15L, 15L, 15L, 15L, 15L), .Label = c("A", "B", "C","D",
"E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P"),
class = "factor"), ILP = c(10.72,
8.66, 7.75, 7.25, 7.91, 9.12, 10.33, 12.93, 15.7, 15.72, 16.7,
15.34, 14.6, 14.34, 13.09, 11.52, 12.08, 13.29, 14.56, 13.74,
15.46, 15.37, 12.49, 11.11, 7.78, 5.92, 5.92, 5.91, 6.11, 7.01,
10.05, 10.49, 11.43, 11.45, 12.21, 12.79, 12.09, 11.94, 11.99,
10.87, 11.31, 12.08, 13.33, 13.31, 14.23, 13.01, 10.84, 10.07,
7.08, 5.04, 5.01, 5.06, 5.22, 6, 9.92, 10.74, 12.53, 12.43, 13.34,
13.24, 13.52, 12.95, 13.86, 13.09, 13.79, 12.44, 14.14, 14.05,
14.36, 14.45, 11.66, 10.4, 11.3366666666667, 11.3366666666667,
11.3366666666667, 11.3366666666667, 11.3366666666667, 11.3366666666667,
11.3366666666667, 11.3366666666667, 11.3366666666667, 11.3366666666667,
11.3366666666667, 11.3366666666667, 11.3366666666667, 11.3366666666667,
11.3366666666667, 11.3366666666667, 11.3366666666667, 11.3366666666667,
11.3366666666667, 11.3366666666667, 11.3366666666667, 11.3366666666667,
11.3366666666667, 11.3366666666667, 11.3366666666667, 11.3366666666667,
11.3366666666667, 11.3366666666667, 11.3366666666667, 11.3366666666667,
11.3366666666667, 11.3366666666667, 11.3366666666667, 11.3366666666667,
11.3366666666667, 11.3366666666667, 11.3366666666667, 11.3366666666667,
11.3366666666667, 11.3366666666667, 11.3366666666667, 11.3366666666667,
11.3366666666667, 11.3366666666667, 11.3366666666667, 11.3366666666667,
11.3366666666667, 11.3366666666667, 11.3366666666667, 11.3366666666667,
11.3366666666667, 11.3366666666667, 11.3366666666667, 11.3366666666667,
11.3366666666667, 11.3366666666667, 11.3366666666667, 11.3366666666667,
11.3366666666667, 11.3366666666667, 11.3366666666667, 11.3366666666667,
11.3366666666667, 11.3366666666667, 11.3366666666667, 11.3366666666667,
11.3366666666667, 11.3366666666667, 11.3366666666667, 11.3366666666667,
11.3366666666667, 11.3366666666667, 11.3366666666667, 11.3366666666667,
11.3366666666667, 11.3366666666667, 11.3366666666667, 11.3366666666667,
11.3366666666667, 11.3366666666667, 11.3366666666667, 11.3366666666667,
11.3366666666667, 11.3366666666667, 11.3366666666667, 11.3366666666667,
11.3366666666667, 11.3366666666667, 11.3366666666667, 11.3366666666667,
11.3366666666667, 11.3366666666667, 11.3366666666667, 11.3366666666667,
11.3366666666667, 11.3366666666667, 11.3366666666667, 11.3366666666667,
11.3366666666667, 11.3366666666667, 11.3366666666667, 11.3366666666667,
11.3366666666667, 11.3366666666667, 11.3366666666667, 11.3366666666667,
11.3366666666667, 11.3366666666667, 11.3366666666667, 11.3366666666667,
11.3366666666667, 11.3366666666667, 11.3366666666667, 11.3366666666667,
11.3366666666667, 11.3366666666667, 11.3366666666667, 11.3366666666667,
11.3366666666667, 11.3366666666667, 11.3366666666667, 11.3366666666667,
11.3366666666667, 11.3366666666667, 11.3366666666667, 11.3366666666667,
11.3366666666667, 11.3366666666667, 11.3366666666667, 11.3366666666667,
11.3366666666667, 11.3366666666667, 11.3366666666667, 11.3366666666667,
11.3366666666667, 11.3366666666667, 11.3366666666667, 11.3366666666667,
11.3366666666667, 11.3366666666667, 11.3366666666667, 11.3366666666667,
11.3366666666667, 11.3366666666667), Class = structure(c(2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 4L, 4L, 4L, 3L, 3L, 3L, 3L, 2L, 2L, 3L,
3L, 3L, 4L, 3L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 2L, 2L, 3L, 4L, 4L, 4L, 4L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 3L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 2L,
4L, 4L, 4L, 4L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L), .Label = c("NA", "Low", "Medium",
"High"), class = "factor")), .Names = c("date.time", "production",
"ID", "ILP", "Class"), row.names = 3025:3240, class = "data.frame")
prod.data2 = structure(list(date.time = structure(c(1465362000, 1465365600,
1465369200, 1465372800, 1465376400, 1465380000, 1465383600, 1465387200,
1465390800, 1465394400, 1465398000, 1465401600, 1465405200, 1465408800,
1465412400, 1465416000, 1465419600, 1465423200, 1465426800, 1465430400,
1465434000, 1465437600, 1465441200, 1465444800, 1465448400, 1465452000,
1465455600, 1465459200, 1465462800, 1465466400, 1465470000, 1465473600,
1465477200, 1465480800, 1465484400, 1465488000, 1465491600, 1465495200,
1465498800, 1465502400, 1465506000, 1465509600, 1465513200, 1465516800,
1465520400, 1465524000, 1465527600, 1465531200, 1465534800, 1465538400,
1465542000, 1465545600, 1465549200, 1465552800, 1465556400, 1465560000,
1465563600, 1465567200, 1465570800, 1465574400, 1465578000, 1465581600,
1465585200, 1465588800, 1465592400, 1465596000, 1465599600, 1465603200,
1465606800, 1465610400, 1465614000, 1465617600, 1465621200, 1465624800,
1465628400, 1465632000, 1465635600, 1465639200, 1465642800, 1465646400,
1465650000, 1465653600, 1465657200, 1465660800, 1465664400, 1465668000,
1465671600, 1465675200, 1465678800, 1465682400, 1465686000, 1465689600,
1465693200, 1465696800, 1465700400, 1465704000, 1465707600, 1465711200,
1465714800, 1465718400, 1465722000, 1465725600, 1465729200, 1465732800,
1465736400, 1465740000, 1465743600, 1465747200, 1465750800, 1465754400,
1465758000, 1465761600, 1465765200, 1465768800, 1465772400, 1465776000,
1465779600, 1465783200, 1465786800, 1465790400, 1465794000, 1465797600,
1465801200, 1465804800, 1465808400, 1465812000, 1465815600, 1465819200,
1465822800, 1465826400, 1465830000, 1465833600, 1465837200, 1465840800,
1465844400, 1465848000, 1465851600, 1465855200, 1465858800, 1465862400,
1465866000, 1465869600, 1465873200, 1465876800, 1465880400, 1465884000,
1465887600, 1465891200, 1465894800, 1465898400, 1465902000, 1465905600,
1465909200, 1465912800, 1465916400, 1465920000, 1465923600, 1465927200,
1465930800, 1465934400, 1465938000, 1465941600, 1465945200, 1465948800,
1465952400, 1465956000, 1465959600, 1465963200, 1465966800, 1465970400,
1465974000, 1465977600, 1465981200, 1465984800, 1465988400, 1465992000,
1465995600, 1465999200, 1466002800, 1466006400, 1466010000, 1466013600,
1466017200, 1466020800, 1466024400, 1466028000, 1466031600, 1466035200,
1466038800, 1466042400, 1466046000, 1466049600, 1466053200, 1466056800,
1466060400, 1466064000, 1466067600, 1466071200, 1466074800, 1466078400,
1466082000, 1466085600, 1466089200, 1466092800, 1466096400, 1466100000,
1466103600, 1466107200, 1466110800, 1466114400, 1466118000, 1466121600,
1466125200, 1466128800, 1466132400, 1466136000), class = c("POSIXct",
"POSIXt")), production = c(17.9, 17.9, 17.9, 17.9, 17.9, 17.9,
17.9, 17.9, 17.9, 17.9, 17.9, 65.4, 75.7, 89.2, 59.2, 49.2, 17.9,
17.9, 17.9, 17.9, 17.9, 17.9, 17.9, 17.9, 23.2, 23.2, 23.2, 23.2,
23.2, 23.2, 23.2, 23.2, 23.2, 23.2, 23.2, 70.7, 71.7, 98.7, 72.7,
71.7, 23.2, 23.2, 23.2, 23.2, 23.2, 23.2, 23.2, 23.2, 24.2, 24.2,
24.2, 24.2, 24.2, 24.2, 24.2, 24.2, 24.2, 24.2, 24.2, 71.7, 72.2,
97.7, 76.7, 76.7, 32.2, 24.7, 24.2, 24.2, 24.2, 24.2, 24.2, 24.2,
24.7, 24.7, 24.7, 24.7, 24.7, 24.7, 24.7, 24.7, 24.7, 24.7, 66.2,
66.2, 67.2, 101.7, 101.7, 101.7, 60.2, 60.2, 25.7, 24.7, 24.7,
24.7, 24.7, 24.7, 24.7, 24.7, 24.7, 24.7, 24.7, 24.7, 24.7, 24.7,
24.7, 24.7, 66.2, 66.2, 67.2, 98.7, 98.7, 98.7, 57.2, 25.7, 24.7,
24.7, 24.7, 24.7, 24.7, 24.7, 20.7, 20.7, 20.7, 20.7, 20.7, 20.7,
20.7, 20.7, 20.7, 20.7, 62.2, 62.2, 21.2, 56.2, 21.2, 20.7, 20.7,
20.7, 20.7, 20.7, 20.7, 20.7, 20.7, 20.7, 19.7, 19.7, 19.7, 19.7,
19.7, 19.7, 19.7, 19.7, 19.7, 19.7, 19.7, 61.2, 64.7, 43.2, 41.2,
28.2, 23.2, 19.7, 19.7, 19.7, 19.7, 19.7, 19.7, 19.7, 17.9, 17.9,
17.9, 17.9, 17.9, 17.9, 17.9, 17.9, 17.9, 17.9, 17.9, 59.4, 64.7,
32.2, 56.2, 32.2, 23.2, 17.9, 17.9, 17.9, 17.9, 17.9, 17.9, 17.9,
17.9, 17.9, 17.9, 17.9, 17.9, 17.9, 17.9, 17.9, 17.9, 17.9, 17.9,
59.4, 64.7, 73.7, 56.2, 32.2, 23.2, 17.9, 17.9, 17.9, 17.9, 17.9,
17.9, 17.9), ID = structure(c(4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L
), .Label = c("A", "B", "C","D", "E", "F", "G", "H", "I", "J",
"K", "L", "M", "N", "O", "P"), class = "factor"), ILP = c(19.8, 19.06, 14.88,
13.28, 12.3, 13.2, 18.95, 21.19, 21.37, 25, 25.75, 25.57, 25.19,
23.31, 23.4, 22.69, 25.37, 24.96, 22.53, 20.54, 22.21, 20.26,
18.94, 16.29, 16.89, 14.21, 12.72, 11.18, 10.26, 11.69, 15.95,
18.23, 19.11, 19.78, 20.35, 20.15, 21.17, 22.45, 23.7, 24.04,
25.58, 25.35, 21.73, 19.81, 20.77, 21.05, 20.58, 17.48, 16.09,
14.15, 11.13, 10.14, 9.76, 11.16, 14.29, 18.79, 18.99, 19.45,
19.52, 19.45, 20.38, 20.15, 21.52, 20.2, 20.96, 20.56, 18.42,
17.33, 17.31, 16.95, 18.92, 15.59, 18.9094444444444, 18.9094444444444,
18.9094444444444, 18.9094444444444, 18.9094444444444, 18.9094444444444,
18.9094444444444, 18.9094444444444, 18.9094444444444, 18.9094444444444,
18.9094444444444, 18.9094444444444, 18.9094444444444, 18.9094444444444,
18.9094444444444, 18.9094444444444, 18.9094444444444, 18.9094444444444,
18.9094444444444, 18.9094444444444, 18.9094444444444, 18.9094444444444,
18.9094444444444, 18.9094444444444, 18.9094444444444, 18.9094444444444,
18.9094444444444, 18.9094444444444, 18.9094444444444, 18.9094444444444,
18.9094444444444, 18.9094444444444, 18.9094444444444, 18.9094444444444,
18.9094444444444, 18.9094444444444, 18.9094444444444, 18.9094444444444,
18.9094444444444, 18.9094444444444, 18.9094444444444, 18.9094444444444,
18.9094444444444, 18.9094444444444, 18.9094444444444, 18.9094444444444,
18.9094444444444, 18.9094444444444, 18.9094444444444, 18.9094444444444,
18.9094444444444, 18.9094444444444, 18.9094444444444, 18.9094444444444,
18.9094444444444, 18.9094444444444, 18.9094444444444, 18.9094444444444,
18.9094444444444, 18.9094444444444, 18.9094444444444, 18.9094444444444,
18.9094444444444, 18.9094444444444, 18.9094444444444, 18.9094444444444,
18.9094444444444, 18.9094444444444, 18.9094444444444, 18.9094444444444,
18.9094444444444, 18.9094444444444, 18.9094444444444, 18.9094444444444,
18.9094444444444, 18.9094444444444, 18.9094444444444, 18.9094444444444,
18.9094444444444, 18.9094444444444, 18.9094444444444, 18.9094444444444,
18.9094444444444, 18.9094444444444, 18.9094444444444, 18.9094444444444,
18.9094444444444, 18.9094444444444, 18.9094444444444, 18.9094444444444,
18.9094444444444, 18.9094444444444, 18.9094444444444, 18.9094444444444,
18.9094444444444, 18.9094444444444, 18.9094444444444, 18.9094444444444,
18.9094444444444, 18.9094444444444, 18.9094444444444, 18.9094444444444,
18.9094444444444, 18.9094444444444, 18.9094444444444, 18.9094444444444,
18.9094444444444, 18.9094444444444, 18.9094444444444, 18.9094444444444,
18.9094444444444, 18.9094444444444, 18.9094444444444, 18.9094444444444,
18.9094444444444, 18.9094444444444, 18.9094444444444, 18.9094444444444,
18.9094444444444, 18.9094444444444, 18.9094444444444, 18.9094444444444,
18.9094444444444, 18.9094444444444, 18.9094444444444, 18.9094444444444,
18.9094444444444, 18.9094444444444, 18.9094444444444, 18.9094444444444,
18.9094444444444, 18.9094444444444, 18.9094444444444, 18.9094444444444,
18.9094444444444, 18.9094444444444, 18.9094444444444, 18.9094444444444,
18.9094444444444, 18.9094444444444, 18.9094444444444, 18.9094444444444,
18.9094444444444, 18.9094444444444), Class = structure(c(2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 4L, 4L, 4L, 3L, 3L, 3L, 4L, 3L,
3L, 2L, 3L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 3L, 2L, 3L, 3L, 3L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 4L, 3L, 4L, 3L, 4L, 4L,
2L, 2L, 2L, 2L, 3L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L), .Label = c("NA", "Low", "Medium",
"High"), class = "factor")), .Names = c("date.time", "production",
"ID", "ILP", "Class"), row.names = 649:864, class = "data.frame")
prod.data.list = list(prod.data1,prod.data2)
ID = c('D','O')
for(i in 1:length(ID)) {
prod.data = prod.data.list[[i]]
s= ggplot(prod.data, aes(x=date.time, y= production, fill = Class),binwidth=0) +
geom_bar(stat = 'identity')+
scale_fill_manual(values=c("#A8A8A8","#CCFF99", "#FFA31A", "#CC2900")) +
scale_x_datetime() +
theme(legend.position = c(0.06,0.82), legend.key.height=unit(0.5,"line"),legend.key.width=unit(0.3,"line"),legend.background = element_blank(),legend.title = element_text("Class",size=8))
f= ggplot(prod.data, aes(x=date.time, y=ILP)) +
geom_line() +
geom_point() +
scale_x_datetime()
tiff(filename = paste0("Prod_Price_",as.Date(now()),ID[i],".tiff"), width=7,height=4.5,units="in", pointsize=12, bg = "white", res = 150, restoreConsole = TRUE)
gs <- ggplotGrob(s)
gf<- ggplotGrob(f)
grid::grid.draw(rbind(gf, gs))
dev.off()
}
And this is an example of what I get (two graphs out of 20
), any idea why I get hair line spaces between bars on the second graph? Why is the pattern so regular? Thanks for your help!
Your issue relates to some combination of screen and plot resolution. Your plots actually have thin lines between every bar, but finite screen and/or plot resolution results in some of those spaces appearing and some not.
Here are two plots created with your sample data. I've used grid.arrange to lay them out and save them to an object. I removed the theme statement because I got an error when I tried to run it, but that shouldn't matter for the issue you're having (note, that binwidth=0 has no effect, but I've left in the code below):
pl = gridExtra::grid.arrange(
ggplot(prod.data.list[[1]], aes(x=date.time, y= production, fill = Class),binwidth=0) +
geom_bar(stat = 'identity')+
scale_fill_manual(values=c("#A8A8A8","#CCFF99", "#FFA31A", "#CC2900")) +
scale_x_datetime() + theme_bw() ,
ggplot(prod.data.list[[2]], aes(x=date.time, y= production, fill = Class),binwidth=0) +
geom_bar(stat = 'identity')+
scale_fill_manual(values=c("#A8A8A8","#CCFF99", "#FFA31A", "#CC2900")) +
scale_x_datetime() + theme_bw()
)
Okay, now let's save it to a PDF:
pdf("test.pdf", 8,2)
plot(pl)
dev.off()
Note that there's a space between every bar:
Now look at the same plot saved as a png with two resolutions:
png("test1.png", 1000,200)
plot(pl)
dev.off()
png("test2.png", 3000,600)
plot(pl)
dev.off()
As you can see, the first plot, at lower resolution shows some of the spaces between bars while in other cases the spaces are gone. The higher resolution plot shows all the spaces.
To get rid of the spaces, use width=3600 in geom_bar(because your bins are 1 hour = 3600 seconds wide, and POSIXct format is in seconds).
pl2 = gridExtra::grid.arrange(
ggplot(prod.data.list[[1]], aes(x=date.time, y= production, fill = Class)) +
geom_bar(stat = 'identity', width=3600)+
scale_fill_manual(values=c("#A8A8A8","#CCFF99", "#FFA31A", "#CC2900")) +
scale_x_datetime() + theme_bw() ,
ggplot(prod.data.list[[2]], aes(x=date.time, y= production, fill = Class)) +
geom_bar(stat = 'identity')+
scale_fill_manual(values=c("#A8A8A8","#CCFF99", "#FFA31A", "#CC2900")) +
scale_x_datetime() + theme_bw()
)
png("test3.png", 1000, 200)
plot(pl2)
dev.off()
png("test4.png", 3000, 600)
plot(pl2)
dev.off()
As you can see in each of the plots below, the spaces between bars are now gone in the upper panel, due to the change in width.
Note that binwidth no longer works with geom_bar. From the docs:
geom_bar no longer has a binwidth argument - if you use it you'll get
an warning telling to you use geom_histogram instead.
You managed to avoid the warning by using binwidth in ggplot instead of geom_bar, which made it hard to realize that it wasn't actually doing anything.
Use width in geom_bar to change the width of the bars. When working with factors, you would use width = 1 (as noted in the comments on your question) to get bars with no spaces between them. Things are a bit different when working with scale_*_datetime.
Per this answer, it turns out the datetime scale is in seconds, so width = 1 amounts to a bar width of 1 second. To get a bar width for 1 hour as in your data, you would need a width of 3600.
Using this gets rid of the spaces you see in your first example dataset.
ggplot(prod.data1, aes(x=date.time, y= production, fill = Class)) +
geom_bar(stat = 'identity', width = 3600)+
scale_fill_manual(values=c("#A8A8A8","#CCFF99", "#FFA31A", "#CC2900")) +
scale_x_datetime()

Bar graph in ggplot2 with width as a variable and even spacing between bars

So I am trying to make a stacked bar graph with bar width mapped to a variable; but I want the spacing between my bars to be constant.
Does anyone know how to make the spacing constant between the bars?
Right now I've got this:
p<-ggplot(dd, aes(variable, value.y, fill=Date, width=value.x / 15))+ coord_flip() + opts(ylab="")
p1<-p+ geom_bar(stat="identity") + scale_fill_brewer(palette="Dark2") + scale_fill_hue(l=55,c=55)
p2<-p1 + opts(axis.title.x = theme_blank(), axis.title.y = theme_blank())
p2
Thanks in advance.
Here's my data by the way (sorry for the long, bulky dput):
> dput(dd)
structure(list(variable = structure(c(1L, 1L, 1L, 1L, 1L, 3L,
3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 7L, 7L, 7L, 7L, 7L, 2L, 2L,
2L, 2L, 2L, 6L, 6L, 6L, 6L, 6L, 5L, 5L, 5L, 5L, 5L, 9L, 9L, 9L,
9L, 9L, 8L, 8L, 8L, 8L, 8L), .Label = c("Alcohol and Tobacco",
"Health and Personal Care", "Clothing", "Energy", "Recreation and Education",
"Household", "Food", "Transportation", "Shelter"), class = "factor", scores = structure(c(2.91,
5.31, 10.08, 15.99, 4.95, 11.55, 11.2, 27.49, 20.6), .Dim = 9L, .Dimnames = list(
c("Alcohol and Tobacco", "Clothing", "Energy", "Food", "Health and Personal Care",
"Household", "Recreation and Education", "Shelter", "Transportation"
)))), value.x = c(2.91, 2.91, 2.91, 2.91, 2.91, 5.31, 5.31,
5.31, 5.31, 5.31, 10.08, 10.08, 10.08, 10.08, 10.08, 15.99, 15.99,
15.99, 15.99, 15.99, 4.95, 4.95, 4.95, 4.95, 4.95, 11.55, 11.55,
11.55, 11.55, 11.55, 11.2, 11.2, 11.2, 11.2, 11.2, 27.49, 27.49,
27.49, 27.49, 27.49, 20.6, 20.6, 20.6, 20.6, 20.6), Date = structure(c(5L,
4L, 3L, 2L, 1L, 5L, 4L, 3L, 2L, 1L, 5L, 4L, 3L, 2L, 1L, 5L, 4L,
3L, 2L, 1L, 5L, 4L, 3L, 2L, 1L, 5L, 4L, 3L, 2L, 1L, 5L, 4L, 3L,
2L, 1L, 5L, 4L, 3L, 2L, 1L, 5L, 4L, 3L, 2L, 1L), .Label = c("1993-2001",
"2001-2006", "2007-2010", "2010-2011", "2012 Jan - May"), class = "factor"),
value.y = c(2.1, 2.5, 7.6, 21.7, 2.8, 1.5, 0.3, -4.1, -4.2,
4.7, 3, 16.9, 1.9, 32.8, 23.9, 3.2, 4.6, 11.3, 8.9, 12.9,
1.7, 2, 7.8, 5.9, 10, 1.9, 2.1, 5.6, 2.2, 9.9, 1.4, 1.3,
2.2, 0.6, 17.3, 1.1, 2.3, 6.4, 13.1, 10, 4.3, 7.6, 0.9, 15.2,
20.5)), .Names = c("variable", "value.x", "Date", "value.y"
), row.names = c(NA, -45L), class = "data.frame")
For a categorical or "discrete" scale - you can adjust the width, but it needs to be between 0 and 1. Your value.x's put it over 1, hence the overlap. You can use rescale, from the scales packages to adjust this quickly so that the within category width of the bar is representative of some other variable (in this case value.x)
install.packages("scales")
library(scales)
ggplot(dd,aes(x=variable,y=value.y,fill=Date)) +
geom_bar(aes(width=rescale(value.x,c(0.5,1))),stat="identity",position="stack")' +
coord_flip()
Play with rescaling for optimal "view" change 0.5 to 0.25... etc.
Personally, I think something like this is more informative:
ggplot(dd,aes(x=variable,y=value.y,fill=Date)) +
geom_bar(aes(width=rescale(value.x,c(0.2,1))),stat="identity") +
coord_flip() + facet_grid(~Date) + opts(legend.position="none")
Attempt # 2.
I'm tricking ggplot2 into writing a continuous scale as categorical.
# The numbers for tmp I calculated by hand. Not sure how to program
# this part but the math is
# last + half(previous_width) + half(current_width)
# Change the 1st number in cumsum to adjust the between category width
tmp <- c(2.91,7.02,14.715,27.75,38.22,46.47,57.845,77.19,101.235) + cumsum(rep(5,9))
dd$x.pos1 <- rep(tmp,each=5)
ggplot(dd,aes(x=x.pos1,y=value.y,fill=Date)) +
geom_bar(aes(width=value.x),stat="identity",position="stack") +
scale_x_continuous(breaks=tmp,labels=levels(dd$variable)) +
coord_flip()
For good measure you're probably going to want to adjust the text size. That's done with ... + opts(axis.text.y=theme_text(size=12))

Resources