Skip to content

geom_rastercan produce an error when aesthetics are set to floating-point values.  #2516

@alexgenin

Description

@alexgenin

When using floating-point values as x/y aesthetics in geom_raster, I sometimes get the following error (and additional warning):

library(ggplot2)

data_error <- structure(list(bw = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1.22222222222222, 
  1.22222222222222, 1.22222222222222, 1.22222222222222, 1.22222222222222, 
  1.22222222222222, 1.22222222222222, 1.22222222222222, 1.22222222222222, 
  1.22222222222222, 1.44444444444444, 1.44444444444444, 1.44444444444444, 
  1.44444444444444, 1.44444444444444, 1.44444444444444, 1.44444444444444, 
  1.44444444444444, 1.44444444444444, 1.44444444444444, 1.66666666666667, 
  1.66666666666667, 1.66666666666667, 1.66666666666667, 1.66666666666667, 
  1.66666666666667, 1.66666666666667, 1.66666666666667, 1.66666666666667, 
  1.66666666666667, 1.88888888888889, 1.88888888888889, 1.88888888888889, 
  1.88888888888889, 1.88888888888889, 1.88888888888889, 1.88888888888889, 
  1.88888888888889, 1.88888888888889, 1.88888888888889, 2.11111111111111, 
  2.11111111111111, 2.11111111111111, 2.11111111111111, 2.11111111111111, 
  2.11111111111111, 2.11111111111111, 2.11111111111111, 2.11111111111111, 
  2.11111111111111, 2.33333333333333, 2.33333333333333, 2.33333333333333, 
  2.33333333333333, 2.33333333333333, 2.33333333333333, 2.33333333333333, 
  2.33333333333333, 2.33333333333333, 2.33333333333333, 2.55555555555556, 
  2.55555555555556, 2.55555555555556, 2.55555555555556, 2.55555555555556, 
  2.55555555555556, 2.55555555555556, 2.55555555555556, 2.55555555555556, 
  2.55555555555556, 2.77777777777778, 2.77777777777778, 2.77777777777778, 
  2.77777777777778, 2.77777777777778, 2.77777777777778, 2.77777777777778, 
  2.77777777777778, 2.77777777777778, 2.77777777777778, 3, 3, 3, 3, 3, 3, 
  3, 3, 3, 3), cutoff = c(0, 0.111111111111111, 0.222222222222222, 0.333333333333333, 
  0.444444444444444, 0.555555555555556, 0.666666666666667, 0.777777777777778, 
  0.888888888888889, 1, 0, 0.111111111111111, 0.222222222222222, 0.333333333333333, 
  0.444444444444444, 0.555555555555556, 0.666666666666667, 0.777777777777778, 
  0.888888888888889, 1, 0, 0.111111111111111, 0.222222222222222, 0.333333333333333, 
  0.444444444444444, 0.555555555555555, 0.666666666666667, 0.777777777777778, 
  0.888888888888889, 1, 0, 0.111111111111111, 0.222222222222222, 0.333333333333333, 
  0.444444444444444, 0.555555555555556, 0.666666666666667, 0.777777777777778, 
  0.888888888888889, 1, 0, 0.111111111111111, 0.222222222222222, 0.333333333333333, 
  0.444444444444444, 0.555555555555556, 0.666666666666667, 0.777777777777778, 
  0.888888888888889, 1, 0, 0.111111111111111, 0.222222222222222, 0.333333333333333, 
  0.444444444444444, 0.555555555555556, 0.666666666666667, 0.777777777777778, 
  0.888888888888889, 1, 0, 0.111111111111111, 0.222222222222222, 0.333333333333333, 
  0.444444444444444, 0.555555555555556, 0.666666666666667, 0.777777777777778, 
  0.888888888888889, 1, 0, 0.111111111111111, 0.222222222222222, 0.333333333333333, 
  0.444444444444444, 0.555555555555555, 0.666666666666667, 0.777777777777778, 
  0.888888888888889, 1, 0, 0.111111111111111, 0.222222222222222, 0.333333333333333, 
  0.444444444444444, 0.555555555555556, 0.666666666666667, 0.777777777777778, 
  0.888888888888889, 1, 0, 0.111111111111111, 0.222222222222222, 0.333333333333333, 
  0.444444444444444, 0.555555555555555, 0.666666666666667, 0.777777777777778, 
  0.888888888888889, 1), ncomp = c(1, 1, 1, 1, 1, 1, 1, 2, 2, 1, 1, 1, 1, 
  1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)), .Names = c("bw", "cutoff", 
  "ncomp"), class = "data.frame", row.names = c(NA, -100L))

data_ok <- data.frame(expand.grid(bw = seq(1, 3, length.out = 10), cutoff = seq(0, 
  1, length.out = 10)))
data_ok <- data_ok[order(data_ok$bw, data_ok$cutoff), ]
data_ok[, "ncomp"] <- data_error[, "ncomp"]

row.names(data_error) <- row.names(data_ok) <- NULL

# The two data.frames seem to be equal
all.equal(data_ok, data_error)
#> [1] TRUE

# ...but this produces an error
ggplot(data_error) + geom_raster(aes(x = bw, y = cutoff, fill = ncomp))
#> Warning in f(...): NAs introduced by coercion to integer range
#> Error in matrix(NA_character_, nrow = nrow, ncol = ncol): invalid 'nrow' value (too large or NA)

# ...and this is ok
ggplot(data_ok) + geom_raster(aes(x = bw, y = cutoff, fill = ncomp))

# The problem can be fixed by rounding
ggplot(data_error) + geom_raster(aes(x = round(bw, 10), y = round(cutoff, 10), 
  fill = ncomp))

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions