r-spatial / mapview

Interactive viewing of spatial data in R
https://r-spatial.github.io/mapview/
GNU General Public License v3.0
519 stars 90 forks source link

Unable to make maps side by side "Error: attempt to select less than one element in get1index" #460

Closed Rohit-Satyam closed 1 year ago

Rohit-Satyam commented 1 year ago

Dear Developers

I was trying to use mapview to make a plot of dengue cases in R using mapview function. The separate plots could be produced separately but when I tried creating Side-by-side maps, I get an error:

m1 | m2
Error in `*tmp*`[[idx]] : 
  attempt to select less than one element in get1index

Code to reproduce

library(ggplot2)
library(viridis)
library(plotly)

d <- st_read("india.shp", quiet = TRUE)
d$Y2018_cases <- replicate(n = nrow(d), expr = {sample(1:100,size = 1,replace = T)})
d$Y2019_cases <- replicate(n = nrow(d), expr = {sample(1:100,size = 1,replace = T)})
st_crs(d)$epsg
d <- st_transform(d, 4326)

## mAKING Side by side plots
library(leaflet.extras2)
library(RColorBrewer)
library(leaflet)
library(mapview)
pal <- colorRampPalette(brewer.pal(9, "YlOrRd"))
at <- seq(min(c(d$Y2018_cases, d$Y2019_cases)), max(c(d$Y2018_cases, d$Y2019_cases)),
          length.out = 8)

m1 <- mapview(d, zcol = "Y2018_cases", map.types = "CartoDB.Positron",
              col.regions = pal, at = at)
m2 <- mapview(d, zcol = "Y2019_cases", map.types = "CartoDB.Positron",
              col.regions = pal, at = at)

Side Note: The shape files were created using QGIS software because the correct and the most updated state boundary information was not available anywhere. So they are provided below.

I thought maybe the way shape files were produced could be the source of error, but the error persists even when I get the sf object using raster::getData function as follows:

d1 <- raster::getData('GADM', country = 'INDIA', level = 2) 
d1 <- st_as_sf(d1)
d1$Y2018_cases <- replicate(n = nrow(d1), expr = {sample(1:100,size = 1,replace = T)})
d1$Y2019_cases <- replicate(n = nrow(d1), expr = {sample(1:100,size = 1,replace = T)})
pal <- colorRampPalette(brewer.pal(9, "YlOrRd"))
at <- seq(min(c(d1$Y2018_cases, d1$Y2019_cases)), max(c(d1$Y2018_cases, d1$Y2019_cases)),
          length.out = 8)

m1 <- mapview(d1, zcol = "Y2018_cases", map.types = "CartoDB.Positron",
              col.regions = pal, at = at)
m2 <- mapview(d1, zcol = "Y2019_cases", map.types = "CartoDB.Positron",
              col.regions = pal, at = at)
m1 | m2

india.zip

Rohit-Satyam commented 1 year ago

Here is session Info if it's relevant

> sessionInfo()
R version 4.3.1 (2023-06-16 ucrt)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 11 x64 (build 22621)

Matrix products: default

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

time zone: Asia/Riyadh
tzcode source: internal

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

other attached packages:
 [1] mapview_2.11.0        RColorBrewer_1.1-3    leaflet.extras2_1.2.2 leaflet_2.2.0        
 [5] plotly_4.10.2         viridis_0.6.4         viridisLite_0.4.2     dplyr_1.1.2          
 [9] mgsub_1.7.3           sf_1.0-14             raster_3.6-23         sp_2.0-0             
[13] ggplot2_3.4.3         maps_3.4.1            rapportools_1.1      

loaded via a namespace (and not attached):
 [1] tidyselect_1.2.0         farver_2.1.1             fastmap_1.1.1            lazyeval_0.2.2          
 [5] digest_0.6.33            lifecycle_1.0.3          ellipsis_0.3.2           terra_1.7-39            
 [9] magrittr_2.0.3           compiler_4.3.1           rlang_1.1.1              tools_4.3.1             
[13] utf8_1.2.3               yaml_2.3.7               data.table_1.14.8        knitr_1.44              
[17] labeling_0.4.3           htmlwidgets_1.6.2        classInt_0.4-9           plyr_1.8.8              
[21] KernSmooth_2.23-21       withr_2.5.0              purrr_1.0.2              BiocGenerics_0.46.0     
[25] grid_4.3.1               stats4_4.3.1             fansi_1.0.4              e1071_1.7-13            
[29] leafem_0.2.3             colorspace_2.1-0         scales_1.2.1             MASS_7.3-60             
[33] cli_3.6.1                generics_0.1.3           rstudioapi_0.15.0        httr_1.4.7              
[37] reshape2_1.4.4           DBI_1.1.3                proxy_0.4-27             pander_0.6.5            
[41] stringr_1.5.0            base64enc_0.1-3          vctrs_0.6.3              webshot_0.5.5           
[45] jsonlite_1.8.7           S4Vectors_0.38.1         crosstalk_1.2.0          tidyr_1.3.0             
[49] jquerylib_0.1.4          units_0.8-3              glue_1.6.2               codetools_0.2-19        
[53] leaflet.providers_1.13.0 stringi_1.7.12           gtable_0.3.4             munsell_0.5.0           
[57] tibble_3.2.1             pillar_1.9.0             htmltools_0.5.6          satellite_1.0.4         
[61] R6_2.5.1                 lattice_0.21-8           png_0.1-8                class_7.3-22            
[65] Rcpp_1.0.11              gridExtra_2.3            xfun_0.40                pkgconfig_2.0.3    
tim-salabim commented 1 year ago

Thanks, good catch! Should work now