奇怪的y轴间隙

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

a weird gap on the y-axis

问题

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

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

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

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.02, 1.2, 0.05)) + 
  scale_x_continuous(breaks = seq(0, 1, 0.05))

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

英文:

My data is:

structure(list(country = c(&quot;Austria&quot;, &quot;Austria&quot;, &quot;Austria&quot;, &quot;Austria&quot;, 
&quot;Austria&quot;, &quot;Austria&quot;, &quot;Austria&quot;, &quot;Austria&quot;, &quot;Austria&quot;, &quot;Austria&quot;, 
&quot;Austria&quot;, &quot;Austria&quot;, &quot;Austria&quot;, &quot;Austria&quot;, &quot;Austria&quot;, &quot;Austria&quot;, 
&quot;Austria&quot;, &quot;Austria&quot;, &quot;Austria&quot;, &quot;Austria&quot;, &quot;Austria&quot;, &quot;Austria&quot;, 
&quot;Austria&quot;, &quot;Austria&quot;, &quot;Austria&quot;, &quot;Austria&quot;, &quot;Austria&quot;, &quot;Austria&quot;, 
&quot;Austria&quot;, &quot;Austria&quot;, &quot;Austria&quot;, &quot;Austria&quot;, &quot;Austria&quot;, &quot;Austria&quot;, 
&quot;Austria&quot;, &quot;Austria&quot;, &quot;Austria&quot;, &quot;Austria&quot;, &quot;Austria&quot;, &quot;Austria&quot;, 
&quot;Austria&quot;, &quot;Austria&quot;, &quot;Austria&quot;, &quot;Austria&quot;, &quot;Austria&quot;, &quot;Austria&quot;, 
&quot;Austria&quot;, &quot;Austria&quot;, &quot;Austria&quot;, &quot;Austria&quot;, &quot;Austria&quot;, &quot;Austria&quot;, 
&quot;Austria&quot;, &quot;Austria&quot;, &quot;Austria&quot;, &quot;Austria&quot;, &quot;Austria&quot;, &quot;Austria&quot;, 
&quot;Austria&quot;, &quot;Austria&quot;, &quot;Austria&quot;, &quot;Austria&quot;, &quot;Austria&quot;, &quot;Austria&quot;, 
&quot;Austria&quot;, &quot;Austria&quot;, &quot;Austria&quot;, &quot;Austria&quot;, &quot;Austria&quot;, &quot;Austria&quot;, 
&quot;Austria&quot;, &quot;Austria&quot;, &quot;Austria&quot;, &quot;Austria&quot;, &quot;Austria&quot;, &quot;Austria&quot;, 
&quot;Austria&quot;, &quot;Austria&quot;, &quot;Austria&quot;, &quot;Austria&quot;, &quot;Austria&quot;, &quot;Austria&quot;, 
&quot;Austria&quot;, &quot;Austria&quot;, &quot;Austria&quot;, &quot;Austria&quot;, &quot;Austria&quot;, &quot;Austria&quot;, 
&quot;Austria&quot;, &quot;Austria&quot;, &quot;Austria&quot;, &quot;Austria&quot;, &quot;Austria&quot;, &quot;Austria&quot;, 
&quot;Austria&quot;, &quot;Austria&quot;, &quot;Austria&quot;, &quot;Austria&quot;, &quot;Austria&quot;), 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(&quot;grouped_df&quot;, 
&quot;tbl_df&quot;, &quot;tbl&quot;, &quot;data.frame&quot;), 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(&quot;vctrs_list_of&quot;, &quot;vctrs_vctr&quot;, 
&quot;list&quot;))), class = c(&quot;tbl_df&quot;, &quot;tbl&quot;, &quot;data.frame&quot;), row.names = c(NA, 
-4L), .drop = TRUE))

>

And I want to produce plot as:

plot &lt;- ggplot(df, 
aes(x = polity2, y = mean)) + geom_pointrange(aes(ymin = fit.0.025quant, ymax = fit.0.975quant),
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的观测:

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))

奇怪的y轴间隙


<details>
<summary>英文:</summary>
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][1]][1]
[1]: https://i.stack.imgur.com/DFlEG.png
</details>
# 答案2
**得分**: 1
我们需要将 `limits` 添加到 `breaks` 中:
```R
ggplot(df, aes(x = polity2, y = mean)) + 
geom_pointrange(aes(ymin = fit.0.025quant, ymax = fit.0.975quant), color = "red") + 
scale_x_continuous(limits = c(-0.1, 0.5), breaks = seq(-0.1, 0.5, by = 0.05)) + 
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:

ggplot(df, aes(x = polity2, y = mean)) + 
geom_pointrange(aes(ymin = fit.0.025quant, ymax = fit.0.975quant), color = &quot;red&quot;) + 
scale_x_continuous(limits = c(-0.1, 0.5), breaks = seq(-0.1, 0.5, by = 0.05)) + 
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:

确定