Search code examples
rggplot2yaxis

a weird gap on the y-axis


My data is:

structure(list(country = c("Austria", "Austria", "Austria", "Austria", 
                       "Austria", "Austria", "Austria", "Austria", "Austria", "Austria", 
                       "Austria", "Austria", "Austria", "Austria", "Austria", "Austria", 
                       "Austria", "Austria", "Austria", "Austria", "Austria", "Austria", 
                       "Austria", "Austria", "Austria", "Austria", "Austria", "Austria", 
                       "Austria", "Austria", "Austria", "Austria", "Austria", "Austria", 
                       "Austria", "Austria", "Austria", "Austria", "Austria", "Austria", 
                       "Austria", "Austria", "Austria", "Austria", "Austria", "Austria", 
                       "Austria", "Austria", "Austria", "Austria", "Austria", "Austria", 
                       "Austria", "Austria", "Austria", "Austria", "Austria", "Austria", 
                       "Austria", "Austria", "Austria", "Austria", "Austria", "Austria", 
                       "Austria", "Austria", "Austria", "Austria", "Austria", "Austria", 
                       "Austria", "Austria", "Austria", "Austria", "Austria", "Austria", 
                       "Austria", "Austria", "Austria", "Austria", "Austria", "Austria", 
                       "Austria", "Austria", "Austria", "Austria", "Austria", "Austria", 
                       "Austria", "Austria", "Austria", "Austria", "Austria", "Austria", 
                       "Austria", "Austria", "Austria", "Austria", "Austria"), year = c(1800, 
                                                                                        1801, 1802, 1803, 1804, 1805, 1806, 1807, 1808, 1809, 1810, 1811, 
                                                                                        1812, 1813, 1814, 1815, 1816, 1817, 1818, 1819, 1820, 1821, 1822, 
                                                                                        1823, 1824, 1825, 1826, 1827, 1828, 1829, 1830, 1831, 1832, 1833, 
                                                                                        1834, 1835, 1836, 1837, 1838, 1839, 1840, 1841, 1842, 1843, 1844, 
                                                                                        1845, 1846, 1847, 1848, 1849, 1850, 1851, 1852, 1853, 1854, 1855, 
                                                                                        1856, 1857, 1858, 1859, 1860, 1861, 1862, 1863, 1864, 1865, 1866, 
                                                                                        1867, 1868, 1869, 1870, 1871, 1872, 1873, 1874, 1875, 1876, 1877, 
                                                                                        1878, 1879, 1880, 1881, 1882, 1883, 1884, 1885, 1886, 1887, 1888, 
                                                                                        1889, 1890, 1891, 1892, 1893, 1894, 1895, 1896, 1897, 1898), 
           polity2 = c(0, 0, 0, 0, 0, NA, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
                       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
                       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.2, 0.2, 0.2, 
                       0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.3, 0.3, 
                       0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 
                       0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 
                       0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3
           ), fit.0.025quant = c(-0.0228377190990235, -0.0228377190990235, 
                                 -0.0228377190990235, -0.0228377190990235, -0.0228377190990235, 
                                 -0.360467515197976, -0.0228377190990235, -0.0228377190990235, 
                                 -0.0228377190990235, -0.0228377190990235, -0.0228377190990235, 
                                 -0.0228377190990235, -0.0228377190990235, -0.0228377190990235, 
                                 -0.0228377190990235, -0.0228377190990235, -0.0228377190990235, 
                                 -0.0228377190990235, -0.0228377190990235, -0.0228377190990235, 
                                 -0.0228377190990235, -0.0228377190990235, -0.0228377190990235, 
                                 -0.0228377190990235, -0.0228377190990235, -0.0228377190990235, 
                                 -0.0228377190990235, -0.0228377190990235, -0.0228377190990235, 
                                 -0.0228377190990235, -0.0228377190990235, -0.0228377190990235, 
                                 -0.0228377190990235, -0.0228377190990235, -0.0228377190990235, 
                                 -0.0228377190990235, -0.0228377190990235, -0.0228377190990235, 
                                 -0.0228377190990235, -0.0228377190990235, -0.0228377190990235, 
                                 -0.0228377190990235, -0.0228377190990235, -0.0228377190990235, 
                                 -0.0228377190990235, -0.0228377190990235, -0.0228377190990235, 
                                 -0.0228377190990235, 0.16674339213966, 0.16674339213966, 
                                 0.16674339213966, 0.16674339213966, 0.16674339213966, 0.16674339213966, 
                                 0.16674339213966, 0.16674339213966, 0.16674339213966, 0.16674339213966, 
                                 0.16674339213966, 0.16674339213966, 0.16674339213966, 0.242005668666783, 
                                 0.242005668666783, 0.242005668666783, 0.242005668666783, 
                                 0.242005668666783, 0.242005668666783, 0.242005668666783, 
                                 0.242005668666783, 0.242005668666783, 0.242005668666783, 
                                 0.242005668666783, 0.242005668666783, 0.242005668666783, 
                                 0.242005668666783, 0.242005668666783, 0.242005668666783, 
                                 0.242005668666783, 0.242005668666783, 0.242005668666783, 
                                 0.242005668666783, 0.242005668666783, 0.242005668666783, 
                                 0.242005668666783, 0.242005668666783, 0.242005668666783, 
                                 0.242005668666783, 0.242005668666783, 0.242005668666783, 
                                 0.242005668666783, 0.242005668666783, 0.242005668666783, 
                                 0.242005668666783, 0.242005668666783, 0.242005668666783, 
                                 0.242005668666783, 0.242005668666783, 0.242005668666783, 
                                 0.242005668666783), fit.mean = c(3.14825130391602e-08, 2.60205063225599e-08, 
                                                                  2.59559368612124e-08, 2.5984794678656e-08, 2.83391441725141e-08, 
                                                                  2.52942314305284e-09, 2.83391440436086e-08, 2.59847906547622e-08, 
                                                                  2.5955396223965e-08, 2.59551516402278e-08, 2.5955149733964e-08, 
                                                                  2.5955149659684e-08, 2.59551496875235e-08, 2.59551496990232e-08, 
                                                                  2.59551496697443e-08, 2.59551496762558e-08, 2.59551497113832e-08, 
                                                                  2.59551496830708e-08, 2.59551496000976e-08, 2.59551496100631e-08, 
                                                                  2.59551498151052e-08, 2.59551496273394e-08, 2.59551496256576e-08, 
                                                                  2.59551496498655e-08, 2.59551496827474e-08, 2.59551496919159e-08, 
                                                                  2.59551497380275e-08, 2.59551498119649e-08, 2.5955149656671e-08, 
                                                                  2.5955149726764e-08, 2.59551496311154e-08, 2.59551496629923e-08, 
                                                                  2.59551496748244e-08, 2.59551496503481e-08, 2.59551496599462e-08, 
                                                                  2.59551498719153e-08, 2.59551516171938e-08, 2.59551725579831e-08, 
                                                                  2.59554430639403e-08, 2.59589965355424e-08, 2.60069290863896e-08, 
                                                                  2.6671828589446e-08, 3.61804464990433e-08, 1.76724037684815e-07, 
                                                                  2.32931085826976e-06, 3.65813076312562e-05, 0.000604208916987207, 
                                                                  0.0104234834558744, 0.189576586574593, 0.199395843381036, 
                                                                  0.199963470607777, 0.199997722599642, 0.199999875188186, 
                                                                  0.200000015755743, 0.200000025596808, 0.200000031016018, 
                                                                  0.200000101335746, 0.200001177632703, 0.200018303630534, 
                                                                  0.200302117262285, 0.205211743139791, 0.294788313124213, 
                                                                  0.299697934741236, 0.299981748281017, 0.299998874277316, 
                                                                  0.299999950570704, 0.3000000208425, 0.300000025596811, 0.300000025929259, 
                                                                  0.300000025953225, 0.300000025955003, 0.30000002595514, 0.300000025955146, 
                                                                  0.300000025955149, 0.30000002595515, 0.300000025955149, 0.300000025955149, 
                                                                  0.300000025955151, 0.300000025955148, 0.30000002595515, 0.300000025955149, 
                                                                  0.300000025955151, 0.300000025955148, 0.300000025955146, 
                                                                  0.300000025955149, 0.300000025955151, 0.30000002595515, 0.300000025955151, 
                                                                  0.30000002595515, 0.300000025955152, 0.300000025955152, 0.300000025955148, 
                                                                  0.300000025955146, 0.300000025955149, 0.300000025955148, 
                                                                  0.300000025955149, 0.300000025955148, 0.300000025955149, 
                                                                  0.300000025955146), fit.0.975quant = c(0.0398209805304305, 
                                                                                                         0.0398209805304305, 0.0398209805304305, 0.0398209805304305, 
                                                                                                         0.0398209805304305, 0.895588490397268, 0.0398209805304305, 
                                                                                                         0.0398209805304305, 0.0398209805304305, 0.0398209805304305, 
                                                                                                         0.0398209805304305, 0.0398209805304305, 0.0398209805304305, 
                                                                                                         0.0398209805304305, 0.0398209805304305, 0.0398209805304305, 
                                                                                                         0.0398209805304305, 0.0398209805304305, 0.0398209805304305, 
                                                                                                         0.0398209805304305, 0.0398209805304305, 0.0398209805304305, 
                                                                                                         0.0398209805304305, 0.0398209805304305, 0.0398209805304305, 
                                                                                                         0.0398209805304305, 0.0398209805304305, 0.0398209805304305, 
                                                                                                         0.0398209805304305, 0.0398209805304305, 0.0398209805304305, 
                                                                                                         0.0398209805304305, 0.0398209805304305, 0.0398209805304305, 
                                                                                                         0.0398209805304305, 0.0398209805304305, 0.0398209805304305, 
                                                                                                         0.0398209805304305, 0.0398209805304305, 0.0398209805304305, 
                                                                                                         0.0398209805304305, 0.0398209805304305, 0.0398209805304305, 
                                                                                                         0.0398209805304305, 0.0398209805304305, 0.0398209805304305, 
                                                                                                         0.0398209805304305, 0.0398209805304305, 0.230715038583087, 
                                                                                                         0.230715038583087, 0.230715038583087, 0.230715038583087, 
                                                                                                         0.230715038583087, 0.230715038583087, 0.230715038583087, 
                                                                                                         0.230715038583087, 0.230715038583087, 0.230715038583087, 
                                                                                                         0.230715038583087, 0.230715038583087, 0.230715038583087, 
                                                                                                         0.3453320450646, 0.3453320450646, 0.3453320450646, 0.3453320450646, 
                                                                                                         0.3453320450646, 0.3453320450646, 0.3453320450646, 0.3453320450646, 
                                                                                                         0.3453320450646, 0.3453320450646, 0.3453320450646, 0.3453320450646, 
                                                                                                         0.3453320450646, 0.3453320450646, 0.3453320450646, 0.3453320450646, 
                                                                                                         0.3453320450646, 0.3453320450646, 0.3453320450646, 0.3453320450646, 
                                                                                                         0.3453320450646, 0.3453320450646, 0.3453320450646, 0.3453320450646, 
                                                                                                         0.3453320450646, 0.3453320450646, 0.3453320450646, 0.3453320450646, 
                                                                                                         0.3453320450646, 0.3453320450646, 0.3453320450646, 0.3453320450646, 
                                                                                                         0.3453320450646, 0.3453320450646, 0.3453320450646, 0.3453320450646, 
                                                                                                         0.3453320450646, 0.3453320450646), mean = c(0.000219534613172309, 
                                                                                                                                                     0.000219534613172309, 0.000219534613172309, 0.000219534613172309, 
                                                                                                                                                     0.000219534613172309, 0.179349478235255, 0.000219534613172309, 
                                                                                                                                                     0.000219534613172309, 0.000219534613172309, 0.000219534613172309, 
                                                                                                                                                     0.000219534613172309, 0.000219534613172309, 0.000219534613172309, 
                                                                                                                                                     0.000219534613172309, 0.000219534613172309, 0.000219534613172309, 
                                                                                                                                                     0.000219534613172309, 0.000219534613172309, 0.000219534613172309, 
                                                                                                                                                     0.000219534613172309, 0.000219534613172309, 0.000219534613172309, 
                                                                                                                                                     0.000219534613172309, 0.000219534613172309, 0.000219534613172309, 
                                                                                                                                                     0.000219534613172309, 0.000219534613172309, 0.000219534613172309, 
                                                                                                                                                     0.000219534613172309, 0.000219534613172309, 0.000219534613172309, 
                                                                                                                                                     0.000219534613172309, 0.000219534613172309, 0.000219534613172309, 
                                                                                                                                                     0.000219534613172309, 0.000219534613172309, 0.000219534613172309, 
                                                                                                                                                     0.000219534613172309, 0.000219534613172309, 0.000219534613172309, 
                                                                                                                                                     0.000219534613172309, 0.000219534613172309, 0.000219534613172309, 
                                                                                                                                                     0.000219534613172309, 0.000219534613172309, 0.000219534613172309, 
                                                                                                                                                     0.000219534613172309, 0.000219534613172309, 0.200204245756936, 
                                                                                                                                                     0.200204245756936, 0.200204245756936, 0.200204245756936, 
                                                                                                                                                     0.200204245756936, 0.200204245756936, 0.200204245756936, 
                                                                                                                                                     0.200204245756936, 0.200204245756936, 0.200204245756936, 
                                                                                                                                                     0.200204245756936, 0.200204245756936, 0.200204245756936, 
                                                                                                                                                     0.299996880329696, 0.299996880329696, 0.299996880329696, 
                                                                                                                                                     0.299996880329696, 0.299996880329696, 0.299996880329696, 
                                                                                                                                                     0.299996880329696, 0.299996880329696, 0.299996880329696, 
                                                                                                                                                     0.299996880329696, 0.299996880329696, 0.299996880329696, 
                                                                                                                                                     0.299996880329696, 0.299996880329696, 0.299996880329696, 
                                                                                                                                                     0.299996880329696, 0.299996880329696, 0.299996880329696, 
                                                                                                                                                     0.299996880329696, 0.299996880329696, 0.299996880329696, 
                                                                                                                                                     0.299996880329696, 0.299996880329696, 0.299996880329696, 
                                                                                                                                                     0.299996880329696, 0.299996880329696, 0.299996880329696, 
                                                                                                                                                     0.299996880329696, 0.299996880329696, 0.299996880329696, 
                                                                                                                                                     0.299996880329696, 0.299996880329696, 0.299996880329696, 
                                                                                                                                                     0.299996880329696, 0.299996880329696, 0.299996880329696, 
                                                                                                                                                     0.299996880329696, 0.299996880329696)), class = c("grouped_df", 
                                                                                                                                                                                                       "tbl_df", "tbl", "data.frame"), row.names = c(NA, -99L), groups = structure(list(
                                                                                                                                                                                                         polity2 = c(0, 0.2, 0.3, NA), .rows = structure(list(c(1L, 
                                                                                                                                                                                                                                                                2L, 3L, 4L, 5L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 
                                                                                                                                                                                                                                                                16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 
                                                                                                                                                                                                                                                                28L, 29L, 30L, 31L, 32L, 33L, 34L, 35L, 36L, 37L, 38L, 39L, 
                                                                                                                                                                                                                                                                40L, 41L, 42L, 43L, 44L, 45L, 46L, 47L, 48L), 49:61, 62:99, 
                                                                                                                                                                                                                                                              6L), ptype = integer(0), class = c("vctrs_list_of", "vctrs_vctr", 
                                                                                                                                                                                                                                                                                                 "list"))), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, 
                                                                                                                                                                                                                                                                                                                                                                    -4L), .drop = TRUE))

And I want to produce plot as:

plot <- ggplot(df, 
                                          aes(x = polity2, y = mean)) + geom_pointrange(aes(ymin = fit.0.025quant, ymax = fit.0.975quant),
              color = "red") + scale_y_continuous(breaks=seq(-0.1,1.2,0.05)) + scale_x_continuous(breaks=seq(0,1,0.05)) 

Which produces plot as:

enter image description here

Why is there a weird gap on the y-axis (marked with a wave blue line)? And how it can be removed?

It is supposed to show y-axis and x-axis to be coming from the same origin, as any typical graph.

Could you guys please help me out with this?


Solution

  • The issue is that the range of fit.0.025quant extends to -.36... which is reflected in the limits of the y scale. However, this happens for only one obs for which the polity2 value is missing and hence it will not be taken into account for the geom_pointrange. One option to fix that would be to drop the observation(s) with a missing value for polity2:

    library(ggplot2)
    library(dplyr)
    
    df |>
      filter(!is.na(polity2)) |>
      ggplot(
        aes(x = polity2, y = mean)
      ) +
      geom_pointrange(aes(ymin = fit.0.025quant, ymax = fit.0.975quant),
        color = "red"
      ) +
      scale_y_continuous(breaks = seq(-0.1, 1.2, 0.05)) +
      scale_x_continuous(breaks = seq(0, 1, 0.05))
    

    enter image description here