Y axis scaling issue with small numbers

autoscaling, ggplot2, r

Solution

As Brian Diggs commented in response to a related post that @joran pointed to above, the problem's being caused by the function `scales::zero_range()`. It uses `all.equal()` to test whether the smallest and largest y-values are within `tolerance = .Machine$double.eps ^ 0.5 = 1.490116e-08` of one another; if they are, the data are plotted with the "zero scale" used in your example.

As a temporary fix, you can use `fixInNamespace()` to remove the offending bit of `zero_range()`.

library(scales)  ## (The scales package needs to be on your search path)
fixInNamespace("zero_range", pos="package:scales")

In the editor launched by `fixInNamespace()`, replace this definition of `zero_range()`:

function (x) 
{
    length(x) == 1 || isTRUE(all.equal(x[1] - x[2], 0))
}

with this one (making sure to save the edited version):

function (x) 
{
    length(x) == 1 
}

The code you supplied then runs just fine:

Problem

I have an issue with y-scaling for numbers smaller than 1e-10 : they all appear on the same horizontal line. Here is a reproducible example : ``` file <- structure(list(I = c(-7.254574e-11, -5.649333e-11, -5.015416e-11, -4.228137e-11, -3.287486e-11, -2.714915e-11, -2.203692e-11, -1.784489e-11, -1.150574e-11, -1.058553e-11, -6.189018e-12, -3.735149e-12, -2.303724e-12, 6.610914e-13, 1.274374e-12, -3.610768e-13, 5.465134e-12, 6.691699e-12, 8.020478e-12, 1.139353e-11, 1.537988e-11, 1.926399e-11, 2.130825e-11, 2.45791e-11, 3.204071e-11, 3.582262e-11, 4.287535e-11, 4.624839e-11, 5.16657e-11, 6.035387e-11), V = c(-2, -1.867, -1.733, -1.6, -1.467, -1.333, -1.2, -1.067, -0.933, -0.8, -0.667, -0.533, -0.4, -0.267, -0.133, 0, 0.133, 0.267, 0.4, 0.533, 0.667, 0.8, 0.933, 1.067, 1.2, 1.333, 1.467, 1.6, 1.733, 1.867)), .Names = c("I", "V"), class = "data.frame", row.names = c(NA, -30L)) plot(file$V,file$I) gg <- ggplot(file,aes(x = V,y=I)) print(gg + geom_point()) ``` As you can see, using the base plot function the points are displayed correctly while using ggplot2 they are displayed on a horizontal line. I saw a similar post on the mailing list in May 2011, to which Hadley answered that it worked with the development version of ggplot2. However, using the R version described below I still get the error. ``` > sessionInfo() R version 2.15.0 (2012-03-30) Platform: i686-pc-linux-gnu (32-bit) locale: [1] LC_CTYPE=fr_FR.UTF-8 LC_NUMERIC=C LC_TIME=fr_FR.UTF-8 [4] LC_COLLATE=fr_FR.UTF-8 LC_MONETARY=fr_FR.UTF-8 LC_MESSAGES=fr_FR.UTF-8 [7] LC_PAPER=C LC_NAME=C LC_ADDRESS=C [10] LC_TELEPHONE=C LC_MEASUREMENT=fr_FR.UTF-8 LC_IDENTIFICATION=C attached base packages: [1] stats graphics grDevices utils datasets methods base other attached packages: [1] tikzDevice_0.6.2 filehash_2.2-1 scales_0.2.0 plyr_1.7.1 reshape2_1.2.1 [6] ggplot2_0.9.0 loaded via a namespace (and not attached): [1] colorspace_1.1-1 dichromat_1.2-4 digest_0.5.2 grid_2.15.0 MASS_7.3-18 [6] memoise_0.1 munsell_0.3 proto_0.3-9.2 RColorBrewer_1.0-5 stringr_0.6 [11] tools_2.15.0 ``` Anyone has a clue ? Thank you in advance ! Thibaud Ruelle

Original source

Related problems