tidyverse / ggplot2

An implementation of the Grammar of Graphics in R
https://ggplot2.tidyverse.org
Other
6.47k stars 2.02k forks source link

Error: Invalid index: field name 'x_start' not found #6052

Open YUCHENG-ZHAO opened 3 weeks ago

YUCHENG-ZHAO commented 3 weeks ago

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 image I'm trying to find a picture of this. but Error: Invalid index: field name 'x_start' not found

teunbrand commented 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.

YUCHENG-ZHAO commented 3 weeks ago

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

A tibble: 983 × 2

cell geometry

1 1 EMPTY 2 2 ((3755 21600, 3754 21601, 3752 21601, 3751 21602, 3749 21602, 3748 216… 3 3 ((6301 21139, 6300 21140, 6298 21140, 6297 21141, 6295 21141, 6294 211… 4 4 ((5473 20969, 5472 20970, 5470 20970, 5469 20971, 5467 20971, 5466 209… 5 6 ((17097 20835, 17096 20836, 17088 20836, 17087 20837, 17085 20837, 170… 6 7 ((5593 20688, 5592 20689, 5588 20689, 5587 20690, 5583 20690, 5582 206… 7 9 ((5916 20649, 5915 20650, 5913 20650, 5912 20651, 5910 20651, 5909 206… 8 10 ((16466 20508, 16465 20509, 16463 20509, 16462 20510, 16460 20510, 164… 9 12 ((12131 20423, 12130 20424, 12128 20424, 12127 20425, 12125 20425, 121… 10 13 ((4470 20342, 4469 20343, 4467 20343, 4466 20344, 4464 20344, 4463 203… # ℹ 973 more rows # ℹ Use `print(n = ...)` to see more rows meta: total_counts pct_counts_mt nFeaturess_RNA_by_counts orig.ident x 759.0 2358 5.470738 1028 sample 14457.199 182.0 3832 2.948852 1290 sample 6393.816 789.0 3171 10.091454 1158 sample 12332.908 570.0 1938 5.417957 767 sample 9320.935 946.0 4935 4.235056 1745 sample 9965.027 104.0 6951 7.207596 2037 sample 6290.000 y leiden cell_type cell_num cell 759.0 14453.123 7 7_C03427D4_dendritic cell 7_C03427D4_dendritic cell 759 182.0 6651.079 1 1_C03427D4_Plasma cells 1_C03427D4_Plasma cells 182 789.0 14785.839 1 1_C03427D4_Plasma cells 1_C03427D4_Plasma cells 789 570.0 10175.530 5 5_C03427D4_B cell 5_C03427D4_B cell 570 946.0 16804.823 7 7_C03427D4_dendritic cell 7_C03427D4_dendritic cell 946 104.0 5803.645 1 1_C03427D4_Plasma cells 1_C03427D4_Plasma cells 104 code: p1 <- ggplot(nc) + geom_sf(aes(fill=meta[ as.character(nc$cell),'cell_type']))+ theme_void() p1 error:R[write to console]: Error: Invalid index: field name 'x_start' not found
teunbrand commented 3 weeks ago

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?

gbiele commented 2 weeks ago

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

teunbrand commented 2 weeks ago

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