Home > Software design >  Issue with fitted line on ggplot
Issue with fitted line on ggplot

Time:03-03

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) enter image description here

CodePudding user response:

There are two things causing this.

  1. 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.
  2. 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