Issue with fitted line on ggplot - r

I am trying to plot the model predictions using ggplot2 in r of the quasipoisson glm model below. Some sample data is given below:
Rabbit_ACT = structure(list(Occurrence_ID = c(628437L, 668157L, 658431L, 623576L,
683597L, 629915L, 682595L, 683739L, 683502L, 645019L, 627926L,
630368L, 630595L, 629778L, 628486L, 678084L, 25909L, 683462L,
630460L, 658385L, 167L, 733L, 627921L, 628208L, 627820L, 629402L,
654144L, 679478L, 660806L, 628352L, 684863L, 629369L, 628186L,
679270L, 679285L, 654213L, 627689L, 669989L, 629540L, 629117L,
630666L, 629645L, 653212L, 629375L, 686819L, 662562L, 644488L,
683672L, 648112L, 337L, 688L, 630133L, 629846L, 683339L, 679402L,
670064L, 628215L, 628952L, 763L, 648919L, 628507L, 679518L, 650769L,
648981L, 648261L, 684892L, 630539L, 650628L, 628598L, 683627L,
628098L, 679430L, 679298L, 628815L, 668723L, 630227L, 683776L,
222L, 629792L, 668887L, 669247L, 688104L, 660411L, 630734L, 627837L,
682613L, 484L, 628584L, 659998L, 628792L, 629186L, 668881L, 630721L,
630346L, 661689L, 630637L, 627809L, 684913L, 688100L, 689276L
), Lat = c(-32.25, -37.65, -38.35, -25.75, -37.45, -32.25, -40.95,
-37.25, -38.25, -33.95, -32.25, -32.25, -32.25, -32.25, -32.25,
-32.95, -37.65, -37.35, -32.25, -37.45, -31.95, -22.64, -32.25,
-32.25, -32.25, -32.25, -38.05, -34.55, -37.45, -32.25, -36.25,
-32.25, -32.25, -37.45, -37.35, -37.25, -32.25, -24.55, -32.25,
-32.25, -32.25, -32.25, -36.65, -32.25, -37.65, -34.55, -34.85,
-37.25, -36.65, -31.95, -27.45, -32.25, -32.25, -38.45, -36.85,
-24.75, -32.25, -32.25, -20.75, -37.75, -32.25, -35.65, -35.35,
-36.85, -31.55, -36.75, -32.25, -38.25, -32.25, -37.95, -32.25,
-35.85, -37.45, -32.25, -42.25, -32.25, -37.85, -31.95, -32.25,
-38.25, -35.15, -36.65, -29.65, -32.25, -32.25, -33.65, -33.55,
-32.25, -37.55, -32.25, -32.25, -38.05, -32.25, -32.25, -34.45,
-32.25, -32.25, -36.35, -36.55, -32.55), Long = c(138.75, 145.15,
146.15, 118.85, 146.85, 138.75, 148.05, 149.35, 147.25, 147.95,
138.75, 138.75, 138.75, 138.75, 138.75, 146.15, 144.35, 146.95,
138.75, 141.15, 116.35, 113.68, 138.75, 138.75, 138.75, 138.75,
141.15, 147.45, 149.95, 138.75, 147.65, 138.75, 138.75, 149.95,
149.95, 143.25, 138.75, 134.25, 138.75, 138.75, 138.75, 138.75,
146.45, 138.75, 145.25, 147.45, 149.05, 147.95, 149.45, 116.35,
114.55, 138.75, 138.75, 145.55, 149.05, 133.65, 138.75, 138.75,
118.85, 144.25, 138.75, 148.65, 138.75, 141.65, 129.15, 146.45,
138.75, 145.75, 138.75, 147.25, 138.75, 147.55, 149.95, 138.75,
146.95, 138.75, 146.05, 116.35, 138.75, 147.25, 141.35, 141.65,
152.95, 138.75, 138.75, 115.95, 115.05, 138.75, 148.25, 138.75,
138.75, 147.65, 138.75, 138.75, 147.45, 138.75, 138.75, 146.25,
141.55, 149.55), Occurences = c(5121L, 111L, 5L, 1L, 5L, 5121L,
7L, 4L, 15L, 15L, 5121L, 5121L, 5121L, 5121L, 5121L, 8L, 17410L,
8L, 5121L, 12L, 259L, 2L, 5121L, 5121L, 5121L, 5121L, 1L, 46L,
66L, 5121L, 6L, 5121L, 5121L, 66L, 22L, 11L, 5121L, 16L, 5121L,
5121L, 5121L, 5121L, 3L, 5121L, 95L, 46L, 12L, 3L, 109L, 259L,
1L, 5121L, 5121L, 14L, 4L, 21L, 5121L, 5121L, 1L, 849L, 5121L,
8L, 8L, 5L, 7L, 2L, 5121L, 4L, 5121L, 15L, 5121L, 6L, 66L, 5121L,
1L, 5121L, 7L, 259L, 5121L, 15L, 3209L, 6L, 8L, 5121L, 5121L,
24L, 95L, 5121L, 8L, 5121L, 5121L, 6L, 5121L, 5121L, 8L, 5121L,
5121L, 4L, 6L, 5L), Abund.1 = c(5121L, 111L, 5L, 1L, 5L, 5121L,
7L, 4L, 15L, 15L, 5121L, 5121L, 5121L, 5121L, 5121L, 8L, 17410L,
8L, 5121L, 12L, 259L, 2L, 5121L, 5121L, 5121L, 5121L, 1L, 46L,
66L, 5121L, 6L, 5121L, 5121L, 66L, 22L, 11L, 5121L, 16L, 5121L,
5121L, 5121L, 5121L, 3L, 5121L, 95L, 46L, 12L, 3L, 109L, 259L,
1L, 5121L, 5121L, 14L, 4L, 21L, 5121L, 5121L, 1L, 849L, 5121L,
8L, 8L, 5L, 7L, 2L, 5121L, 4L, 5121L, 15L, 5121L, 6L, 66L, 5121L,
1L, 5121L, 7L, 259L, 5121L, 15L, 3209L, 6L, 8L, 5121L, 5121L,
24L, 95L, 5121L, 8L, 5121L, 5121L, 6L, 5121L, 5121L, 8L, 5121L,
5121L, 4L, 6L, 5L), Abund.2 = c(NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_),
Abund.3 = c(256.05, 0.001727626, 7.78e-05, 0.000769231, 7.78e-05,
256.05, 0.000108949, 6.23e-05, 0.000233463, 0.000233463,
256.05, 256.05, 256.05, 256.05, 256.05, 0.000124514, 1741,
0.000124514, 256.05, 0.00018677, 25.9, 0.000961538, 256.05,
256.05, 256.05, 256.05, 1.56e-05, 0.000715953, 0.001027237,
256.05, 9.34e-05, 256.05, 256.05, 0.001027237, 0.000342412,
0.000171206, 256.05, 0.000249027, 256.05, 256.05, 256.05,
256.05, 4.67e-05, 256.05, 0.001478599, 0.000715953, 0.00018677,
4.67e-05, 0.001696498, 25.9, 0.000480769, 256.05, 256.05,
0.000217899, 6.23e-05, 0.000326848, 256.05, 256.05, 0.000480769,
0.013214008, 256.05, 0.000124514, 0.000124514, 7.78e-05,
0.000108949, 3.11e-05, 256.05, 6.23e-05, 256.05, 0.000233463,
256.05, 9.34e-05, 0.001027237, 256.05, 1.56e-05, 256.05,
0.000108949, 25.9, 256.05, 0.000233463, 0.049945525, 9.34e-05,
0.000124514, 256.05, 256.05, 0.000373541, 9.5, 256.05, 0.000124514,
256.05, 256.05, 9.34e-05, 256.05, 256.05, 0.000124514, 256.05,
256.05, 6.23e-05, 9.34e-05, 0.005681818), No.of.10km.cells = c(2L,
6425L, 6425L, 130L, 6425L, 2L, 6425L, 6425L, 6425L, 6425L,
2L, 2L, 2L, 2L, 2L, 6425L, 1L, 6425L, 2L, 6425L, 1L, 208L,
2L, 2L, 2L, 2L, 6425L, 6425L, 6425L, 2L, 6425L, 2L, 2L, 6425L,
6425L, 6425L, 2L, 6425L, 2L, 2L, 2L, 2L, 6425L, 2L, 6425L,
6425L, 6425L, 6425L, 6425L, 1L, 208L, 2L, 2L, 6425L, 6425L,
6425L, 2L, 2L, 208L, 6425L, 2L, 6425L, 6425L, 6425L, 6425L,
6425L, 2L, 6425L, 2L, 6425L, 2L, 6425L, 6425L, 2L, 6425L,
2L, 6425L, 1L, 2L, 6425L, 6425L, 6425L, 6425L, 2L, 2L, 6425L,
1L, 2L, 6425L, 2L, 2L, 6425L, 2L, 2L, 6425L, 2L, 2L, 6425L,
6425L, 88L), Year = c(1972L, 1980L, 1967L, 1981L, 1978L,
1979L, 1965L, 1977L, 1978L, 1978L, 1969L, 1981L, 1982L, 1978L,
1972L, 1981L, 1982L, 1978L, 1981L, 1979L, 1974L, 1981L, 1969L,
1970L, 1969L, 1976L, 1980L, 1981L, 1971L, 1971L, 1982L, 1976L,
1970L, 1980L, 1980L, 1975L, 1968L, 1980L, 1977L, 1975L, 1982L,
1977L, 1981L, 1976L, 1979L, 1982L, 1982L, 1977L, 1900L, 1974L,
1981L, 1980L, 1978L, 1981L, 1981L, 1980L, 1970L, 1974L, 1981L,
1969L, 1972L, 1964L, 1968L, 1980L, 1950L, 1981L, 1981L, 1981L,
1972L, 1978L, 1970L, 1979L, 1980L, 1973L, 1979L, 1980L, 1967L,
1974L, 1978L, 1978L, 1980L, 1978L, 1978L, 1982L, 1969L, 1966L,
1975L, 1972L, 1979L, 1973L, 1975L, 1978L, 1982L, 1980L, 1975L,
1982L, 1969L, 1978L, 1978L, 1883L), Day = c(19L, NA, NA,
NA, NA, 1L, NA, NA, NA, NA, 9L, 4L, 24L, 21L, 29L, NA, 15L,
NA, 22L, NA, 9L, NA, 9L, 15L, 10L, 8L, NA, NA, NA, 15L, NA,
9L, 3L, NA, NA, NA, 12L, NA, 6L, 26L, 1L, 16L, NA, 28L, NA,
NA, NA, NA, NA, 1L, NA, 24L, 13L, NA, NA, NA, 15L, 16L, NA,
NA, 3L, NA, NA, NA, NA, NA, 26L, NA, 12L, NA, 17L, NA, NA,
31L, NA, 10L, NA, 13L, 21L, NA, NA, NA, NA, 10L, 29L, NA,
28L, 12L, NA, 12L, 31L, NA, 23L, 18L, NA, 20L, 10L, NA, NA,
NA), A_Prec_Avg30Yr = c(299.76, 708.74, NA, 246.95, 1176.45,
299.76, NA, 875.46, 579.65, 580.43, NA, 299.76, 299.76, 299.76,
299.76, 382.24, 575.45, 832.4, 299.76, 623.53, 732.58, 243.46,
NA, 299.76, NA, 299.76, 784.35, 542.49, 909.03, 299.76, 1003.98,
299.76, 299.76, 909.03, 902.49, 803.54, NA, 243.56, 299.76,
299.76, 299.76, 299.76, 958.2, 299.76, 769.46, 542.49, 668.97,
793.91, NA, 732.58, 308.18, 299.76, 299.76, 959.99, 511.97,
241.04, 299.76, 299.76, 354.01, NA, 299.76, NA, NA, 502.69,
NA, 1099.01, 299.76, 961.28, 299.76, 643.55, 299.76, 964.26,
909.03, 299.76, 602.15, 299.76, NA, 732.58, 299.76, 579.65,
299.88, 449.9, 1025.68, 299.76, NA, NA, 854.37, 299.76, 849.48,
299.76, 299.76, 594.14, 299.76, 299.76, 529.02, 299.76, NA,
618.71, 440.67, NA), A_Psea_Avg30Yr = c(21.19, 16.34, NA,
54.98, 27.29, 21.19, NA, 15.32, 12.77, 14.38, NA, 21.19,
21.19, 21.19, 21.19, 16.01, 20.69, 21.15, 21.19, 44.64, 79.51,
74.18, NA, 21.19, NA, 21.19, 44.32, 16.54, 13.41, 21.19,
34.18, 21.19, 21.19, 13.41, 13.36, 32.69, NA, 48.96, 21.19,
21.19, 21.19, 21.19, 34.46, 21.19, 18.09, 16.54, 15.76, 16.55,
NA, 79.51, 74.64, 21.19, 21.19, 25.09, 24.33, 47.77, 21.19,
21.19, 116.17, NA, 21.19, NA, NA, 41.58, NA, 36.53, 21.19,
22.21, 21.19, 16.23, 21.19, 31.83, 13.41, 21.19, 16.98, 21.19,
NA, 79.51, 21.19, 12.77, 29.83, 39.68, 46.99, 21.19, NA,
NA, 85.12, 21.19, 16.45, 21.19, 21.19, 14.12, 21.19, 21.19,
15.36, 21.19, NA, 26.3, 39.78, NA), A_TAvg_Avg30Yr = c(15.86,
14.33, NA, 22.5, 8.94, 15.86, NA, 13.06, 14.17, 15.84, NA,
15.86, 15.86, 15.86, 15.86, 17.91, 13.37, 11.39, 15.86, 14,
17.45, 24.34, NA, 15.86, NA, 15.86, 13.67, 15.45, 14.9, 15.86,
12.63, 15.86, 15.86, 14.9, 14.43, 11.96, NA, 21.66, 15.86,
15.86, 15.86, 15.86, 14.1, 15.86, 14.11, 15.45, 13.52, 10.54,
NA, 17.45, 21.1, 15.86, 15.86, 13.62, 11.55, 21.7, 15.86,
15.86, 27.13, NA, 15.86, NA, NA, 14.15, NA, 13.42, 15.86,
13.77, 15.86, 14.38, 15.86, 12.94, 14.9, 15.86, 9.31, 15.86,
NA, 17.45, 15.86, 14.17, 16.34, 14.37, 19.26, 15.86, NA,
NA, 16.52, 15.86, 14.32, 15.86, 15.86, 14.65, 15.86, 15.86,
15.7, 15.86, NA, 15.01, 14.45, NA), A_TMax_Avg30Yr = c(31.24,
27.18, NA, 38.82, 20.86, 31.24, NA, 24.84, 23.85, 31.86,
NA, 31.24, 31.24, 31.24, 31.24, 33.59, 26.82, 24.4, 31.24,
27.36, 32.04, 35.4, NA, 31.24, NA, 31.24, 23.95, 31.32, 23.56,
31.24, 27.9, 31.24, 31.24, 23.56, 22.98, 25.55, NA, 37.66,
31.24, 31.24, 31.24, 31.24, 29.76, 31.24, 27.02, 31.32, 28.48,
22.81, NA, 32.04, 36.93, 31.24, 31.24, 24.84, 25.04, 37.64,
31.24, 31.24, 39.96, NA, 31.24, NA, NA, 28.87, NA, 28.69,
31.24, 25.6, 31.24, 26.1, 31.24, 28.24, 23.56, 31.24, 21.03,
31.24, NA, 32.04, 31.24, 23.85, 31.84, 29.34, 29.89, 31.24,
NA, NA, 27.39, 31.24, 26.09, 31.24, 31.24, 23.6, 31.24, 31.24,
31.59, 31.24, NA, 30.88, 29.41, NA), A_TMin_Avg30Yr = c(2.78,
4.59, NA, 5.97, -0.13, 2.78, NA, 2.26, 4.92, 2.52, NA, 2.78,
2.78, 2.78, 2.78, 3.92, 3.06, 1.29, 2.78, 4.71, 6.65, 12.96,
NA, 2.78, NA, 2.78, 5.31, 2.15, 6.09, 2.78, 1.06, 2.78, 2.78,
6.09, 5.65, 2.81, NA, 3.81, 2.78, 2.78, 2.78, 2.78, 2.5,
2.78, 4.48, 2.15, 0.74, 0.82, NA, 6.65, 7.76, 2.78, 2.78,
4.88, -0.65, 3.92, 2.78, 2.78, 11.98, NA, 2.78, NA, NA, 3.92,
NA, 2.24, 2.78, 4.44, 2.78, 3.67, 2.78, 1.25, 6.09, 2.78,
0.04, 2.78, NA, 6.65, 2.78, 4.92, 4.06, 3.76, 5.94, 2.78,
NA, NA, 8.76, 2.78, 4.15, 2.78, 2.78, 5.85, 2.78, 2.78, 2.2,
2.78, NA, 2.69, 3.8, NA), A_TSea_Avg30Yr = c(563.7, 405.82,
NA, 663.22, 438.27, 563.7, NA, 416.92, 330.39, 588.51, NA,
563.7, 563.7, 563.7, 563.7, 612.28, 416.04, 438.13, 563.7,
376.52, 488.8, 417.94, NA, 563.7, NA, 563.7, 303.9, 596.8,
320.72, 563.7, 537.25, 563.7, 563.7, 320.72, 321.8, 441.32,
NA, 651.46, 563.7, 563.7, 563.7, 563.7, 515.65, 563.7, 404.93,
596.8, 543.86, 422.81, NA, 488.8, 529.02, 563.7, 563.7, 345.9,
468.25, 650.8, 563.7, 563.7, 479.28, NA, 563.7, NA, NA, 438.45,
NA, 505.71, 563.7, 363, 563.7, 380.24, 563.7, 550.8, 320.72,
563.7, 358.72, 563.7, NA, 488.8, 563.7, 330.39, 501.96, 449.81,
412.56, 563.7, NA, NA, 320.65, 563.7, 380.63, 563.7, 563.7,
310.08, 563.7, 563.7, 598.64, 563.7, NA, 544.19, 449.76,
NA), A_TWet_Avg30Yr = c(12.47, 13.7, NA, 27.06, 8.19, 12.47,
NA, 12.71, 13.56, 9.6, NA, 12.47, 12.47, 12.47, 12.47, 11.27,
12.65, 10.17, 12.47, 9.54, 11.75, 20.15, NA, 12.47, NA, 12.47,
9.98, 9.15, 15.87, 12.47, 6.08, 12.47, 12.47, 15.87, 15.37,
6.7, NA, 28.21, 12.47, 12.47, 12.47, 12.47, 7.95, 12.47,
13.25, 9.15, 9.98, 10.06, NA, 11.75, 15.99, 12.47, 12.47,
9.42, 15.96, 28.25, 12.47, 12.47, 31.83, NA, 12.47, NA, NA,
8.96, NA, 7.37, 12.47, 11.34, 12.47, 13.91, 12.47, 6.26,
15.87, 12.47, 7.09, 12.47, NA, 11.75, 12.47, 13.56, 10.2,
9.03, 23.67, 12.47, NA, NA, 12.95, 12.47, 13.91, 12.47, 12.47,
15.69, 12.47, 12.47, 9.37, 12.47, NA, 9.05, 9.1, NA), A_TWrm_Avg30Yr = c(22.87,
19.34, NA, 30.4, 14.29, 22.87, NA, 18.1, 18.23, 23.22, NA,
22.87, 22.87, 22.87, 22.87, 25.47, 18.55, 16.76, 22.87, 18.72,
23.51, 29.35, NA, 22.87, NA, 22.87, 17.47, 22.94, 18.85,
22.87, 19.32, 22.87, 22.87, 18.85, 18.38, 17.47, NA, 29.22,
22.87, 22.87, 22.87, 22.87, 20.61, 22.87, 19.11, 22.94, 20.3,
15.7, NA, 23.51, 27.6, 22.87, 22.87, 17.96, 17.28, 29.25,
22.87, 22.87, 32.23, NA, 22.87, NA, NA, 19.7, NA, 19.78,
22.87, 18.27, 22.87, 18.98, 22.87, 19.84, 18.85, 22.87, 13.74,
22.87, NA, 23.51, 22.87, 18.23, 22.62, 20.06, 24, 22.87,
NA, NA, 20.66, 22.87, 18.93, 22.87, 22.87, 18.45, 22.87,
22.87, 23.21, 22.87, NA, 21.9, 20.13, NA), A_Prec_AvgAutumn30Yr = c(19.88,
57.41, NA, 26.9, 83.64, 19.88, NA, 69.05, 47.86, 39.89, NA,
19.88, 19.88, 19.88, 19.88, 29.31, 42.5, 56.51, 19.88, 45.19,
47.28, 29.61, NA, 19.88, NA, 19.88, 59.33, 39.38, 79.74,
19.88, 67.46, 19.88, 19.88, 79.74, 78.05, 57.85, NA, 20.29,
19.88, 19.88, 19.88, 19.88, 66.85, 19.88, 60.98, 39.38, 48.04,
60.31, NA, 47.28, 27.14, 19.88, 19.88, 75.63, 39.77, 19.61,
19.88, 19.88, 33.34, NA, 19.88, NA, NA, 35.13, NA, 73.9,
19.88, 73.36, 19.88, 54.09, 19.88, 67.42, 79.74, 19.88, 45.96,
19.88, NA, 47.28, 19.88, 47.86, 21.98, 31.63, 101.12, 19.88,
NA, NA, 59.49, 19.88, 63.58, 19.88, 19.88, 49.92, 19.88,
19.88, 39.45, 19.88, NA, 47.15, 31.11, NA), A_Prec_AvgSummer30Yr = c(25.99,
55.28, NA, 34.05, 77.66, 25.99, NA, 68.99, 47.85, 49.57,
NA, 25.99, 25.99, 25.99, 25.99, 35.61, 49.96, 58.93, 25.99,
29.85, 13.97, 19.69, NA, 25.99, NA, 25.99, 35.63, 44.06,
71.62, 25.99, 61.43, 25.99, 25.99, 71.62, 74.09, 47.48, NA,
32.21, 25.99, 25.99, 25.99, 25.99, 58.55, 25.99, 57.47, 44.06,
56.79, 66.65, NA, 13.97, 12.32, 25.99, 25.99, 54.2, 51.59,
30.96, 25.99, 25.99, 72.09, NA, 25.99, NA, NA, 26.11, NA,
63.04, 25.99, 62.38, 25.99, 54.9, 25.99, 61.34, 71.62, 25.99,
45.53, 25.99, NA, 13.97, 25.99, 47.85, 20.77, 24.7, 131.36,
25.99, NA, NA, 11.64, 25.99, 68.81, 25.99, 25.99, 49.93,
25.99, 25.99, 43.77, 25.99, NA, 45.04, 24.02, NA), A_Prec_AvgSpring30Yr = c(28.46,
70.69, NA, 7.71, 128.29, 28.46, NA, 79.69, 55.23, 54.13,
NA, 28.46, 28.46, 28.46, 28.46, 33.6, 65.37, 83.85, 28.46,
58.34, 51.89, 3.39, NA, 28.46, NA, 28.46, 68.15, 49.5, 74.25,
28.46, 91.9, 28.46, 28.46, 74.25, 74.26, 74.53, NA, 17.47,
28.46, 28.46, 28.46, 28.46, 81.92, 28.46, 76.34, 49.5, 66.25,
83.35, NA, 51.89, 12.17, 28.46, 28.46, 82.69, 49.07, 18.62,
28.46, 28.46, 1.21, NA, 28.46, NA, NA, 47.15, NA, 91.76,
28.46, 93.91, 28.46, 62.61, 28.46, 83.59, 74.25, 28.46, 58.5,
28.46, NA, 51.89, 28.46, 55.23, 30.91, 43.08, 69.22, 28.46,
NA, NA, 55.02, 28.46, 80.65, 28.46, 28.46, 55.46, 28.46,
28.46, 48.66, 28.46, NA, 57.2, 42.15, NA), A_Prec_AvgWinter30Yr = c(26.81,
59.12, NA, 18.1, 103.8, 26.81, NA, 73.29, 45.33, 49.58, NA,
26.81, 26.81, 26.81, 26.81, 30.62, 48.06, 75.28, 26.81, 83.67,
132.13, 32.53, NA, 26.81, NA, 26.81, 107.33, 47.63, 73.48,
26.81, 113.92, 26.81, 26.81, 73.48, 69.56, 89.1, NA, 10.36,
26.81, 26.81, 26.81, 26.81, 106.49, 26.81, 65.63, 47.63,
61.61, 65.62, NA, 132.13, 51.46, 26.81, 26.81, 93.42, 32.25,
11.31, 26.81, 26.81, 12.17, NA, 26.81, NA, NA, 60.19, NA,
121.9, 26.81, 87.78, 26.81, 49.24, 26.81, 105.6, 73.48, 26.81,
55.64, 26.81, NA, 132.13, 26.81, 45.33, 31.48, 52.89, 42.98,
26.81, NA, NA, 148.58, 26.81, 72.5, 26.81, 26.81, 47.51,
26.81, 26.81, 45.99, 26.81, NA, 66.13, 51.99, NA), DistPermWater = c(5.35,
2.93, NA, 12.82, 8.21, 5.35, NA, 2.09, 2.48, 2.08, NA, 5.35,
5.35, 5.35, 5.35, 19.6, 0.41, 2.54, 5.35, 1.04, 7.11, 3.75,
NA, 5.35, NA, 5.35, 1.81, 1.37, 2.36, 5.35, 0.71, 5.35, 5.35,
2.36, 3.15, 0.92, NA, 5.81, 5.35, 5.35, 5.35, 5.35, 0.51,
5.35, 3.27, 1.37, 0.31, 3.59, NA, 7.11, 5.21, 5.35, 5.35,
9.53, 2.99, 5.66, 5.35, 5.35, 15.74, NA, 5.35, NA, NA, 0.52,
NA, 0.61, 5.35, 2.56, 5.35, 2.87, 5.35, 2.34, 2.36, 5.35,
2.41, 5.35, NA, 7.11, 5.35, 2.48, 5.9, 0.48, 0.89, 5.35,
NA, NA, 3.91, 5.35, 3.14, 5.35, 5.35, 0.68, 5.35, 5.35, 0.88,
5.35, NA, 0.32, 1.66, NA), DistAgriLand = c(35.73, 4.7, NA,
362.03, 28.52, 35.73, NA, 6.52, 1.53, 1.74, NA, 35.73, 35.73,
35.73, 35.73, 5.49, 1.33, 42.58, 35.73, 5.82, 6.66, 531.66,
NA, 35.73, NA, 35.73, 7.14, 0.66, 22.83, 35.73, 12.29, 35.73,
35.73, 22.83, 11.03, 8.58, NA, 649.51, 35.73, 35.73, 35.73,
35.73, 3.05, 35.73, 6.96, 0.66, 10.14, 19.12, NA, 6.66, 27.47,
35.73, 35.73, 2.14, 9.5, 687.88, 35.73, 35.73, 861.68, NA,
35.73, NA, NA, 1.92, NA, 6.16, 35.73, 3.27, 35.73, 4.57,
35.73, 3.75, 22.83, 35.73, 4.24, 35.73, NA, 6.66, 35.73,
1.53, 1.57, 1, 3.46, 35.73, NA, NA, 3.2, 35.73, 5.49, 35.73,
35.73, 6.66, 35.73, 35.73, 0.72, 35.73, NA, 3.02, 5.6, NA
), PercSoilClay = c(27.6, 23.22, NA, 29.98, 25.71, 27.6,
NA, 20.54, 8.61, 19.46, NA, 27.6, 27.6, 27.6, 27.6, 29.74,
24.42, 25.06, 27.6, 44.6, 5.96, 7.09, NA, 27.6, NA, 27.6,
22.83, 18.75, 19.83, 27.6, 17.76, 27.6, 27.6, 19.83, 20.01,
15, NA, 10.96, 27.6, 27.6, 27.6, 27.6, 16.19, 27.6, 30.81,
18.75, 15, 20.35, NA, 5.96, 13.33, 27.6, 27.6, 16.08, 26.25,
10.93, 27.6, 27.6, 20, NA, 27.6, NA, NA, 12.46, NA, 20.23,
27.6, 28.55, 27.6, 15.21, 27.6, 19.29, 19.83, 27.6, 15.74,
27.6, NA, 5.96, 27.6, 8.61, 21.61, 29.17, 23.97, 27.6, NA,
NA, 14.86, 27.6, 19.05, 27.6, 27.6, 16.11, 27.6, 27.6, 17.67,
27.6, NA, 14.51, 11.71, NA), MinDayLength = c(10.03, 9.56,
NA, 10.53, 9.57, 10.03, NA, 9.59, 9.5, 9.89, NA, 10.03, 10.03,
10.03, 10.03, 9.97, 9.56, 9.58, 10.03, 9.57, 10.06, 10.74,
NA, 10.03, NA, 10.03, 9.52, 9.84, 9.57, 10.03, 9.69, 10.03,
10.03, 9.57, 9.58, 9.59, NA, 10.61, 10.03, 10.03, 10.03,
10.03, 9.65, 10.03, 9.56, 9.84, 9.81, 9.59, NA, 10.06, 10.4,
10.03, 10.03, 9.48, 9.63, 10.6, 10.03, 10.03, 10.87, NA,
10.03, NA, NA, 9.63, NA, 9.64, 10.03, 9.5, 10.03, 9.53, 10.03,
9.72, 9.57, 10.03, 9.08, 10.03, NA, 10.06, 10.03, 9.5, 9.78,
9.65, 10.24, 10.03, NA, NA, 9.92, 10.03, 9.56, 10.03, 10.03,
9.52, 10.03, 10.03, 9.85, 10.03, NA, 9.68, 9.66, NA), VarDayLength = c(2.14,
3.22, NA, 1.24, 3.18, 2.14, NA, 3.13, 3.37, 2.44, NA, 2.14,
2.14, 2.14, 2.14, 2.26, 3.22, 3.15, 2.14, 3.18, 2.09, 0.92,
NA, 2.14, NA, 2.14, 3.32, 2.55, 3.18, 2.14, 2.9, 2.14, 2.14,
3.18, 3.15, 3.13, NA, 1.11, 2.14, 2.14, 2.14, 2.14, 2.99,
2.14, 3.22, 2.55, 2.61, 3.13, NA, 2.09, 1.44, 2.14, 2.14,
3.42, 3.04, 1.13, 2.14, 2.14, 0.76, NA, 2.14, NA, NA, 3.04,
NA, 3.01, 2.14, 3.37, 2.14, 3.3, 2.14, 2.82, 3.18, 2.14,
4.52, 2.14, NA, 2.09, 2.14, 3.37, 2.67, 2.99, 1.73, 2.14,
NA, NA, 2.37, 2.14, 3.2, 2.14, 2.14, 3.31, 2.14, 2.14, 2.53,
2.14, NA, 2.93, 2.97, NA)), row.names = c(1665L, 6514L, 503L,
7208L, 4855L, 4989L, 411L, 4103L, 4761L, 4327L, 899L, 7332L,
8054L, 4203L, 1714L, 7775L, 7986L, 4721L, 7424L, 5392L, 2292L,
6992L, 894L, 1234L, 793L, 3365L, 6434L, 7836L, 1616L, 1497L,
8606L, 3332L, 1212L, 6651L, 6656L, 3188L, 590L, 6553L, 3669L,
3007L, 8125L, 3774L, 7634L, 3338L, 5797L, 8372L, 8238L, 4037L,
165L, 2462L, 6947L, 5999L, 4271L, 7861L, 7822L, 6618L, 1241L,
2640L, 7022L, 995L, 1735L, 382L, 690L, 6361L, 284L, 7896L, 7503L,
7581L, 1826L, 4885L, 1124L, 5671L, 6658L, 2140L, 5580L, 6093L,
516L, 2347L, 4217L, 4639L, 6527L, 4964L, 4518L, 8193L, 810L,
468L, 2893L, 1812L, 5430L, 2117L, 3076L, 4633L, 8180L, 6212L,
3200L, 8096L, 782L, 4896L, 4960L, 108L), class = "data.frame")
Here is the fully simplified model that I fitted to the data
final_ACT2 = glm(Occurences ~ A_Prec_Avg30Yr + DistAgriLand + DistPermWater + A_TAvg_Avg30Yr,
family = quasipoisson(link = "log"), data = Rabbit_ACT)
I also wanted to plot 95% confidence intervals around the fitted line and used a function from a different question given below:
pois_ci = function(pred, data){
fit = pred$fit
lower = fit - 1.96*pred$se.fit
upper = fit + 1.96*pred$se.fit
return(data.frame(fit = exp(fit), lwr = exp(lower), upr = exp(upper), data))
}
To plot the predictions I used the following code:
ACT_rain = data.frame(A_Prec_Avg30Yr =
seq(from = min(Rabbit_ACT$A_Prec_Avg30Yr, na.rm = TRUE),
to = max(Rabbit_ACT$A_Prec_Avg30Yr, na.rm = TRUE),
length.out = length(Rabbit_ACT$A_Prec_Avg30Yr)),
DistAgriLand = Rabbit_ACT$DistAgriLand,
DistPermWater = Rabbit_ACT$DistPermWater,
A_TAvg_Avg30Yr = Rabbit_ACT$A_TAvg_Avg30Yr,
Occurences = Rabbit_ACT$Occurences)
preds.ACT_rain = predict(final_ACT2, newdata = ACT_rain, se.fit = TRUE, type = "link")
preds.ACT_rain.ci = pois_ci(preds.ACT_rain, ACT_rain)
ACT.a = ggplot(data = na.omit(preds.ACT_rain.ci)) +
geom_point(aes(x = A_Prec_Avg30Yr, y = Occurences), alpha = 0.3) +
geom_line(aes(x = A_Prec_Avg30Yr, fit)) +
geom_ribbon(aes(ymin = lwr, ymax = upr, x = A_Prec_Avg30Yr, y = Occurences)) +
labs(x = "Mean Average 30yr AVerage Percipitation (mL)", "No.of Rabbit Occurences") +
ylim(0, 5000) +
theme_classic()
ACT.a
But the output I got was a graph where the fitted line was zig-zagging between data points when it should be a smooth line but I don't know what I have done that is causing this. I know there are NA values in the data set but unsure if this is causing the issue. I have tried looking at other questions but I was struggling to understand what was going on and would be grateful if anyone has a solution. Thanks in advance.
Heres the plot that was produced (not it was done using the full dataset)

There are two things causing this.
You're adding the observed values of Occurences to your prediction dataset, then using those observed values as your y aesthetic in geom_ribbon(). You should instead set y = fit to use the predicted values.
You're generating predictions using the observed values of your covariates, which will introduce noise. Instead, set each covariate at its mean in your prediction set.
Also - are you trying to plot the observed values using geom_point()? If so, set data = Rabbit_ACT to use the original data there for that geom. Otherwise, to plot the predicted values, change the mapping to y = fit there as well.
library(ggplot2)
final_ACT2 = glm(Occurences ~ A_Prec_Avg30Yr + DistAgriLand + DistPermWater + A_TAvg_Avg30Yr,
family = quasipoisson(link = "log"), data = Rabbit_ACT)
pois_ci = function(pred, data){
fit = pred$fit
lower = fit - 1.96*pred$se.fit
upper = fit + 1.96*pred$se.fit
return(data.frame(fit = exp(fit), lwr = exp(lower), upr = exp(upper), data))
}
ACT_rain = data.frame(A_Prec_Avg30Yr =
seq(from = min(Rabbit_ACT$A_Prec_Avg30Yr, na.rm = TRUE),
to = max(Rabbit_ACT$A_Prec_Avg30Yr, na.rm = TRUE),
length.out = length(Rabbit_ACT$A_Prec_Avg30Yr)),
# set covariates to mean
DistAgriLand = mean(Rabbit_ACT$DistAgriLand, na.rm = TRUE),
DistPermWater = mean(Rabbit_ACT$DistPermWater, na.rm = TRUE),
A_TAvg_Avg30Yr = mean(Rabbit_ACT$A_TAvg_Avg30Yr, na.rm = TRUE)
# Occurences = Rabbit_ACT$Occurences) # not needed
)
preds.ACT_rain = predict(final_ACT2, newdata = ACT_rain, se.fit = TRUE, type = "link")
preds.ACT_rain.ci = pois_ci(preds.ACT_rain, ACT_rain)
ACT.a = ggplot(data = na.omit(preds.ACT_rain.ci)) +
geom_point(
data = Rabbit_ACT, # use observed data for points
aes(x = A_Prec_Avg30Yr, y = Occurences),
alpha = 0.3
) +
geom_line(aes(x = A_Prec_Avg30Yr, fit)) +
geom_ribbon(
aes(ymin = lwr, ymax = upr, x = A_Prec_Avg30Yr, y = fit), # set `y` to `fit`
alpha = .2
) +
labs(x = "Mean Average 30yr AVerage Percipitation (mL)", "No.of Rabbit Occurences") +
# ylim(0, 5000) + # remove or adjust `ylim` to avoid cutting off CI
theme_classic()
ACT.a

Related

Getting different ggplot when using purrr::map

I am trying to use map() to generate a list of ggplot objects that I will combine with ggpubr::ggarrange after.
The stock ggplot call works fine:
# library(tidyverse)
ggplot(Tile,aes(x = Site, y = Volume))+
geom_point(aes(color = Event))+
geom_line(aes(x = Site, y = Volume, group = Event))+
ggtitle(paste("Tile", 'Volume'))
output (desired plot):
Now I use map() with the slightly modified but overall same ggplot call (should be):
x<-map(names(Tile)[-(1:2)], ~
ggplot(Tile,aes(x = Site, y = .x))+
geom_point(aes(color = Event))+
geom_line(aes(x = Site, y = .x, group = Event))+
ggtitle(paste("Tile", as.character(.x)))
)
which gives:
x[[5]]
What am I missing? Thanks.
data:
Tile<-structure(list(Event = c("10/17/2019", "10/17/2019", "10/23/2019",
"10/23/2019", "10/27/2019", "10/27/2019", "10/31/2019", "10/31/2019",
"11/24/2019", "11/24/2019", "11/28/2019", "11/28/2019", "12/10/2019",
"12/10/2019", "12/15/2019", "12/15/2019", "12/28/2019", "12/28/2019",
"12/30/2019", "12/30/2019", "1/3/2020", "1/3/2020", "1/12/2020",
"1/12/2020", "1/26/2020", "1/26/2020", "3/3/2020", "3/3/2020",
"3/8/2020", "3/8/2020", "3/13/2020", "3/13/2020", "5/12/2020",
"5/12/2020", "8/5/2020", "8/5/2020", "9/30/2020", "9/30/2020",
"12/1/2020", "12/1/2020", "12/25/2020", "12/25/2020", "1/17/2021",
"1/17/2021", "3/11/2021", "3/11/2021", "4/16/2021", "4/16/2021",
"4/22/2021", "4/22/2021", "4/30/2021", "4/30/2021", "5/6/2021",
"5/6/2021", "7/2/2021", "7/2/2021", "7/3/2021", "7/3/2021", "7/9/2021",
"7/9/2021", "7/13/2021", "7/13/2021", "7/14/2021", "7/14/2021",
"7/18/2021", "7/18/2021", "7/19/2021", "7/19/2021", "7/21/2021",
"7/21/2021", "7/31/2021", "7/31/2021", "8/2/2021", "8/2/2021",
"8/20/2021", "8/20/2021", "9/9/2021", "9/9/2021", "9/24/2021",
"9/24/2021", "10/17/2021", "10/17/2021", "10/22/2021", "10/22/2021",
"10/25/2021", "10/25/2021", "10/27/2021", "10/27/2021", "11/1/2021",
"11/1/2021"), Site = structure(c(3L, 1L, 3L, 1L, 3L, 1L, 3L,
1L, 3L, 1L, 3L, 1L, 3L, 1L, 3L, 1L, 3L, 1L, 3L, 1L, 3L, 1L, 3L,
1L, 3L, 1L, 3L, 1L, 3L, 1L, 3L, 1L, 3L, 1L, 3L, 1L, 3L, 1L, 3L,
1L, 3L, 1L, 3L, 1L, 3L, 1L, 3L, 1L, 3L, 1L, 3L, 1L, 3L, 1L, 3L,
1L, 3L, 1L, 3L, 1L, 3L, 1L, 3L, 1L, 3L, 1L, 3L, 1L, 3L, 1L, 3L,
1L, 3L, 1L, 3L, 1L, 3L, 1L, 3L, 1L, 3L, 1L, 3L, 1L, 3L, 1L, 3L,
1L, 3L, 1L), .Label = c("DCNSUB", "DCNSUR", "DCSSUB", "DCSSUR"
), class = "factor"), TP.conc = c(1550, 2770, NA, NA, 1650, NA,
1810, NA, 666, 468, 1190, 1120, 574, 538, 487, 580, NA, 238,
610, 378, 398, 306, 744, 766, 447, 468, 504, 413, 384, 377, 714,
542, 927.2, 1000.1, 265.4, 285.1, 1527, 1764.5, NA, 460.9, NA,
469.8, NA, 172.8, 454, 432.8, 524.4, 476.4, 300, 303.6, 588.7,
598.1, 852.5, 797.6, 144.4, 122.1, 170.6, 110.1, 301.8, 328.8,
363.3, 498.5, 283.7, 104.9, 314.1, 327.6, 436.6, 262.1, 398.1,
358, 312, 251, 598, 831, 348, 456, 345, 240, 648, 949, 852, 1260,
643, 549, 712, 999, 982, 1100, 1240, 1555), TP.load = c(180.4,
NA, NA, NA, 67.6, NA, 201.5, NA, NA, NA, 53.3, 131.7, 12.1, 38.1,
7, 21.6, NA, NA, 21.2, 44.4, 6.1, 10.9, 79.7, 189.9, 10.5, 27.9,
84.2, 178.7, 13.6, 46.3, 14.4, 26.2, 11.4, 35, 4.2, 10.1, 4.6,
18.9, NA, 58.3, NA, 140.9, NA, 7.6, 181.8, 238.2, 72.5, 97.7,
18.5, 30.6, 121.4, 177.7, 114.1, 166.3, 22.8, 21.9, 15.1, 9.2,
25.8, 29.4, 7.6, 12.3, 3, 1.7, 58.5, 71, 23.3, 15.7, 32.7, 39.7,
3.1, 3.4, 67.2, 126, 49.1, 79.1, 8.6, 5.8, 8.7, 15.3, 38.62755,
62.40857143, NA, NA, NA, NA, NA, NA, NA, NA), SRP.conc = c(NA,
NA, NA, NA, 403, NA, NA, NA, NA, NA, NA, NA, 245, 234, 238, 197,
NA, 118, NA, NA, NA, NA, NA, 270, 121, 135, NA, NA, NA, NA, NA,
NA, 596.7, 635.6, 48, 85.9, 514.8, 572.7, NA, 161.5, NA, 163.3,
NA, 46.4, 96.9, 127, 83.1, 92.3, 53.5, 60.7, 111.7, 133.7, 132.2,
164.1, 50.1, 49.1, 54, 42.5, 122.5, 131.9, 104.2, 194.5, 84.6,
34.8, 90.2, 106.6, NA, NA, 129.9, 118.2, 62.2, 84.7, 105, 152,
92.6, 66, 45.9, 50.5, 66.2, 167, 264, 412, 203, 175, 352, 560,
503, 625, 621, 836), SRP.load = c(NA, NA, NA, NA, 16.5, NA, NA,
NA, NA, NA, NA, NA, 5.2, 16.6, 3.4, 7.3, NA, NA, NA, NA, NA,
NA, NA, 66.9, 2.8, 8, NA, NA, NA, NA, NA, NA, 7.3, 22.2, 0.8,
3.1, 1.6, 6.1, NA, 20.4, NA, 49, NA, 2, 38.8, 69.9, 11.5, 18.9,
3.3, 6.1, 23, 39.7, 17.7, 34.2, 7.9, 8.8, 4.8, 3.6, 10.5, 11.8,
2.2, 4.8, 0.9, 0.6, 16.8, 23.1, NA, NA, 10.7, 13.1, 0.6, 1.2,
11.8, 23, 13.1, 11.4, 1.1, 1.2, 0.9, 2.7, 12, 20.4, NA, NA, NA,
NA, NA, NA, NA, NA), Volume = c(11.64, NA, 1.87, 4.5, 4.1, 9.69,
11.13, 34, NA, NA, 4.48, 11.76, 2.1, 7.08, 1.45, 3.73, NA, NA,
3.47, 11.74, 1.52, 3.56, 10.71, 24.79, 2.34, 5.96, 16.71, 43.28,
3.54, 12.29, 2.02, 4.84, 1.22, 3.5, 1.59, 3.56, 0.3, 1.07, NA,
12.66, NA, 29.99, NA, 4.37, 40.04, 55.03, 13.82, 20.51, 6.18,
10.07, 20.62, 29.72, 13.38, 20.85, 15.76, 17.96, 8.82, 8.36,
8.56, 8.94, 2.1, 2.46, 1.07, 1.58, 18.64, 21.67, 5.33, 6, 8.22,
11.09, 0.99, 1.36, 11.23, 15.16, 14.11, 17.34, 2.48, 2.4, 1.34,
1.61, 4.53, 4.95, NA, NA, NA, NA, NA, NA, NA, NA)), row.names = c(1L,
4L, 5L, 8L, 9L, 12L, 13L, 16L, 17L, 20L, 21L, 24L, 25L, 28L,
29L, 32L, 33L, 36L, 37L, 40L, 41L, 44L, 45L, 48L, 49L, 52L, 53L,
56L, 57L, 60L, 61L, 64L, 65L, 68L, 69L, 72L, 73L, 76L, 77L, 80L,
81L, 84L, 85L, 88L, 89L, 92L, 93L, 96L, 97L, 100L, 101L, 104L,
105L, 108L, 109L, 112L, 113L, 116L, 117L, 120L, 121L, 124L, 125L,
128L, 129L, 132L, 133L, 136L, 137L, 140L, 141L, 144L, 145L, 148L,
149L, 152L, 153L, 156L, 157L, 160L, 161L, 164L, 165L, 168L, 169L,
172L, 173L, 176L, 177L, 180L), class = "data.frame")
The .x is the individual column names, as aes can take only symbol or unquoted column names, use .data to subset the data column or another option is to convert to symbol and evaluate (!!). But, it is better to use .data
library(purrr)
library(ggplot2)
x <- map(names(Tile)[-(1:2)], ~
ggplot(Tile,aes(x = Site, y = .data[[.x]]))+
geom_point(aes(color = Event))+
geom_line(aes(x = Site, y = .data[[.x]], group = Event))+
ggtitle(paste("Surface", .x))
)
-output
x[[5]]

