为什么GGPLOT突然将Y轴数的甲酸变为指数?

发布于 2025-02-13 04:17:09 字数 2063 浏览 0 评论 0原文

I found various posts solving the issue to change the y-axis from exponential back to a noraml decimal:

避免科学符号x轴ggplot

我如何改变数字格式在带有ggplot的轴上?

,但我只想知道为什么y轴值格式从0.4到4.000000e-01,当我更改我的时代码来自 scale_y_continous(breaks = seq(-0.2,0.4,0.05))) to scale> scale_y_continous(breaks = seq(-0.3,0.4,0.05)) ????我只将-0.2更改为-0.3,我不认为它应该更改y轴值的格式。

代码复制问题。

ISD2 = data.frame(Distance = c(50,150,250,350,450,550,650,750,850,950,50,150,250,350,450,550,650,750,850,950), 
                 Related = c(-0.00036,-0.02968,-0.02685,-0.05379,0.01345,-0.03163,-0.00826,-0.12643,-0.05609,0.10906,0.03957,-0.02621,-0.01709,-0.03086,-0.06208,-0.00839,-0.03827,0.01823,-0.01130,-0.02848),
                 R_LCI = c(-0.08,-0.06,-0.06,-0.05,-0.05,-0.05,-0.07,-0.10,-0.14,-0.14,-0.28,-0.06,-0.05,-0.05,-0.05,-0.05,-0.06,-0.09,-0.12,-0.23),
                 R_UCI = c(0.04,0.01,0.01,0.00,0.01,0.01,0.03,0.09,0.16,0.40,0.03,0.01,0.00,0.00,0.00,0.00,0.02,0.05,0.08,0.23),
                 Sex = c('M','M','M','M','M','M','M','M','M','M','F','F','F','F','F','F','F','F','F','F'))
ggplot(aes(x=Distance, y = Related, color=Sex),data = ISD) + 
  stat_smooth(method = "lm", se = F) +
  geom_point(position=position_dodge(width=30), size = 4) +
  geom_point(aes(x=0, y=0.37), color = "#DC3220",size=4) + 
  geom_point(aes(x=5, y=0.28), color = "#005AB5",size=4) + 
  geom_errorbar(aes(ymin=R_LCI, ymax=R_UCI), position=position_dodge(width=30), width=20) +
  scale_x_continuous("Distance (m)", breaks = seq(0,1000,100), limit = c(0,1000), expand = c(0,20)) +
  scale_y_continuous("Relatedness", breaks = seq(-0.3,0.35,0.05)) +
  annotate("text", label = "Adults Only", x =800, y =.35, size = 6) +
  scale_color_manual(values = c("#DC3220","#005AB5"))```

I found various posts solving the issue to change the y-axis from exponential back to a noraml decimal:

avoid scientific notation x axis ggplot

How do I change the formatting of numbers on an axis with ggplot?

But I just want to know are WHY the y-axis value format goes from 0.4 to 4.000000e-01 when I change my code from
scale_y_continuous(breaks = seq(-0.2,0.4,0.05)) to scale_y_continuous(breaks = seq(-0.3,0.4,0.05))??? I only changed -0.2 to -0.3, I wouldn't think it should change the format of the y-axis values.

Code to reproduce the issue.

ISD2 = data.frame(Distance = c(50,150,250,350,450,550,650,750,850,950,50,150,250,350,450,550,650,750,850,950), 
                 Related = c(-0.00036,-0.02968,-0.02685,-0.05379,0.01345,-0.03163,-0.00826,-0.12643,-0.05609,0.10906,0.03957,-0.02621,-0.01709,-0.03086,-0.06208,-0.00839,-0.03827,0.01823,-0.01130,-0.02848),
                 R_LCI = c(-0.08,-0.06,-0.06,-0.05,-0.05,-0.05,-0.07,-0.10,-0.14,-0.14,-0.28,-0.06,-0.05,-0.05,-0.05,-0.05,-0.06,-0.09,-0.12,-0.23),
                 R_UCI = c(0.04,0.01,0.01,0.00,0.01,0.01,0.03,0.09,0.16,0.40,0.03,0.01,0.00,0.00,0.00,0.00,0.02,0.05,0.08,0.23),
                 Sex = c('M','M','M','M','M','M','M','M','M','M','F','F','F','F','F','F','F','F','F','F'))
ggplot(aes(x=Distance, y = Related, color=Sex),data = ISD) + 
  stat_smooth(method = "lm", se = F) +
  geom_point(position=position_dodge(width=30), size = 4) +
  geom_point(aes(x=0, y=0.37), color = "#DC3220",size=4) + 
  geom_point(aes(x=5, y=0.28), color = "#005AB5",size=4) + 
  geom_errorbar(aes(ymin=R_LCI, ymax=R_UCI), position=position_dodge(width=30), width=20) +
  scale_x_continuous("Distance (m)", breaks = seq(0,1000,100), limit = c(0,1000), expand = c(0,20)) +
  scale_y_continuous("Relatedness", breaks = seq(-0.3,0.35,0.05)) +
  annotate("text", label = "Adults Only", x =800, y =.35, size = 6) +
  scale_color_manual(values = c("#DC3220","#005AB5"))```

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

乜一 2025-02-20 04:17:09

可以直接简化这两个序列的比较:

options(scipen = 20) # Show more digits before resorting
                     # to scientific notation

seq(-0.2,0.35,0.05)
[1] -0.20 -0.15 -0.10 -0.05  0.00  0.05  0.10  0.15  0.20  0.25  0.30  0.35


seq(-0.3,0.35,0.05)
 [1] -0.29999999999999998889777 -0.25000000000000000000000 -0.19999999999999998334665
 [4] -0.14999999999999996669331 -0.09999999999999997779554 -0.04999999999999998889777
 [7]  0.00000000000000005551115  0.05000000000000004440892  0.10000000000000003330669
[10]  0.15000000000000002220446  0.20000000000000001110223  0.25000000000000005551115
[13]  0.30000000000000009992007  0.34999999999999997779554

看起来像是与此相关的浮点问题:
为什么这些数字不相等?

快速修复可能是使用

round(seq(-0.3,0.35,0.05), digits = 2)
[1] -0.30 -0.25 -0.20 -0.15 -0.10 -0.05  0.00  0.05  0.10  0.15  0.20  0.25  0.30  0.35

scale_y_continuous("Relatedness", 
                   breaks = seq(-0.3,0.35,0.05), 
                   labels = ~format(round(.x,2), nsmall = 2)) 

This can be further simplified to a comparison of the two sequences directly:

options(scipen = 20) # Show more digits before resorting
                     # to scientific notation

seq(-0.2,0.35,0.05)
[1] -0.20 -0.15 -0.10 -0.05  0.00  0.05  0.10  0.15  0.20  0.25  0.30  0.35


seq(-0.3,0.35,0.05)
 [1] -0.29999999999999998889777 -0.25000000000000000000000 -0.19999999999999998334665
 [4] -0.14999999999999996669331 -0.09999999999999997779554 -0.04999999999999998889777
 [7]  0.00000000000000005551115  0.05000000000000004440892  0.10000000000000003330669
[10]  0.15000000000000002220446  0.20000000000000001110223  0.25000000000000005551115
[13]  0.30000000000000009992007  0.34999999999999997779554

Looks like a floating point issue related to this:
Why are these numbers not equal?.

Quick fix could be to to use

round(seq(-0.3,0.35,0.05), digits = 2)
[1] -0.30 -0.25 -0.20 -0.15 -0.10 -0.05  0.00  0.05  0.10  0.15  0.20  0.25  0.30  0.35

or

scale_y_continuous("Relatedness", 
                   breaks = seq(-0.3,0.35,0.05), 
                   labels = ~format(round(.x,2), nsmall = 2)) 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文