rggplot2axis-labelsdecimalformat

Why does ggplot suddenly change formate of y-axis numbers to exponential?


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


Solution

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