1

I have the first data frame with geospatial data from .gpkg file loaded into R in usual way:

geo_data <- read_sf()

Than I have a second data frame from an external excel file containing a number of variables:

participantion_2020_raw <- read_excel()

Then I am merging the two data frames:

participantion_2020 <- participantion_2020_raw %>%
    left_join(
      geo_data %>% select(-c(2:4)),
      by = join_by(obec_code == IDN4)
    ) %>%
    st_as_sf()

Then I create a map:

map <- ggplot(participantion_2020) + geom_sf_interactive(aes(fill = votes_percent, tooltip = kraj_code, data_id = kraj_code)) + theme_void() + scale_fill_viridis_c()
girafe(ggobj = map)

and get this error:

Error:
! Problem while converting geom to grob.
ℹ Error occurred in the 1st layer.
Caused by error in `gList()`:
! only 'grobs' allowed in "gList"
Run `rlang::last_trace()` to see where the error occurred.

When I create map from the original geo_data data frame, Ggiraphe works fine. Also when I draw the map without Ggiraph using just geom_sf(), everything works, map is rendered correctly.

Then I noticed: when I change the order of data frames in left_join() that the geo_data is the first, Ggiraphe does work fine. This code works:

participantion_2020 <-
    left_join(
      geo_data %>% select(-c(2:4)),
      participantion_2020_raw,
      by = join_by(IDN4 == obec_code)
    ) %>%
    st_as_sf()

but when I do the full_join() the same way as above, Ggiraphe does NOT work and throws the same error as before:

participantion_2020 <-
    full_join(
      geo_data %>% select(-c(2:4)),
      participantion_2020_raw,
      by = join_by(IDN4 == obec_code)
    ) %>%
    st_as_sf()

Just noting again that in all cases above the non-interactive geom_sf() works correctly. It seems to me that something happens to the data structure when merging two data frames that causes the error; however I am not a programmer so I cant debug it. And finally, my environment:

R version 4.2.2 (2022-10-31 ucrt)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19045)

Matrix products: default

locale:
[1] LC_COLLATE=Slovak_Slovakia.utf8  LC_CTYPE=Slovak_Slovakia.utf8    LC_MONETARY=Slovak_Slovakia.utf8
[4] LC_NUMERIC=C                     LC_TIME=Slovak_Slovakia.utf8    

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

other attached packages:
 [1] spData_2.2.2          ggiraph_0.8.7         stringi_1.7.12        sf_1.0-12             readxl_1.4.2         
 [6] here_1.0.1            shinycssloaders_1.0.0 dataui_0.0.1          bslib_0.4.2           shinyWidgets_0.7.6   
[11] reactablefmtr_2.0.0   reactable_0.4.4       plotly_4.10.1         lubridate_1.9.2       forcats_1.0.0        
[16] stringr_1.5.0         dplyr_1.1.0           purrr_1.0.1           readr_2.1.4           tidyr_1.3.0          
[21] tibble_3.1.8          ggplot2_3.4.2         tidyverse_2.0.0       shinydashboard_0.7.2  shiny_1.7.4          

loaded via a namespace (and not attached):
 [1] httr_1.4.5         sass_0.4.5         jsonlite_1.8.4     viridisLite_0.4.1  sp_1.6-0           cellranger_1.1.0  
 [7] remotes_2.4.2      yaml_2.3.7         lattice_0.20-45    pillar_1.9.0       glue_1.6.2         uuid_1.1-0        
[13] digest_0.6.31      promises_1.2.0.1   colorspace_2.1-0   htmltools_0.5.4    httpuv_1.6.9       spDataLarge_2.0.9 
[19] pkgconfig_2.0.3    xtable_1.8-4       scales_1.2.1       later_1.3.0        tzdb_0.3.0         timechange_0.2.0  
[25] proxy_0.4-27       generics_0.1.3     farver_2.1.1       ellipsis_0.3.2     cachem_1.0.7       pacman_0.5.1      
[31] withr_2.5.0        lazyeval_0.2.2     cli_3.6.0          crayon_1.5.2       magrittr_2.0.3     mime_0.12         
[37] fansi_1.0.4        class_7.3-20       tools_4.2.2        data.table_1.14.8  hms_1.1.3          lifecycle_1.0.3   
[43] munsell_0.5.0      compiler_4.2.2     jquerylib_0.1.4    e1071_1.7-13       systemfonts_1.0.4  rlang_1.1.0       
[49] classInt_0.4-8     units_0.8-1        grid_4.2.2         rstudioapi_0.14    htmlwidgets_1.6.2  crosstalk_1.2.0   
[55] labeling_0.4.2     gtable_0.3.3       curl_5.0.0         DBI_1.1.3          R6_2.5.1           fastmap_1.1.0     
[61] utf8_1.2.3         rprojroot_2.0.3    KernSmooth_2.23-20 Rcpp_1.0.10        vctrs_0.5.2        tidyselect_1.2.0 

I tryed and tested many alternatives...

sketman
  • 21
  • 1
  • 1
  • 1
    You should make your post [reproducible](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) by using some data that everyone has access to (you can also provide a subset of your own data with `dput()`) – bretauv Apr 13 '23 at 15:08
  • When plotting U.S. ZIP codes clipped to county boundaries, I had the same issue (`geom_sf_interactive` failed while `geom_sf` worked) when using an sf geometry collection created with sf::st_intersection. Some of the shapes in the sf file returned had geometries that were not polygons. I did not get an error with `geom_sf_interactive` after using `st_make_valid` to fix the sf file. In this example, "zips" and "county" are sf files: `shapes_to_plot <- st_intersection(zips, county)` `shapes_to_plot <- st_make_valid(shapes_to_plot)` – Charlie Jul 19 '23 at 21:54

0 Answers0