sf icon indicating copy to clipboard operation
sf copied to clipboard

.Machine$double.eps values on boundary left blank when plotting

Open frousseu opened this issue 3 years ago • 2 comments

Hi!

When a negative .Machine$double.neg.eps is on the boundary of values, it seems to be treated as an NA and no color is assigned when plotting (polygon is left blank). The problem appears in the first plot, while the second seems to be working fine. Let me know if more information is needed.

library(sf)

nc = st_read(system.file("gpkg/nc.gpkg", package = "sf"), quiet = TRUE)

nc[, "SID74"] <- seq(-.Machine$double.neg.eps, 1, length.out = nrow(nc))
plot(nc["SID74"])

nc[, "SID74"] <-
  c(-1, seq(-.Machine$double.neg.eps, 1, length.out = nrow(nc) - 1))
plot(nc["SID74"])
sessionInfo() R version 4.2.0 (2022-04-22 ucrt) Platform: x86_64-w64-mingw32/x64 (64-bit) Running under: Windows 10 x64 (build 17763)

Matrix products: default

locale: [1] LC_COLLATE=French_Canada.1252 LC_CTYPE=French_Canada.1252 LC_MONETARY=French_Canada.1252 [4] LC_NUMERIC=C LC_TIME=French_Canada.1252

attached base packages: [1] stats graphics grDevices utils datasets methods base

other attached packages: [1] sf_1.0-7

loaded via a namespace (and not attached): [1] Rcpp_1.0.8.3 compiler_4.2.0 pillar_1.7.0 class_7.3-20 viridis_0.6.2 tools_4.2.0
[7] dotCall64_1.0-1 digest_0.6.29 viridisLite_0.4.0 evaluate_0.15 lifecycle_1.0.1 tibble_3.1.7
[13] gtable_0.3.0 lattice_0.20-45 pkgconfig_2.0.3 rlang_1.0.2 DBI_1.1.2 cli_3.3.0
[19] yaml_2.3.5 spam_2.8-0 xfun_0.30 fastmap_1.1.0 e1071_1.7-9 gridExtra_2.3
[25] terra_1.5-21 dplyr_1.0.9 raster_3.5-15 knitr_1.39 generics_0.1.2 vctrs_0.4.1
[31] fields_13.3 maps_3.4.0 classInt_0.4-3 grid_4.2.0 tidyselect_1.1.2 glue_1.6.2
[37] R6_2.5.1 fansi_1.0.3 rmarkdown_2.14 sp_1.4-7 ggplot2_3.3.6 purrr_0.3.4
[43] magrittr_2.0.3 units_0.8-0 scales_1.2.0 codetools_0.2-18 fortunes_1.5-4 htmltools_0.5.2
[49] ellipsis_0.3.2 colorspace_2.0-3 KernSmooth_2.23-20 utf8_1.2.2 proxy_0.4-26 munsell_0.5.0
[55] crayon_1.5.1

sf::sf_extSoftVersion() GEOS GDAL proj.4 GDAL_with_GEOS USE_PROJ_H PROJ "3.9.1" "3.3.2" "7.2.1" "true" "true" "7.2.1"

frousseu avatar Jun 03 '22 12:06 frousseu

Wow. This is what is happening, afaict:

> cut(nc$SID74, pretty(nc$SID74), include.lowest=TRUE)
  [1] <NA>      [0,0.2]   [0,0.2]   [0,0.2]   [0,0.2]   [0,0.2]   [0,0.2]  
  [8] [0,0.2]   [0,0.2]   [0,0.2]   [0,0.2]   [0,0.2]   [0,0.2]   [0,0.2]  
 [15] [0,0.2]   [0,0.2]   [0,0.2]   [0,0.2]   [0,0.2]   [0,0.2]   (0.2,0.4]
 [22] (0.2,0.4] (0.2,0.4] (0.2,0.4] (0.2,0.4] (0.2,0.4] (0.2,0.4] (0.2,0.4]
 [29] (0.2,0.4] (0.2,0.4] (0.2,0.4] (0.2,0.4] (0.2,0.4] (0.2,0.4] (0.2,0.4]
 [36] (0.2,0.4] (0.2,0.4] (0.2,0.4] (0.2,0.4] (0.2,0.4] (0.4,0.6] (0.4,0.6]
 [43] (0.4,0.6] (0.4,0.6] (0.4,0.6] (0.4,0.6] (0.4,0.6] (0.4,0.6] (0.4,0.6]
 [50] (0.4,0.6] (0.4,0.6] (0.4,0.6] (0.4,0.6] (0.4,0.6] (0.4,0.6] (0.4,0.6]
 [57] (0.4,0.6] (0.4,0.6] (0.4,0.6] (0.4,0.6] (0.6,0.8] (0.6,0.8] (0.6,0.8]
 [64] (0.6,0.8] (0.6,0.8] (0.6,0.8] (0.6,0.8] (0.6,0.8] (0.6,0.8] (0.6,0.8]
 [71] (0.6,0.8] (0.6,0.8] (0.6,0.8] (0.6,0.8] (0.6,0.8] (0.6,0.8] (0.6,0.8]
 [78] (0.6,0.8] (0.6,0.8] (0.6,0.8] (0.8,1]   (0.8,1]   (0.8,1]   (0.8,1]  
 [85] (0.8,1]   (0.8,1]   (0.8,1]   (0.8,1]   (0.8,1]   (0.8,1]   (0.8,1]  
 [92] (0.8,1]   (0.8,1]   (0.8,1]   (0.8,1]   (0.8,1]   (0.8,1]   (0.8,1]  
 [99] (0.8,1]   (0.8,1]  
Levels: [0,0.2] (0.2,0.4] (0.4,0.6] (0.6,0.8] (0.8,1]

edzer avatar Jun 03 '22 12:06 edzer

I was half-expecting this was not a sf bug. pretty seems to be able to handle the small value with the eps.correct argument, but cut does not seem to want to include it:

x <- c(-.Machine$double.neg.eps, 1)
as.character(x)
## [1] "-1.11022302462516e-16" "1"                    
as.character(pretty(x, eps.correct = 1))
## [1] "-1.11022302462516e-16" "0.2"                   "0.4"                   "0.6"                   "0.8"                  
## [6] "1"                    
cut(x, pretty(x, eps.correct = 1), include.lowest = TRUE)
## [1] <NA>    (0.8,1]
## Levels: [-1.11e-16,0.2] (0.2,0.4] (0.4,0.6] (0.6,0.8] (0.8,1]

I can try to file a bug in bugzilla if indeed it is one. You never know with floating-point arithmetic...

frousseu avatar Jun 03 '22 17:06 frousseu