for loop that lets you pipe in different column names to dplyr pipe

I want to run four linear regressions from four metrics from two sites.
Site DCNSUB is the response variable and DCSSUB is the predictor in the regression.
I only want to regress where I have a complete pair of data for an event.
I do this for one metric at a time using the following dplyr pipe:
mV<-Tile%>% # model V for Volume
select(Event, Site, Volume)%>%
group_by(Event)%>%
filter(!any(is.na(all_of(Volume))))%>% # group by event and remove pairs that are missing volume
ungroup()%>%
mutate(Volume = log(Volume))%>% # take log
pivot_wider(names_from = Site,values_from = Volume) %>% # get responsive and predictor variable data into columns
as.data.frame(.)%>%
lm(DCNSUB~DCSSUB, data = .)
How can incorporate this into a for loop, where each iteration puts a different metric where 'Volume' is in the pipe? Here is my attempt:
for (i in names(Tile[-c(1,2)])){
mX<-Tile%>%
select(Event, Site, i)%>%
group_by(Event)%>%
filter(!any(is.na(all_of(i))))%>% # remove pairs that are missing i, note group by event helps removes the pairs
ungroup()%>%
mutate(i = log(i))%>% # take log
pivot_wider(names_from = Site,values_from = i) %>%# get responsive and predictor variable data into columns
as.data.frame(.)%>%
lm(DCNSUB~DCSSUB, data = .)
}
There have been other posts that use column indexing to call columns, but this doesn't work when trying to mix it with the column I want to remain constant in each loop. Also, those solution are for much less complicated pipes. Any help is appreciated, thanks.
data:
Tile<-structure(list(Event = c("10/17/2019", "10/17/2019", "10/23/2019",
"10/23/2019", "10/27/2019", "10/27/2019", "10/31/2019", "10/31/2019",
"11/24/2019", "11/24/2019", "11/28/2019", "11/28/2019", "12/10/2019",
"12/10/2019", "12/15/2019", "12/15/2019", "12/28/2019", "12/28/2019",
"12/30/2019", "12/30/2019", "1/3/2020", "1/3/2020", "1/12/2020",
"1/12/2020", "1/26/2020", "1/26/2020", "3/3/2020", "3/3/2020",
"3/8/2020", "3/8/2020", "3/13/2020", "3/13/2020", "5/12/2020",
"5/12/2020", "8/5/2020", "8/5/2020", "9/30/2020", "9/30/2020",
"12/1/2020", "12/1/2020", "12/25/2020", "12/25/2020", "1/17/2021",
"1/17/2021", "3/11/2021", "3/11/2021", "4/16/2021", "4/16/2021",
"4/22/2021", "4/22/2021", "4/30/2021", "4/30/2021", "5/6/2021",
"5/6/2021", "7/2/2021", "7/2/2021", "7/3/2021", "7/3/2021", "7/9/2021",
"7/9/2021", "7/13/2021", "7/13/2021", "7/14/2021", "7/14/2021",
"7/18/2021", "7/18/2021", "7/19/2021", "7/19/2021", "7/21/2021",
"7/21/2021", "7/31/2021", "7/31/2021", "8/2/2021", "8/2/2021",
"8/20/2021", "8/20/2021", "9/9/2021", "9/9/2021", "9/24/2021",
"9/24/2021", "10/17/2021", "10/17/2021", "10/22/2021", "10/22/2021",
"10/25/2021", "10/25/2021", "10/27/2021", "10/27/2021", "11/1/2021",
"11/1/2021"), Site = structure(c(3L, 1L, 3L, 1L, 3L, 1L, 3L,
1L, 3L, 1L, 3L, 1L, 3L, 1L, 3L, 1L, 3L, 1L, 3L, 1L, 3L, 1L, 3L,
1L, 3L, 1L, 3L, 1L, 3L, 1L, 3L, 1L, 3L, 1L, 3L, 1L, 3L, 1L, 3L,
1L, 3L, 1L, 3L, 1L, 3L, 1L, 3L, 1L, 3L, 1L, 3L, 1L, 3L, 1L, 3L,
1L, 3L, 1L, 3L, 1L, 3L, 1L, 3L, 1L, 3L, 1L, 3L, 1L, 3L, 1L, 3L,
1L, 3L, 1L, 3L, 1L, 3L, 1L, 3L, 1L, 3L, 1L, 3L, 1L, 3L, 1L, 3L,
1L, 3L, 1L), .Label = c("DCNSUB", "DCNSUR", "DCSSUB", "DCSSUR"
), class = "factor"), TP.conc = c(1550, 2770, NA, NA, 1650, NA,
1810, NA, 666, 468, 1190, 1120, 574, 538, 487, 580, NA, 238,
610, 378, 398, 306, 744, 766, 447, 468, 504, 413, 384, 377, 714,
542, 927.2, 1000.1, 265.4, 285.1, 1527, 1764.5, NA, 460.9, NA,
469.8, NA, 172.8, 454, 432.8, 524.4, 476.4, 300, 303.6, 588.7,
598.1, 852.5, 797.6, 144.4, 122.1, 170.6, 110.1, 301.8, 328.8,
363.3, 498.5, 283.7, 104.9, 314.1, 327.6, 436.6, 262.1, 398.1,
358, 312, 251, 598, 831, 348, 456, 345, 240, 648, 949, 852, 1260,
643, 549, 712, 999, 982, 1100, 1240, 1555), TP.load = c(180.4,
NA, NA, NA, 67.6, NA, 201.5, NA, NA, NA, 53.3, 131.7, 12.1, 38.1,
7, 21.6, NA, NA, 21.2, 44.4, 6.1, 10.9, 79.7, 189.9, 10.5, 27.9,
84.2, 178.7, 13.6, 46.3, 14.4, 26.2, 11.4, 35, 4.2, 10.1, 4.6,
18.9, NA, 58.3, NA, 140.9, NA, 7.6, 181.8, 238.2, 72.5, 97.7,
18.5, 30.6, 121.4, 177.7, 114.1, 166.3, 22.8, 21.9, 15.1, 9.2,
25.8, 29.4, 7.6, 12.3, 3, 1.7, 58.5, 71, 23.3, 15.7, 32.7, 39.7,
3.1, 3.4, 67.2, 126, 49.1, 79.1, 8.6, 5.8, 8.7, 15.3, 38.62755,
62.40857143, NA, NA, NA, NA, NA, NA, NA, NA), SRP.conc = c(NA,
NA, NA, NA, 403, NA, NA, NA, NA, NA, NA, NA, 245, 234, 238, 197,
NA, 118, NA, NA, NA, NA, NA, 270, 121, 135, NA, NA, NA, NA, NA,
NA, 596.7, 635.6, 48, 85.9, 514.8, 572.7, NA, 161.5, NA, 163.3,
NA, 46.4, 96.9, 127, 83.1, 92.3, 53.5, 60.7, 111.7, 133.7, 132.2,
164.1, 50.1, 49.1, 54, 42.5, 122.5, 131.9, 104.2, 194.5, 84.6,
34.8, 90.2, 106.6, NA, NA, 129.9, 118.2, 62.2, 84.7, 105, 152,
92.6, 66, 45.9, 50.5, 66.2, 167, 264, 412, 203, 175, 352, 560,
503, 625, 621, 836), SRP.load = c(NA, NA, NA, NA, 16.5, NA, NA,
NA, NA, NA, NA, NA, 5.2, 16.6, 3.4, 7.3, NA, NA, NA, NA, NA,
NA, NA, 66.9, 2.8, 8, NA, NA, NA, NA, NA, NA, 7.3, 22.2, 0.8,
3.1, 1.6, 6.1, NA, 20.4, NA, 49, NA, 2, 38.8, 69.9, 11.5, 18.9,
3.3, 6.1, 23, 39.7, 17.7, 34.2, 7.9, 8.8, 4.8, 3.6, 10.5, 11.8,
2.2, 4.8, 0.9, 0.6, 16.8, 23.1, NA, NA, 10.7, 13.1, 0.6, 1.2,
11.8, 23, 13.1, 11.4, 1.1, 1.2, 0.9, 2.7, 12, 20.4, NA, NA, NA,
NA, NA, NA, NA, NA), Volume = c(11.64, NA, 1.87, 4.5, 4.1, 9.69,
11.13, 34, NA, NA, 4.48, 11.76, 2.1, 7.08, 1.45, 3.73, NA, NA,
3.47, 11.74, 1.52, 3.56, 10.71, 24.79, 2.34, 5.96, 16.71, 43.28,
3.54, 12.29, 2.02, 4.84, 1.22, 3.5, 1.59, 3.56, 0.3, 1.07, NA,
12.66, NA, 29.99, NA, 4.37, 40.04, 55.03, 13.82, 20.51, 6.18,
10.07, 20.62, 29.72, 13.38, 20.85, 15.76, 17.96, 8.82, 8.36,
8.56, 8.94, 2.1, 2.46, 1.07, 1.58, 18.64, 21.67, 5.33, 6, 8.22,
11.09, 0.99, 1.36, 11.23, 15.16, 14.11, 17.34, 2.48, 2.4, 1.34,
1.61, 4.53, 4.95, NA, NA, NA, NA, NA, NA, NA, NA)), row.names = c(1L,
4L, 5L, 8L, 9L, 12L, 13L, 16L, 17L, 20L, 21L, 24L, 25L, 28L,
29L, 32L, 33L, 36L, 37L, 40L, 41L, 44L, 45L, 48L, 49L, 52L, 53L,
56L, 57L, 60L, 61L, 64L, 65L, 68L, 69L, 72L, 73L, 76L, 77L, 80L,
81L, 84L, 85L, 88L, 89L, 92L, 93L, 96L, 97L, 100L, 101L, 104L,
105L, 108L, 109L, 112L, 113L, 116L, 117L, 120L, 121L, 124L, 125L,
128L, 129L, 132L, 133L, 136L, 137L, 140L, 141L, 144L, 145L, 148L,
149L, 152L, 153L, 156L, 157L, 160L, 161L, 164L, 165L, 168L, 169L,
172L, 173L, 176L, 177L, 180L), class = "data.frame")
We may use map/lapply to loop as with for loop it needs a list to store the output which can be created of course, however, the output from map/lapply is itself a list
library(purrr)
library(dplyr)
library(tidyr)
# // loop over the names
out <- map(names(Tile)[-(1:2)], ~ Tile %>%
# // select the columns of interest along with looped column names
select(Event, Site, all_of(.x))%>%
# // grouped by Event and remove groups based on the NA in the looped column
group_by(Event)%>%
filter(!any(is.na(.data[[.x]])))%>%
ungroup()%>%
# // convert the column looped to its `log`
mutate(!! .x := log(.data[[.x]]))%>%
# // reshape from long to wide
pivot_wider(names_from = Site,values_from = all_of(.x)) %>%
# // build the linear model
lm(DCNSUB~DCSSUB, data = .)
)
-output
> out
[[1]]
Call:
lm(formula = DCNSUB ~ DCSSUB, data = .)
Coefficients:
(Intercept) DCSSUB
-1.475 1.231
[[2]]
Call:
lm(formula = DCNSUB ~ DCSSUB, data = .)
Coefficients:
(Intercept) DCSSUB
0.5418 0.9812
[[3]]
Call:
lm(formula = DCNSUB ~ DCSSUB, data = .)
Coefficients:
(Intercept) DCSSUB
0.09282 1.00866
[[4]]
Call:
lm(formula = DCNSUB ~ DCSSUB, data = .)
Coefficients:
(Intercept) DCSSUB
0.7099 0.9064
[[5]]
Call:
lm(formula = DCNSUB ~ DCSSUB, data = .)
Coefficients:
(Intercept) DCSSUB
0.8000 0.8535
From the list output, either use tidy (from broom) to convert to a tibble output or extract the components separately by looping (It can be done in the same map looped earlier though)
map_dfr(out, ~ {
v1 <- summary(.x)
tibble(pval = v1$coefficients[,4][2], MSE = v1$sigma^2)})
# A tibble: 5 × 2
pval MSE
<dbl> <dbl>
1 7.23e-17 0.0773
2 5.81e-13 0.267
3 3.49e-12 0.120
4 3.77e-10 0.238
5 2.10e-15 0.156

ggstance(), plotting together boxplot and density plot - R

I'm trying to do a boxplot and a density plot for each numeric variable in the dataframe. I'm adapting this solution to my problem. However, I'm trying to plot for each numeric variable in the dataframe (no for just one as in the solution above):
df %>%
select_if(is.numeric) %>%
gather(na.rm=TRUE) %>%
ggplot(aes(x = value, y = -0.5)) +
geom_boxploth() +
geom_density(aes(x = value), inherit.aes = FALSE) +
facet_wrap(~key, scales = 'free')
And the main problem relies in the scales of each variable. I have tried with free_x, free_y, and free in the scales argument from facet_wrap(), and all of them gives me some esthetic problems:
scales="free": The width of boxplot changes in the graphs, making it kind of messy.
scales="free_x": The plots are almost perfect, the only problem
relies that some density plots aren't readable.
scales="free_y": The worst one.
As the numeric variables have very different ranges, the x-axis needs to be free in order to plot an understandable boxplot. But if I fix the y-axis (with scales="free_x") a density plot with a peak of, for example 0.1, will be shown as a straight line in the fixed axis from 0 to 1 (air_temperature variable, for example).
The closest solution I have achieved is with "free_x" but I haven't been able to make the density plots more readable.
Any suggestions?
The data (100 rows):
df <- structure(list(site_id = c("2", "13", "2", "2", "14", "13", "9",
"2", "4", "13", "3", "13", "6", "13", "13", "14", "11", "13",
"14", "8", "2", "2", "0", "15", "14", "14", "5", "15", "3", "4",
"13", "13", "7", "9", "8", "2", "9", "13", "13", "13", "3", "0",
"6", "2", "4", "2", "3", "3", "13", "8", "2", "13", "4", "8",
"7", "5", "9", "2", "13", "5", "2", "9", "3", "9", "3", "13",
"13", "1", "9", "14", "8", "13", "13", "14", "13", "7", "13",
"5", "3", "14", "9", "15", "9", "9", "11", "3", "9", "13", "2",
"9", "9", "3", "9", "9", "14", "9", "9", "3", "2", "13"), building_id = c("187",
"1137", "214", "254", "1234", "1218", "878", "236", "631", "1169",
"472", "1148", "747", "1179", "1152", "1233", "1031", "1154",
"1242", "826", "271", "217", "62", "1341", "1245", "1320", "660",
"1352", "526", "632", "1204", "1083", "797", "891", "858", "252",
"879", "1145", "1154", "1158", "372", "76", "775", "271", "607",
"247", "337", "353", "1089", "826", "168", "1114", "566", "836",
"796", "662", "988", "191", "1200", "697", "207", "980", "343",
"966", "353", "1162", "1217", "115", "925", "1319", "848", "1139",
"1075", "1253", "1217", "798", "1220", "741", "491", "1226",
"945", "1429", "952", "916", "1032", "380", "919", "1112", "256",
"884", "890", "295", "965", "987", "1224", "946", "928", "350",
"280", "1143"), primary_use = c("Education", "Office", "Education",
"Entertainment/public assembly", "Education", "Entertainment/public assembly",
"Lodging/residential", "Entertainment/public assembly", "Education",
"Education", "Education", "Office", "Education", "Entertainment/public assembly",
"Office", "Education", "Education", "Lodging/residential", "Office",
"Public services", "Education", "Education", "Lodging/residential",
"Education", "Healthcare", "Entertainment/public assembly", "Entertainment/public assembly",
"Office", "Public services", "Education", "Office", "Education",
"Education", "Education", "Other", "Education", "Education",
"Manufacturing/industrial", "Lodging/residential", "Public services",
"Education", "Education", "Office", "Education", "Education",
"Education", "Entertainment/public assembly", "Education", "Office",
"Public services", "Education", "Other", "Education", "Public services",
"Education", "Entertainment/public assembly", "Office", "Education",
"Entertainment/public assembly", "Education", "Public services",
"Entertainment/public assembly", "Public services", "Education",
"Education", "Office", "Education", "Education", "Education",
"Entertainment/public assembly", "Office", "Office", "Office",
"Education", "Education", "Education", "Public services", "Education",
"Education", "Education", "Office", "Education", "Education",
"Education", "Education", "Education", "Education", "Office",
"Education", "Education", "Education", "Education", "Lodging/residential",
"Education", "Office", "Education", "Education", "Education",
"Entertainment/public assembly", "Office"), square_feet = c(44203L,
27452L, 91273L, 254766L, 46037L, 268480L, 41241L, 144086L, 23533L,
155439L, 69400L, 861524L, 30531L, 143228L, 118858L, 43143L, 93206L,
262156L, 23871L, 19446L, 71421L, 282946L, 42731L, 18342L, 35354L,
34565L, 5447L, 42755L, 7636L, 41297L, 38382L, 100813L, 764237L,
50846L, 1566L, 4314L, 110235L, 35201L, 262156L, 294812L, 49100L,
128887L, 104355L, 71421L, 34825L, 64560L, 22334L, 99700L, 62837L,
19446L, 183460L, 64810L, 2010L, 8168L, 226042L, 12777L, 24463L,
78268L, 545351L, 18697L, 99700L, 34819L, 22000L, 393530L, 99700L,
72958L, 73044L, 129716L, 54405L, 287419L, 8552L, 336650L, 72709L,
134748L, 73044L, 409028L, 381221L, 14025L, 66600L, 27995L, 332447L,
40461L, 218024L, 51778L, 127632L, 95100L, 31233L, 190471L, 16744L,
38598L, 202937L, 83400L, 45086L, 166395L, 23481L, 177216L, 179115L,
83800L, 105505L, 96612L), year_built = c(1950L, NA, 1951L, 1989L,
NA, NA, NA, 1989L, 1906L, NA, 1911L, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, 2003L, 2001L, 1960L, NA, NA, 1976L, NA, NA, 1959L,
NA, NA, 1979L, NA, NA, 1907L, NA, NA, NA, NA, 1911L, 2009L, NA,
NA, 1999L, 1965L, 1977L, 1955L, NA, NA, 2005L, NA, 1957L, NA,
1965L, 1976L, NA, 1964L, NA, 1919L, NA, NA, NA, NA, 1955L, NA,
NA, 1968L, NA, NA, NA, NA, NA, NA, NA, 1970L, NA, 1966L, 1970L,
NA, NA, 2002L, NA, NA, NA, 1927L, NA, NA, NA, NA, NA, 1942L,
NA, NA, NA, NA, NA, 1944L, 2008L, NA), floor_count = c(NA, NA,
NA, NA, NA, NA, NA, NA, 3L, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, 1L, NA, NA, NA, NA, NA, NA, 1L, NA, NA, 6L, NA, NA, 13L,
NA, 1L, NA, NA, NA, NA, NA, NA, NA, NA, NA, 2L, NA, NA, NA, NA,
1L, NA, NA, 1L, 1L, 2L, 2L, NA, NA, NA, 2L, NA, NA, NA, NA, NA,
NA, NA, 6L, NA, NA, 1L, NA, NA, NA, NA, 21L, NA, 1L, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA), meter = c("hotwater", "electricity", "chilledwater",
"electricity", "chilledwater", "chilledwater", "chilledwater",
"hotwater", "electricity", "electricity", "electricity", "electricity",
"chilledwater", "electricity", "steam", "chilledwater", "chilledwater",
"steam", "hotwater", "electricity", "electricity", "hotwater",
"electricity", "steam", "electricity", "electricity", "electricity",
"chilledwater", "electricity", "electricity", "steam", "chilledwater",
"steam", "electricity", "electricity", "electricity", "steam",
"steam", "chilledwater", "electricity", "electricity", "chilledwater",
"electricity", "electricity", "electricity", "hotwater", "electricity",
"electricity", "electricity", "electricity", "electricity", "electricity",
"electricity", "electricity", "steam", "electricity", "electricity",
"electricity", "steam", "electricity", "electricity", "electricity",
"electricity", "electricity", "electricity", "chilledwater",
"electricity", "electricity", "electricity", "electricity", "electricity",
"chilledwater", "steam", "hotwater", "chilledwater", "steam",
"steam", "electricity", "electricity", "electricity", "steam",
"electricity", "chilledwater", "steam", "hotwater", "electricity",
"electricity", "steam", "chilledwater", "electricity", "chilledwater",
"electricity", "electricity", "chilledwater", "hotwater", "steam",
"steam", "electricity", "electricity", "electricity"), timestamp = structure(c(1456984800,
1464206400, 1478361600, 1464498000, 1472634000, 1465473600, 1480856400,
1473026400, 1464656400, 1464451200, 1454306400, 1464332400, 1452088800,
1473721200, 1462356000, 1475690400, 1453737600, 1456063200, 1477494000,
1470546000, 1466690400, 1479898800, 1467774000, 1470502800, 1464285600,
1479355200, 1475762400, 1473930000, 1452841200, 1468245600, 1470355200,
1465923600, 1461913200, 1456610400, 1476234000, 1452837600, 1469300400,
1466226000, 1477594800, 1453582800, 1461042000, 1460062800, 1470020400,
1467921600, 1476741600, 1470337200, 1482840000, 1472911200, 1467972000,
1460707200, 1475946000, 1466064000, 1464739200, 1471417200, 1461250800,
1462464000, 1475593200, 1458612000, 1461812400, 1464490800, 1464134400,
1453935600, 1452762000, 1476414000, 1480496400, 1469430000, 1464714000,
1463119200, 1472698800, 1477155600, 1456045200, 1480888800, 1475528400,
1463544000, 1463274000, 1455429600, 1482138000, 1471478400, 1461909600,
1466841600, 1457560800, 1463551200, 1481346000, 1453608000, 1478336400,
1460030400, 1458090000, 1464274800, 1478458800, 1478559600, 1470582000,
1453190400, 1467662400, 1478887200, 1461481200, 1460746800, 1459778400,
1479168000, 1461967200, 1482433200), class = c("POSIXct", "POSIXt"
), tzone = "UTC"), meter_reading = c(14.6536, 29, 6.1193, 179.17,
487.708, 90.1306, 92.9855, 0, 7.0825, 0, 77.34, 1018.46, 0, 1204,
6148.44, 0, 0, 5781.25, 162.07, 8, 140.2, 5.8614, 600.926, 366.595,
78.8438, 71.7563, 13.765, 63.5949, 27.84, 66.5, 143.555, 550.446,
843.81, 27, 8.8333, 33.34, 286.7, 271.484, 0, 163.625, 32.69,
4305.46, 133.25, 120.94, 20.75, 43.9606, 88.04, 100.74, 321.53,
56.25, 407.15, 3.6, 0.563, 4.0833, 0, 12.1, 36, 60.11, 1636.54,
3.274, 108.52, 18, 21.92, 500, 136.76, 100.626, 123.881, 449,
15, 445.716, 0, 1219.51, 0, 1127.07, 0, 3429.23, 20414.1, 2.1,
54.12, 40.1146, 460.55, 24.525, 184.705, 76.25, 161.753, 137.54,
18, 2843.75, 3.4817, 51, 2026.83, 64.53, 0, 763.578, 340.092,
82.35, 250.1, 88.15, 16.02, 68), air_temperature = c(21.1, 23.3,
20, 29.4, 22.2, 18.3, 10, 38.3, 20.6, 18.9, 8.3, 21.1, -7.2,
23.9, 8.3, 19.4, -3, -2.8, 8.3, 23.9, 33.9, 11.7, 28.3, 26.7,
31.1, 10.6, 14, 5, 5.6, 15.6, 26.7, 21.7, 1.2, 22.2, 22.8, 10,
37.2, 23.9, 8.3, -5.6, 16.1, 27.2, 23.3, 38.3, 20.6, 33.9, 13.3,
23.9, 19.4, 20, 30.6, 18.3, 23.9, 25, 14.8, 18, 25, 30.6, 7.2,
12, 31.1, 13.3, -2.2, 23.9, 13.3, 21.7, 20.6, 11.4, 29.4, 10,
13.3, 2.8, 23.9, 11.1, 9.4, -27.2, -15, 17, 8.9, 18.9, 15.6,
3.3, 5, 8.3, 3.3, 12.8, 27.8, 20.6, 23.9, 21.7, 30, -8.9, 37.2,
19.4, 8.3, 23.9, 16.1, 11.7, 26.7, 1.1), cloud_coverage = c(4L,
NA, 2L, 0L, NA, 8L, NA, 0L, 2L, NA, 6L, NA, 0L, NA, 0L, 0L, NA,
NA, 0L, NA, 4L, NA, NA, NA, 0L, NA, NA, 0L, NA, 0L, 2L, NA, NA,
0L, NA, 2L, 0L, NA, NA, NA, NA, NA, 0L, 2L, 2L, 4L, 6L, NA, NA,
NA, 4L, NA, 2L, 2L, NA, 0L, NA, NA, NA, 0L, 0L, 0L, 6L, NA, 8L,
0L, NA, NA, 0L, NA, NA, NA, 2L, 0L, 4L, NA, 0L, NA, 8L, NA, NA,
0L, NA, 0L, NA, 8L, 0L, 2L, 2L, NA, 4L, 0L, 0L, 2L, 0L, 0L, 0L,
8L, 4L, 2L), dew_temperature = c(-1.7, 18.3, 12.8, 1.1, 21.1,
16.7, 8.9, -2.8, 11.7, 15, 2.2, 11.7, -11.1, 16.7, 0, 8.9, -5.8,
-5.6, -3.3, 22.2, 8.9, 7.2, 25.6, 16.7, 8.3, 6.1, 5, 4.4, -1.1,
11.7, 15.6, 17.2, -6.9, 8.9, 19.4, 2.8, 21.1, 15.6, 5, -10, 11.7,
12.8, 21.7, 12.8, 11.7, 21.1, 11.7, 16.7, 16.7, 18.9, 11.1, 15,
11.7, 23.9, -1.9, 3, 21.1, -6.1, -0.6, 10, -1.1, -6.7, -12.2,
20.6, 13.3, 15.6, 16.1, 9.6, 22.2, 5, 11.7, 0, 10.6, 10, -8.9,
-32.3, -20, 16, 7.2, 16.7, 13.9, 2.2, -3.3, 0, 2.2, 10.6, 7.2,
13.9, 11.1, 20.6, 23.9, -19.4, 22.2, 15, -4.4, 15.6, 9.4, 3.3,
2.2, -7.2), precip_depth_1_hr = c(0L, -1L, 0L, 0L, 0L, -1L, 3L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, NA, 0L, 0L, 0L, 0L, 0L, -1L,
NA, 0L, 0L, NA, NA, 0L, 0L, 0L, -1L, NA, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, NA, 0L, -1L, 0L, 0L, -1L, 0L, 0L, 0L,
0L, NA, NA, 0L, 0L, 0L, NA, 0L, 0L, 0L, -1L, 0L, 0L, -1L, NA,
0L, -1L, 0L, 0L, 0L, 0L, 0L, NA, 0L, NA, 0L, 0L, -1L, NA, 0L,
0L, NA, 8L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L), sea_level_pressure = c(1014.2, 1009.7, 1016.2, 1007.2, 1015.7,
1010.6, 1015.1, 1001.9, 1013.4, 1007.6, 1010.4, 1010.3, 1036.1,
NA, 1013.5, 1024.8, 1023.2, 1023.4, 1030.4, 1015.2, 1009.3, 1017.1,
1020.3, 1009, 1017, 1011.8, NA, 1026.5, 1014.2, 1014.7, 1008.5,
NA, 1021.1, 1020.9, 1019.5, 1015.9, 1013.3, 1017.6, 1022.3, 1016.5,
1019.5, 1011.6, 1016.8, 1009.8, 1017.2, 1009.1, 1015, 1018.3,
1007.9, 1012.7, 1011.5, 1012.2, 1009.6, 1021.2, 1016, NA, 1010,
1008.9, 1012.4, NA, 1008.6, 1024.5, 1015.5, 1019.2, 1012.2, 1014.9,
1011.2, 1007.9, 1013.2, 999.7, 1021.4, 1008.6, 1013.5, 1019.8,
1017.9, 1032.6, 1030.4, NA, 1016.2, 1019.2, 1007.2, 1022, 1034.1,
1022, 1018.1, 1003.2, 1009, 1009, 1015.6, 1017.4, 1014, 1024.5,
1012.2, 1024.8, 1015.1, 1011.1, 1021.9, 1016.5, 1007.8, 1023.6
), wind_direction = c(100L, 110L, 110L, 260L, 240L, 160L, 340L,
240L, 310L, 20L, 210L, 180L, 0L, 300L, 360L, 110L, 60L, 0L, 350L,
160L, 80L, 160L, 0L, 290L, 250L, 290L, 100L, 70L, 210L, 0L, 310L,
110L, 70L, 160L, 20L, 80L, NA, 170L, 150L, 170L, 180L, 260L,
190L, 130L, 270L, 160L, 210L, 30L, 230L, 20L, 90L, 20L, 300L,
350L, 90L, 110L, 180L, 280L, 80L, 60L, 290L, 330L, 250L, 0L,
20L, 290L, 230L, 20L, 0L, 290L, 0L, 220L, 150L, 0L, 330L, 280L,
200L, NA, 50L, 30L, NA, 70L, 0L, 220L, 210L, 170L, 350L, 230L,
0L, NA, 230L, 300L, 200L, 310L, 350L, NA, 210L, 230L, 270L, 230L
), wind_speed = c(3.6, 5.1, 3.6, 7.7, 1.5, 5.7, 2.6, 3.1, 5.7,
1.5, 3.1, 3.1, 0, 4.6, 2.1, 3.1, 1.5, 0, 5.7, 1.5, 3.6, 1.5,
0, 6.7, 4.6, 2.6, 6.2, 1.5, 1.5, 0, 7.7, 3.1, 3.1, 5.1, 3.6,
2.1, 2.1, 5.7, 5.1, 6.7, 3.1, 6.7, 2.1, 2.6, 5.7, 4.1, 7.2, 10.3,
3.1, 2.1, 4.6, 4.1, 5.1, 1.5, 1.5, 4.6, 1.5, 4.6, 11.8, 3.1,
6.7, 3.6, 1.5, 0, 2.1, 2.6, 4.6, 4.1, 0, 6.7, 0, 4.6, 6.7, 0,
6.2, 3.6, 4.6, 2.6, 3.6, 1.5, 2.6, 2.6, 0, 1.5, 1.5, 5.1, 2.1,
5.1, 0, 2.6, 2.6, 3.1, 4.6, 2.1, 4.1, 2.1, 1.5, 1.5, 5.7, 4.6
)), row.names = c(NA, 100L), class = "data.frame")
geom_density(), or more accurately stat_density() also makes available computed aesthetics. Using these, you can set geom_density(aes(x = value, y = stat(density))) to get uniform kernel density estimates:
df %>%
select_if(is.numeric) %>%
gather(na.rm=TRUE) %>%
ggplot(aes(x = value, y = -0.5)) +
geom_boxploth() +
geom_density(aes(x = value, y = stat(scaled)), inherit.aes = FALSE) +
facet_wrap(~key, scales = 'free')
This may resolve your issues with the axes.
Of note, due to the merging of this pull request, it seems likely that we wouldn't need ggstance a lot anymore in the future.

Random effects model in R - error

I am running econometric model with panel data in R. I am using plm package and pooled model and fixed effects model works great. But I get this error when trying to do random effects model and I don't know how to fix it.
There is my whole dataset and code:
auto <- structure(list(Country = structure(c(1L, 1L, 1L, 1L, 2L, 2L,
2L, 2L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 6L, 6L,
6L, 6L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L, 10L,
10L, 10L, 10L, 11L, 11L, 11L, 11L), .Label = c("Bahrain", "Cuba",
"China", "Kuwait", "Lao PDR", "Qatar", "Saudi Arabia", "Swaziland",
"Syria", "United Arab Emirates", "Vietnam"), class = "factor"),
Year = structure(c(1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L,
3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L,
2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L,
1L, 2L, 3L, 4L), .Label = c("1971", "1981", "1991", "2001"
), class = "factor"), AVG_GR_. = c(2.44, -2.93, 1.77, -1.04,
3.17, 3.5, -1.59, 5.13, 4.29, 7.51, 9.42, 9.83, -7.39, -5.52,
10.72, -0.14, 1.77, 3.38, 3.68, 5.33, -1.55, -5.72, 4.64,
1.5, 6.06, -5.25, 0.54, 2.28, 6.99, 2.82, 0.82, 1.12, 6.72,
-2, 3.09, 2.15, -1.06, -4.88, 0.2, -6.04, 1.61, 3.21, 5.88,
6.24), GDP_PC = c(17444.65, 19550.76, 15970.05, 18212.71,
2067.93, 3127.98, 3221.25, 3081.73, 153.5, 231.14, 491.26,
1207.52, 70184.35, 23911.92, 9559.35, 27681.03, 162.06, 212.46,
261.98, 386.38, 72617.74, 55370.39, 31970, 51090.02, 13752.55,
21124.79, 12891.51, 12446.49, 881.75, 1595.82, 1995.8, 2191.36,
738.63, 1349.2, 1057.84, 1380.2, 88377.72, 75348.77, 43306.13,
45038.43, 164.15, 194.45, 267.17, 481.92), POP_. = c(5.39,
3.26, 3.03, 6.49, 1.22, 0.75, 0.5, 0.13, 1.91, 1.71, 0.95,
0.6, 6.22, 4.16, -0.66, 4.61, 1.93, 2.7, 2.42, 1.73, 7.44,
7.9, 2.23, 11.57, 5.43, 5.12, 2.2, 3.08, 3.07, 3.64, 2.12,
1.16, 3.45, 3.35, 2.77, 2.78, 15.96, 5.94, 5.3, 10.95, 2.29,
2.3, 1.62, 0.97), CONSUMP_. = c(64.21, 52.81, 51.47, 40.51,
54.58, 54.96, 62.74, 54.02, 51.72, 51.01, 45.63, 39, 27.44,
48.61, 49.76, 35.74, 90.19, 90.65, 89.15, 70.38, 21.33, 26.27,
26.84, 16.81, 22.96, 46.85, 44.2, 31.61, 54.77, 74.9, 80.42,
79.36, 67.09, 69.71, 69.92, 61.26, 15.28, 33.07, 46.79, 59.97,
90, 89.89, 73.9, 65.33), GOV_CON_. = c(11.1, 19.55, 19.21,
14.27, 31.67, 31.66, 29.47, 34.91, 12.99, 14.11, 14.53, 14.1,
12.04, 23.7, 48.98, 18.45, 8.05, 8.29, 7.21, 8.96, 20.47,
36.49, 31.09, 14.5, 16.02, 30.12, 26.94, 22.53, 19.07, 17.11,
17.65, 14.76, 19.93, 19.6, 12.75, 12.67, 10.87, 19.27, 16.99,
7.66, 6.73, 6.85, 7.46, 6.19), CAP_FORM_. = c(34.15, 32.51,
24.24, 26.56, 25.94, 25.49, 10.76, 10.7, 34.57, 35.19, 37.79,
42.21, 13.55, 18.68, 17.9, 17.28, 7.57, 10.24, 16.68, 30.28,
22.49, 18.37, 26.13, 36.58, 22.59, 22.7, 20.49, 23.68, 30.77,
21.42, 17.65, 14.55, 25.34, 20.68, 22.53, 23.48, 29.93, 26.28,
27.29, 22.63, 14.45, 14.46, 25.22, 36.44), NAT_RES_. = c(27.42,
20.18, 17.52, 23.34, 1.81, 1.87, 2.5, 3.42, 41.09, 38.83,
40.09, 17.91, 66.53, 41.25, 35.94, 48.41, 5.28, 4.2, 3.01,
10.15, 63.5, 40.84, 39.7, 54.17, 57.89, 31.24, 32.74, 42.77,
6.47, 3.64, 2.25, 1.32, 9.55, 9.14, 14.19, 22.92, 51.04,
37.08, 27.99, 31.36, 3.95, 4.17, 8.39, 13.57), TRADE = c(1.69,
1.48, 1.37, 1.34, 0.77, 0.76, 0.33, 0.34, 0.11, 0.21, 0.35,
0.58, 1.03, 0.99, 1.09, 0.9, 0.15, 0.23, 0.63, 0.57, 0.95,
0.82, 0.85, 0.91, 0.89, 0.76, 0.66, 0.8, 1.47, 1.54, 1.42,
1.62, 0.51, 0.44, 0.66, 0.71, 1.1, 0.97, 1.37, 1.23, 0.62,
0.62, 0.86, 1.43), INFL_. = c(13.26, 3.24, 1.64, 5.65, 5.22,
0.11, 5.49, 2.44, 1.17, 5.72, 6.85, 4.2, 31.52, -0.47, 3.25,
7.29, 43.86, 56.9, 32.37, 7.95, 20.84, -1.59, 3.18, 8.65,
26.67, -1.16, 2.4, 5.73, 10.71, 11.36, 10.97, 8.04, 11.62,
17.43, 6.74, 6.78, 28.31, 1.25, 2.03, 6.94, 7.05, 156.6,
18.99, 9.45), LIFE_EXP = c(67.39, 71.47, 73.66, 75.55, 72.28,
74.46, 75.6, 77.81, 65.7, 68.43, 70.64, 73.99, 68.17, 71.25,
72.92, 73.79, 47.79, 51.39, 58.38, 64.68, 71.16, 74.31, 76.18,
77.53, 58.65, 66.77, 71.16, 74.03, 51.33, 57.45, 54.96, 46.81,
63.01, 68.42, 72.03, 74.56, 65.49, 70.19, 73.24, 75.66, 62.69,
69.09, 72.28, 74.66), EDU_T = c(0.68, 1.59, 2.63, 3.14, 0.75,
1.46, 2.81, 3.84, 0.37, 0.62, 1.08, 1.71, 1.41, 2.71, 3.53,
3.54, 0.16, 0.35, 0.65, 1, 1.61, 2.11, 2.5, 3.06, 1.06, 1.44,
2.13, 2.66, 0.35, 0.74, 1.07, 0.91, 0.34, 0.74, 1.27, 1.3,
1.14, 1.65, 2.61, 3.85, 0.67, 1.21, 0.67, 1.54)), .Names = c("Country",
"Year", "AVG_GR_.", "GDP_PC", "POP_.", "CONSUMP_.", "GOV_CON_.",
"CAP_FORM_.", "NAT_RES_.", "TRADE", "INFL_.", "LIFE_EXP", "EDU_T"
), row.names = c(1L, 2L, 3L, 4L, 9L, 10L, 11L, 12L, 5L, 6L, 7L,
8L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 25L, 26L, 27L, 28L,
29L, 30L, 31L, 32L, 33L, 34L, 35L, 36L, 37L, 38L, 39L, 40L, 41L,
42L, 43L, 44L, 45L, 46L, 47L, 48L), class = c("plm.dim", "data.frame"
))
Y <- cbind(auto$AVG_GR_.)
X <- cbind(auto$GDP_PC, auto$POP_., auto$CONSUMP_., auto$GOV_CON_.,
auto$CAP_FORM_., auto$NAT_RES_., auto$TRADE, auto$INFL_.,
auto$LIFE_EXP, auto$EDU_T)
pdata <- plm.data(auto, c("Country", "Year"))
random <- plm(Y~X, data=pdata, model="random")
Everything is OK until the last row. I get this error:
Error in if (sigma2$id < 0) stop(paste("the estimated variance of the", :
missing value where TRUE/FALSE needed
Thanks for your help :)
I am looking for help, but solved your problem. The first column has row. Names automatically filled in. You need to delete first column.
This worked:
> pdata <- pdata[,2:13];
> random <- plm(Y~X, data=pdata, model="random")
Just replace last row of your code with the above two lines.

month language in the as.date function

I don't know how to figure out this problem. That's a piece of my df:
structure(list(ID = 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), .Label = c("B0", "B1", "B12", "B2", "B21", "B22", "B26",
"B3", "B33", "B4", "B7", "P1", "P21", "P24", "P24 ", "P25", "P27",
"P28", "P29"), class = "factor"), Date = structure(c(9839, 9946,
10045, 10133, 10190, 10302, 10354, 10423, 10528, 10676, 10756,
10841, 10904, 11032, 11129, 11227, 11290, 11390, 11485, 11571,
11645, 11725, 11843, 11928, 12003, 12128, 12221, 12305, 12380,
12499, 12549, 12640, 12716, 12856, 12926, 12996, 13104, 13580,
13671, 13759, 13802), class = "Date"), T = c(9.6, 10.1, 10.4,
9.9, 9.4, 9.8, 10, 9.8, 9.8, 9.9, 10.3, 10.6, 9.9, 10, 10.3,
10.1, 10.3, 10, 10.2, 10.4, 10.1, 10.1, 10.1, 10.5, 10.3, NA,
NA, NA, NA, 10.3, 10.4, 10.9, 10.6, 10.4, 10.7, 10.2, 10, 10.2,
10.6, 10.5, 10.4), ph = c(6.9, 7.08, 6.96, 7, 7, 6.97, 6.92,
7.02, 6.93, 6.91, 6.83, 6.87, 6.8, 6.92, 7.02, 6.94, 6.94, 6.86,
6.9, 6.89, 6.9, 6.97, 6.92, 6.93, 6.91, 6.88, 6.93, 6.78, 6.87,
6.91, 6.82, 6.91, 6.98, 6.99, 6.79, 6.91, 6.61, 6.86, 6.93, 6.88,
6.74), EC = c(2810, 3020, 2170, 2511, 1695, 3100, 2510, 1759,
1101, 3330, 5370, 3300, 3210, 921, 2300, 3380, 3340, 2850, 3430,
3510, 3450, 3400, 3280, 3170, 3210, 3250, 3010, 2970, 3080, 3120,
3100, 3040, 3100, 2940, 3050, 3070, 3040, 2270, 2990, 2830, 3010
), O2 = c(0.1, 0.1, 1.3, 0.2, 0.2, 0.1, NA, 0.2, 0.1, 0.2, 0.1,
NA, NA, NA, 0.1, 0.1, NA, 0.1, 0.1, 0.2, NA, NA, 0.2, 0.1, 0.1,
0, 0, 0.1, 0.4, 0.2, 0.2, 0.3, 0.2, 0.1, 0.1, 0.3, 0.7, 0.2,
0.4, 0.2, 0.2), Cl = c(696, 718, 722, 856, 776, 752, 745, 788,
822, 727, 650, 800, 766, 700, 800, 720, 760, 710, 730, 720, 810,
610, 720, 830, 820, 740, 670, 510, 710, 500, 640, 630, 650, 430,
660, 660, 630, 560, 680, 670, 670), SO4 = c(152, 111, 133, 245,
194, 110, 105, 104, 185, 156, 137, 194, 196, 170, 220, 230, 240,
200, 220, 200, 220, 170, 230, 210, 240, 280, 240, 190, 260, 360,
280, 250, 220, 380, 240, 240, 230, 320, 220, 210, 220), NO2 = c(NA,
NA, NA, NA, NA, NA, NA, 0.0067, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, 0.015, NA, NA, NA, NA, NA, 0.01,
NA, NA, NA, 0.031, NA, NA, NA, NA, NA, NA, NA), NO3 = c(0.15,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, 0.06, NA, 0.02, NA, 0.02, 0.07, 0.2,
0.02, NA, NA, NA, 0.05, 0.08, NA, NA, NA, NA, NA), Fe = c(22,
20, NA, 23, NA, 25, NA, NA, NA, NA, 27, NA, NA, NA, 32, NA, NA,
NA, 33, NA, NA, NA, 33, NA, NA, NA, 29, NA, NA, NA, 9, NA, NA,
NA, 8.3, NA, NA, NA, 17, NA, NA), Mn = c(3.8, 3.8, NA, 4.5, NA,
4.7, NA, NA, NA, NA, 4.9, NA, NA, NA, 5.8, NA, NA, NA, 6, NA,
NA, NA, 6, NA, NA, NA, 5.3, NA, NA, NA, 4.1, NA, NA, NA, 4.2,
NA, NA, NA, 4.9, NA, NA), Month = c("dicembre", "marzo", "luglio",
"settembre", "novembre", "marzo", "maggio", "luglio", "ottobre",
"marzo", "giugno", "settembre", "novembre", "marzo", "giugno",
"settembre", "novembre", "marzo", "giugno", "settembre", "novembre",
"febbraio", "giugno", "agosto", "novembre", "marzo", "giugno",
"settembre", "novembre", "marzo", "maggio", "agosto", "ottobre",
"marzo", "maggio", "agosto", "novembre", "marzo", "giugno", "settembre",
"ottobre")), .Names = c("ID", "Date", "T", "ph", "EC", "O2",
"Cl", "SO4", "NO2", "NO3", "Fe", "Mn", "Month"), row.names = c(NA,
-41L), class = "data.frame")
I convert the Date column into a Date object using:
df$Date<-as.Date(df$Date, "%d.%m.%y")
Then I created the Month column from the Date one, typing:
df$Month <- months(as.Date(df$Date))
But the month names are in Italian and when I try to create another column of ordered factor
df$Month_factor<-factor(df$Month levels=month.name, ordered=T)
month names are not recognized and it just appears a column NA. So my question is: is it possible to change the language of the months when creating the new column? Else, is it possible for R to recognize the months as an ordered factor even if the month names are not in English?
I need the months as an ordered factor because I have to plot values like explained in this post.
It works for me... there is no italian as you overwrite it with df$Month <- months(as.Date(df$Date)) can you please give us your sessionInfo(), here is mine
sessionInfo()
R version 3.0.1 (2013-05-16)
Platform: x86_64-w64-mingw32/x64 (64-bit)
locale:
[1] LC_COLLATE=French_France.1252 LC_CTYPE=French_France.1252 LC_MONETARY=French_France.1252
[4] LC_NUMERIC=C LC_TIME=C
attached base packages:
[1] stats graphics grDevices datasets utils methods base
other attached packages:
[1] Rcpp_0.10.4 TeachingDemos_2.9 fastmatch_1.0-4 fasttime_1.0-0 data.table_1.8.9 bit64_0.9-2
[7] bit_1.1-10 vimcom_0.9-8
loaded via a namespace (and not attached):
[1] tools_3.0.1
try to set LC_TIME to C too with Sys.setlocale("LC_TIME", "C");

Resources