奇怪的y轴间隙

huangapple go评论121阅读模式
英文:

a weird gap on the y-axis

问题

在你提供的绘图中,垂直的蓝色波浪线代表y轴的起始点,并不是一个实际的数据点。这是由于在你的数据中,y轴的刻度从-0.1开始,而数据的最小值(fit.0.025quant中的最小值)比-0.1要小,因此出现了这个间隙。

要修复这个问题,你可以考虑将y轴的刻度从更接近数据范围的数值开始,例如从-0.05或-0.02开始,以确保y轴的起点与数据的最小值对齐,不会出现间隙。

这是一个修改后的绘图代码示例:

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

通过调整y轴刻度的起点,你可以让y轴与数据对齐,消除间隙。

英文:

My data is:

  1. structure(list(country = c(&quot;Austria&quot;, &quot;Austria&quot;, &quot;Austria&quot;, &quot;Austria&quot;,
  2. &quot;Austria&quot;, &quot;Austria&quot;, &quot;Austria&quot;, &quot;Austria&quot;, &quot;Austria&quot;, &quot;Austria&quot;,
  3. &quot;Austria&quot;, &quot;Austria&quot;, &quot;Austria&quot;, &quot;Austria&quot;, &quot;Austria&quot;, &quot;Austria&quot;,
  4. &quot;Austria&quot;, &quot;Austria&quot;, &quot;Austria&quot;, &quot;Austria&quot;, &quot;Austria&quot;, &quot;Austria&quot;,
  5. &quot;Austria&quot;, &quot;Austria&quot;, &quot;Austria&quot;, &quot;Austria&quot;, &quot;Austria&quot;, &quot;Austria&quot;,
  6. &quot;Austria&quot;, &quot;Austria&quot;, &quot;Austria&quot;, &quot;Austria&quot;, &quot;Austria&quot;, &quot;Austria&quot;,
  7. &quot;Austria&quot;, &quot;Austria&quot;, &quot;Austria&quot;, &quot;Austria&quot;, &quot;Austria&quot;, &quot;Austria&quot;,
  8. &quot;Austria&quot;, &quot;Austria&quot;, &quot;Austria&quot;, &quot;Austria&quot;, &quot;Austria&quot;, &quot;Austria&quot;,
  9. &quot;Austria&quot;, &quot;Austria&quot;, &quot;Austria&quot;, &quot;Austria&quot;, &quot;Austria&quot;, &quot;Austria&quot;,
  10. &quot;Austria&quot;, &quot;Austria&quot;, &quot;Austria&quot;, &quot;Austria&quot;, &quot;Austria&quot;, &quot;Austria&quot;,
  11. &quot;Austria&quot;, &quot;Austria&quot;, &quot;Austria&quot;, &quot;Austria&quot;, &quot;Austria&quot;, &quot;Austria&quot;,
  12. &quot;Austria&quot;, &quot;Austria&quot;, &quot;Austria&quot;, &quot;Austria&quot;, &quot;Austria&quot;, &quot;Austria&quot;,
  13. &quot;Austria&quot;, &quot;Austria&quot;, &quot;Austria&quot;, &quot;Austria&quot;, &quot;Austria&quot;, &quot;Austria&quot;,
  14. &quot;Austria&quot;, &quot;Austria&quot;, &quot;Austria&quot;, &quot;Austria&quot;, &quot;Austria&quot;, &quot;Austria&quot;,
  15. &quot;Austria&quot;, &quot;Austria&quot;, &quot;Austria&quot;, &quot;Austria&quot;, &quot;Austria&quot;, &quot;Austria&quot;,
  16. &quot;Austria&quot;, &quot;Austria&quot;, &quot;Austria&quot;, &quot;Austria&quot;, &quot;Austria&quot;, &quot;Austria&quot;,
  17. &quot;Austria&quot;, &quot;Austria&quot;, &quot;Austria&quot;, &quot;Austria&quot;, &quot;Austria&quot;), year = c(1800,
  18. 1801, 1802, 1803, 1804, 1805, 1806, 1807, 1808, 1809, 1810, 1811,
  19. 1812, 1813, 1814, 1815, 1816, 1817, 1818, 1819, 1820, 1821, 1822,
  20. 1823, 1824, 1825, 1826, 1827, 1828, 1829, 1830, 1831, 1832, 1833,
  21. 1834, 1835, 1836, 1837, 1838, 1839, 1840, 1841, 1842, 1843, 1844,
  22. 1845, 1846, 1847, 1848, 1849, 1850, 1851, 1852, 1853, 1854, 1855,
  23. 1856, 1857, 1858, 1859, 1860, 1861, 1862, 1863, 1864, 1865, 1866,
  24. 1867, 1868, 1869, 1870, 1871, 1872, 1873, 1874, 1875, 1876, 1877,
  25. 1878, 1879, 1880, 1881, 1882, 1883, 1884, 1885, 1886, 1887, 1888,
  26. 1889, 1890, 1891, 1892, 1893, 1894, 1895, 1896, 1897, 1898),
  27. polity2 = c(0, 0, 0, 0, 0, NA, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  28. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  29. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.2, 0.2, 0.2,
  30. 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.3, 0.3,
  31. 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3,
  32. 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3,
  33. 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3
  34. ), fit.0.025quant = c(-0.0228377190990235, -0.0228377190990235,
  35. -0.0228377190990235, -0.0228377190990235, -0.0228377190990235,
  36. -0.360467515197976, -0.0228377190990235, -0.0228377190990235,
  37. -0.0228377190990235, -0.0228377190990235, -0.0228377190990235,
  38. -0.0228377190990235, -0.0228377190990235, -0.0228377190990235,
  39. -0.0228377190990235, -0.0228377190990235, -0.0228377190990235,
  40. -0.0228377190990235, -0.0228377190990235, -0.0228377190990235,
  41. -0.0228377190990235, -0.0228377190990235, -0.0228377190990235,
  42. -0.0228377190990235, -0.0228377190990235, -0.0228377190990235,
  43. -0.0228377190990235, -0.0228377190990235, -0.0228377190990235,
  44. -0.0228377190990235, -0.0228377190990235, -0.0228377190990235,
  45. -0.0228377190990235, -0.0228377190990235, -0.0228377190990235,
  46. -0.0228377190990235, -0.0228377190990235, -0.0228377190990235,
  47. -0.0228377190990235, -0.0228377190990235, -0.0228377190990235,
  48. -0.0228377190990235, -0.0228377190990235, -0.0228377190990235,
  49. -0.0228377190990235, -0.0228377190990235, -0.0228377190990235,
  50. -0.0228377190990235, 0.16674339213966, 0.16674339213966,
  51. 0.16674339213966, 0.16674339213966, 0.16674339213966, 0.16674339213966,
  52. 0.16674339213966, 0.16674339213966, 0.16674339213966, 0.16674339213966,
  53. 0.16674339213966, 0.16674339213966, 0.16674339213966, 0.242005668666783,
  54. 0.242005668666783, 0.242005668666783, 0.242005668666783,
  55. 0.242005668666783, 0.242005668666783, 0.242005668666783,
  56. 0.242005668666783, 0.242005668666783, 0.242005668666783,
  57. 0.242005668666783, 0.242005668666783, 0.242005668666783,
  58. 0.242005668666783, 0.242005668666783, 0.242005668666783,
  59. 0.242005668666783, 0.242005668666783, 0.242005668666783,
  60. 0.242005668666783, 0.242005668666783, 0.242005668666783,
  61. 0.242005668666783, 0.242005668666783, 0.242005668666783,
  62. 0.242005668666783, 0.242005668666783, 0.242005668666783,
  63. 0.242005668666783, 0.242005668666783, 0.242005668666783,
  64. 0.242005668666783, 0.242005668666783, 0.242005668666783,
  65. 0.242005668666783, 0.242005668666783, 0.242005668666783,
  66. 0.242005668666783), fit.mean = c(3.14825130391602e-08, 2.60205063225599e-08,
  67. 2.59559368612124e-08, 2.5984794678656e-08, 2.83391441725141e-08,
  68. 2.52942314305284e-09, 2.83391440436086e-08, 2.59847906547622e-08,
  69. 2.5955396223965e-08, 2.59551516402278e-08, 2.5955149733964e-08,
  70. 2.5955149659684e-08, 2.59551496875235e-08, 2.59551496990232e-08,
  71. 2.59551496697443e-08, 2.59551496762558e-08, 2.59551497113832e-08,
  72. 2.59551496830708e-08, 2.59551496000976e-08, 2.59551496100631e-08,
  73. 2.59551498151052e-08, 2.59551496273394e-08, 2.59551496256576e-08,
  74. 2.59551496498655e-08, 2.59551496827474e-08, 2.59551496919159e-08,
  75. 2.59551497380275e-08, 2.59551498119649e-08, 2.5955149656671e-08,
  76. 2.5955149726764e-08, 2.59551496311154e-08, 2.59551496629923e-08,
  77. 2.59551496748244e-08, 2.59551496503481e-08, 2.59551496599462e-08,
  78. 2.59551498719153e-08, 2.59551516171938e-08, 2.59551725579831e-08,
  79. 2.59554430639403e-08, 2.59589965355424e-08, 2.60069290863896e-08,
  80. 2.6671828589446e-08, 3.61804464990433e-08, 1.76724037684815e-07,
  81. 2.32931085826976e-06, 3.65813076312562e-05, 0.000604208916987207,
  82. 0.0104234834558744, 0.189576586574593, 0.199395843381036,
  83. 0.199963470607777, 0.199997722599642, 0.199999875188186,
  84. 0.200000015755743, 0.200000025596808, 0.200000031016018,
  85. 0.200000101335746, 0.200001177632703, 0.200018303630534,
  86. 0.200302117262285, 0.205211743139791, 0.294788313124213,
  87. 0.299697934741236, 0.299981748281017, 0.299998874277316,
  88. 0.299999950570704, 0.3000000208425, 0.300000025596811, 0.300000025929259,
  89. 0.300000025953225, 0.300000025955003, 0.30000002595514, 0.300000025955146,
  90. 0.300000025955149, 0.30000002595515, 0.300000025955149, 0.300000025955149,
  91. 0.300000025955151, 0.300000025955148, 0.30000002595515, 0.300000025955149,
  92. 0.300000025955151, 0.300000025955148, 0.300000025955146,
  93. 0.300000025955149, 0.300000025955151, 0.30000002595515, 0.300000025955151,
  94. 0.30000002595515, 0.300000025955152, 0.300000025955152, 0.300000025955148,
  95. 0.300000025955146, 0.300000025955149, 0.300000025955148,
  96. 0.300000025955149, 0.300000025955148, 0.300000025955149,
  97. 0.300000025955146), fit.0.975quant = c(0.0398209805304305,
  98. 0.0398209805304305, 0.0398209805304305, 0.0398209805304305,
  99. 0.0398209805304305, 0.895588490397268, 0.0398209805304305,
  100. 0.0398209805304305, 0.0398209805304305, 0.0398209805304305,
  101. 0.0398209805304305, 0.0398209805304305, 0.0398209805304305,
  102. 0.0398209805304305, 0.0398209805304305, 0.0398209805304305,
  103. 0.0398209805304305, 0.0398209805304305, 0.0398209805304305,
  104. 0.0398209805304305, 0.0398209805304305, 0.0398209805304305,
  105. 0.0398209805304305, 0.0398209805304305, 0.0398209805304305,
  106. 0.0398209805304305, 0.0398209805304305, 0.0398209805304305,
  107. 0.0398209805304305, 0.0398209805304305, 0.0398209805304305,
  108. 0.0398209805304305, 0.0398209805304305, 0.0398209805304305,
  109. 0.0398209805304305, 0.0398209805304305, 0.0398209805304305,
  110. 0.0398209805304305, 0.0398209805304305, 0.0398209805304305,
  111. 0.0398209805304305, 0.0398209805304305, 0.0398209805304305,
  112. 0.0398209805304305, 0.0398209805304305, 0.0398209805304305,
  113. 0.0398209805304305, 0.0398209805304305, 0.230715038583087,
  114. 0.230715038583087, 0.230715038583087, 0.230715038583087,
  115. 0.230715038583087, 0.230715038583087, 0.230715038583087,
  116. 0.230715038583087, 0.230715038583087, 0.230715038583087,
  117. 0.230715038583087, 0.230715038583087, 0.230715038583087,
  118. 0.3453320450646, 0.3453320450646, 0.3453320450646, 0.3453320450646,
  119. 0.3453320450646, 0.3453320450646, 0.3453320450646, 0.3453320450646,
  120. 0.3453320450646, 0.3453320450646, 0.3453320450646, 0.3453320450646,
  121. 0.3453320450646, 0.3453320450646, 0.3453320450646, 0.3453320450646,
  122. 0.3453320450646, 0.3453320450646, 0.3453320450646, 0.3453320450646,
  123. 0.3453320450646, 0.3453320450646, 0.3453320450646, 0.3453320450646,
  124. 0.3453320450646, 0.3453320450646, 0.3453320450646, 0.3453320450646,
  125. 0.3453320450646, 0.3453320450646, 0.3453320450646, 0.3453320450646,
  126. 0.3453320450646, 0.3453320450646, 0.3453320450646, 0.3453320450646,
  127. 0.3453320450646, 0.3453320450646), mean = c(0.000219534613172309,
  128. 0.000219534613172309, 0.000219534613172309, 0.000219534613172309,
  129. 0.000219534613172309, 0.179349478235255, 0.000219534613172309,
  130. 0.000219534613172309, 0.000219534613172309, 0.000219534613172309,
  131. 0.000219534613172309, 0.000219534613172309, 0.000219534613172309,
  132. 0.000219534613172309, 0.000219534613172309, 0.000219534613172309,
  133. 0.000219534613172309, 0.000219534613172309, 0.000219534613172309,
  134. 0.000219534613172309, 0.000219534613172309, 0.000219534613172309,
  135. 0.000219534613172309, 0.000219534613172309, 0.000219534613172309,
  136. 0.000219534613172309, 0.000219534613172309, 0.000219534613172309,
  137. 0.000219534613172309, 0.000219534613172309, 0.000219534613172309,
  138. 0.000219534613172309, 0.000219534613172309, 0.000219534613172309,
  139. 0.000219534613172309, 0.000219534613172309, 0.000219534613172309,
  140. 0.000219534613172309, 0.000219534613172309, 0.000219534613172309,
  141. 0.000219534613172309, 0.000219534613172309, 0.000219534613172309,
  142. 0.000219534613172309, 0.000219534613172309, 0.000219534613172309,
  143. 0.000219534613172309, 0.000219534613172309, 0.200204245756936,
  144. 0.200204245756936, 0.200204245756936, 0.200204245756936,
  145. 0.200204245756936, 0.200204245756936, 0.200204245756936,
  146. 0.200204245756936, 0.200204245756936, 0.200204245756936,
  147. 0.200204245756936, 0.200204245756936, 0.200204245756936,
  148. 0.299996880329696, 0.299996880329696, 0.299996880329696,
  149. 0.299996880329696, 0.299996880329696, 0.299996880329696,
  150. 0.299996880329696, 0.299996880329696, 0.299996880329696,
  151. 0.299996880329696, 0.299996880329696, 0.299996880329696,
  152. 0.299996880329696, 0.299996880329696, 0.299996880329696,
  153. 0.299996880329696, 0.299996880329696, 0.299996880329696,
  154. 0.299996880329696, 0.299996880329696, 0.299996880329696,
  155. 0.299996880329696, 0.299996880329696, 0.299996880329696,
  156. 0.299996880329696, 0.299996880329696, 0.299996880329696,
  157. 0.299996880329696, 0.299996880329696, 0.299996880329696,
  158. 0.299996880329696, 0.299996880329696, 0.299996880329696,
  159. 0.299996880329696, 0.299996880329696, 0.299996880329696,
  160. 0.299996880329696, 0.299996880329696)), class = c(&quot;grouped_df&quot;,
  161. &quot;tbl_df&quot;, &quot;tbl&quot;, &quot;data.frame&quot;), row.names = c(NA, -99L), groups = structure(list(
  162. polity2 = c(0, 0.2, 0.3, NA), .rows = structure(list(c(1L,
  163. 2L, 3L, 4L, 5L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L,
  164. 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L,
  165. 28L, 29L, 30L, 31L, 32L, 33L, 34L, 35L, 36L, 37L, 38L, 39L,
  166. 40L, 41L, 42L, 43L, 44L, 45L, 46L, 47L, 48L), 49:61, 62:99,
  167. 6L), ptype = integer(0), class = c(&quot;vctrs_list_of&quot;, &quot;vctrs_vctr&quot;,
  168. &quot;list&quot;))), class = c(&quot;tbl_df&quot;, &quot;tbl&quot;, &quot;data.frame&quot;), row.names = c(NA,
  169. -4L), .drop = TRUE))

