Axis breaks misbehaving #550

Closed
bhive01 opened this Issue May 8, 2012 · 3 comments

4 participants

@bhive01

Thanks to David Kahle for the examples.

n <- 10

# original demonstration of problem
df <- data.frame(x = 10^runif(n, -19, -13), y = rnorm(n))
qplot(x, y, data = df) + scale_x_log10()

# happens on normal scale, too
qplot(x, y, data = df)

# this helps to see where it happens
u <- runif(n); y <- rnorm(n)
df <- NULL
for(k in 1:18){
  df <- rbind(df, data.frame(x = 10^(-k-u), y = y, g = k))
}

qplot(x, y, data = df) # ok
qplot(x, y, data = df) + scale_x_log10() # ok
qplot(x, y, data = df) + facet_wrap(~ g, scales = 'free_x')
qplot(x, y, data = df) + facet_wrap(~ g, scales = 'free_x') + scale_x_log10()
@wch
Collaborator
wch commented May 8, 2012

In scale-.r, the function scale_breaks.continuous() calls zero_range() from scales, which is defined as:

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

The default tolerance for all.equal is .Machine$double.eps ^ 0.5. According to ?.Machine, double.eps is "the smallest positive floating-point number x such that 1 + x != 1."

In general, I think scale_breaks.continuous() needs a better way of figuring out whether a non-zero range is intended or due to floating point error. (I'm not sure this is possible, though.)

@bhive01

I was sure it had something to do with the size of his values. Tiny values are tiny.

@BrianDiggs

Collecting some comments/discussion on this.

There was a patch to scales that addressed some aspects of this problem. That patch was hadley/scales#6 which I believe was at least partially in response to https://groups.google.com/d/topic/ggplot2/voh__Tl5hxA/discussion. However, that fix does not seem to handle all the cases which are triggering false positives. In particular see two StackOverflow questions: http://stackoverflow.com/q/10900809/892313 and http://stackoverflow.com/q/10808056/892313 which deal with small numbers (on the order of 10^-11 and 10^-123).

The current fix applies to small differences in large numbers (based on the code in the tests in hadley/scales@e130daa). What is missing is not triggering a false positive on (relatively) large differences in very small numbers.

There is also a discussion on ggplot-dev: https://groups.google.com/d/topic/ggplot2-dev/hNDrahVs94E/discussion

@BrianDiggs BrianDiggs added a commit to BrianDiggs/scales that referenced this issue Jun 5, 2012
@BrianDiggs BrianDiggs Improvement to zero_range to remove some false positives
Fixes hadley/ggplot2#550 and is a further enhancement to issue #6.
Uses a more robust algorithm to determine if the range is
equal to zero. Additional tests are also included.
18a5968
@hadley hadley was assigned Jul 4, 2012
@hadley hadley added a commit that closed this issue Jul 5, 2012
@hadley Bump scales version. Fixes #550 121334b
@hadley hadley closed this in 121334b Jul 5, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment