Open YUCHENG-ZHAO opened 3 weeks ago
Hi there, thanks for the report, but could you please use a minimal reproducible example to illustrate the issue? At this point, it is unclear whether the issue is with the data or the plotting.
My nc data : Simple feature collection with 983 features and 1 field (with 1 geometry empty) Geometry type: POLYGON Dimension: XY Bounding box: xmin: 2552 ymin: 2525 xmax: 21633 ymax: 21697 Geodetic CRS: WGS 84
cell geometry
I'm sorry I cannot reproduce the issue from this pasted data. Could you try to isolate the issue with one of the build-in datasets?
I encounter the same error with the code below.
Additional information:
Reproducible example (data is attached): bydel2020_mednavn.json
oslo_geojson = st_read("Maps/bydel2020_mednavn.geojson")
rs = which(grepl("^0301",oslo_geojson$bydelnr))
oslo_geojson$Oslo = 0
oslo_geojson$Oslo[rs] = 1
oslo_geojson %>%
filter(Oslo > 0) %>%
ggplot(aes(fill = Oslo)) +
geom_sf() +
theme_minimal()
Session Info R version 4.3.2 (2023-10-31 ucrt) Platform: x86_64-w64-mingw32/x64 (64-bit) Running under: Windows 11 x64 (build 22621)
Matrix products: default
locale:
[1] LC_COLLATE=English_United States.utf8 LC_CTYPE=English_United States.utf8
[3] LC_MONETARY=English_United States.utf8 LC_NUMERIC=C
[5] LC_TIME=English_United States.utf8
time zone: Europe/Oslo tzcode source: internal
attached base packages: [1] stats graphics grDevices utils datasets methods base
other attached packages: [1] dplyr_1.1.4 ggplot2_3.5.1 sf_1.0-16
loaded via a namespace (and not attached):
[1] gtable_0.3.5 jsonlite_1.8.8 compiler_4.3.2 promises_1.3.0 tidyselect_1.2.1
[6] Rcpp_1.0.11 later_1.3.2 jquerylib_0.1.4 scales_1.3.0 fastmap_1.2.0
[11] mime_0.12 R6_2.5.1 generics_0.1.3 classInt_0.4-10 tibble_3.2.1
[16] units_0.8-5 munsell_0.5.1 shiny_1.9.1 DBI_1.2.2 bslib_0.7.0
[21] pillar_1.9.0 rlang_1.1.4 utf8_1.2.4 cachem_1.1.0 httpuv_1.6.15
[26] sass_0.4.9 memoise_2.0.1 cli_3.6.3 withr_3.0.0 magrittr_2.0.3
[31] class_7.3-22 digest_0.6.35 grid_4.3.2 rstudioapi_0.16.0 xtable_1.8-4
[36] lifecycle_1.0.4 vctrs_0.6.5 KernSmooth_2.23-22 proxy_0.4-27 glue_1.7.0
[41] farver_2.1.2 fansi_1.0.6 e1071_1.7-14 colorspace_2.1-0 pkgconfig_2.0.3
[46] tools_4.3.2 htmltools_0.5.8.1
Thanks @gbiele, I can execute your example and reproduce the reported error.
The issue at hand is that we can't seem to compute a graticule for the data (see example). When the graticule is empty, ggplot2 doesn't handle the data well. I think the thing that should be changed is ggplot2 should skip dealing with graticules when they are empty.
library(ggplot2)
library(sf)
#> Linking to GEOS 3.12.1, GDAL 3.8.4, PROJ 9.3.1; sf_use_s2() is TRUE
library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
oslo_geojson = st_read("https://github.com/user-attachments/files/16750144/bydel2020_mednavn.json")
#> Reading layer `sql_statement' from data source
#> `https://github.com/user-attachments/files/16750144/bydel2020_mednavn.json'
#> using driver `GeoJSON'
#> Simple feature collection with 38 features and 2 fields
#> Geometry type: MULTIPOLYGON
#> Dimension: XY
#> Bounding box: xmin: -43954.67 ymin: 6564066 xmax: 285864.4 ymax: 7051059
#> Geodetic CRS: WGS 84
rs = which(grepl("^0301",oslo_geojson$bydelnr))
oslo_geojson$Oslo = 0
oslo_geojson$Oslo[rs] = 1
oslo_geojson %>%
filter(Oslo > 0) %>%
st_graticule()
#> Warning in st_is_longlat(x): bounding box has potentially an invalid value
#> range for longlat data
#> Simple feature collection with 0 features and 3 fields
#> Bounding box: xmin: NA ymin: NA xmax: NA ymax: NA
#> Geodetic CRS: WGS 84
#> [1] degree type degree_label geometry
#> <0 rows> (or 0-length row.names)
Created on 2024-08-26 with reprex v2.1.1
options(repr.plot.width=10,repr.plot.height=10) p1 <- ggplot(nc) + geom_sf(aes(fill=meta[ as.character(nc$cell),'cell_type']))+ theme_void() p1 I'm trying to find a picture of this. but Error: Invalid index: field name 'x_start' not found