>

And I want to produce plot as:

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

Which produces plot as:

奇怪的y轴间隙

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?

答案1

得分: 2

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:

问题在于fit.0.025quant的范围延伸到了-.36...,这反映在y轴的scale的限制中。然而,这仅发生在一个观测中,其中polity2的值缺失,因此不会被考虑在geom_pointrange中。修复的一个选项是删除具有缺失值的polity2的观测:

  1. library(ggplot2)
  2. library(dplyr)
  3. df %>%
  4. filter(!is.na(polity2)) %>%
  5. ggplot(
  6. aes(x = polity2, y = mean)
  7. ) +
  8. geom_pointrange(aes(ymin = fit.0.025quant, ymax = fit.0.975quant),
  9. color = "red"
  10. ) +
  11. scale_y_continuous(breaks = seq(-0.1, 1.2, 0.05)) +
  12. scale_x_continuous(breaks = seq(0, 1, 0.05))

奇怪的y轴间隙

  1. <details>
  2. <summary>英文:</summary>
  3. 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))

  1. [![enter image description here][1]][1]
  2. [1]: https://i.stack.imgur.com/DFlEG.png
  3. </details>
  4. # 答案2
  5. **得分**: 1
  6. 我们需要将 `limits` 添加到 `breaks` 中:
  7. ```R
  8. ggplot(df, aes(x = polity2, y = mean)) +
  9. geom_pointrange(aes(ymin = fit.0.025quant, ymax = fit.0.975quant), color = "red") +
  10. scale_x_continuous(limits = c(-0.1, 0.5), breaks = seq(-0.1, 0.5, by = 0.05)) +
  11. scale_y_continuous(limits = c(-0.1, 1.2), breaks = seq(-0.1, 1.2, by = 0.05))

奇怪的y轴间隙

英文:

We have to add limits to breaks:

  1. ggplot(df, aes(x = polity2, y = mean)) +
  2. geom_pointrange(aes(ymin = fit.0.025quant, ymax = fit.0.975quant), color = &quot;red&quot;) +
  3. scale_x_continuous(limits = c(-0.1, 0.5), breaks = seq(-0.1, 0.5, by = 0.05)) +
  4. scale_y_continuous(limits = c(-0.1, 1.2), breaks = seq(-0.1, 1.2, by = 0.05))

奇怪的y轴间隙

huangapple
  • 本文由 发表于 2023年4月13日 19:04:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76004676.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定