简体   繁体   中英

R: Unusual error plotting multipolygons with ggplot, geom_sf, and openstreetmap

I am trying to plot Lake Geneva/Lac Leman using {ggplot2}, {osmdata} and {sf}. I am getting an error whenever I try to plot the lake multipolygons (of which there are 5). I can't find any mention of this error elsewhere.

library(osmdata)
#> Data (c) OpenStreetMap contributors, ODbL 1.0. http://www.openstreetmap.org/copyright
library(tidyverse)

lake_gva <- 
  getbb("Geneva") %>% 
  opq()%>%
  add_osm_feature(key = "natural", value = "water") %>%
  osmdata_sf()

lake_gva
#> Object of class 'osmdata' with:
#>                  $bbox : 46.1777724,6.1102411,46.231885,6.1758527
#>         $overpass_call : The call submitted to the overpass API
#>                  $meta : metadata including timestamp and version numbers
#>            $osm_points : 'sf' Simple Features Collection with 22394 points
#>             $osm_lines : 'sf' Simple Features Collection with 320 linestrings
#>          $osm_polygons : 'sf' Simple Features Collection with 120 polygons
#>        $osm_multilines : NULL
#>     $osm_multipolygons : 'sf' Simple Features Collection with 5 multipolygons

ggplot() +
  geom_sf(
    data = lake_gva$osm_multipolygons
  )
#> Error in do.call(rbind, x): variable names are limited to 10000 bytes

Here is my session info:

sessionInfo()
#> R version 3.6.0 (2019-04-26)
#> Platform: x86_64-apple-darwin15.6.0 (64-bit)
#> Running under: macOS  10.15.1
#> 
#> Matrix products: default
#> BLAS:   /Library/Frameworks/R.framework/Versions/3.6/Resources/lib/libRblas.0.dylib
#> LAPACK: /Library/Frameworks/R.framework/Versions/3.6/Resources/lib/libRlapack.dylib
#> 
#> locale:
#> [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
#> 
#> attached base packages:
#> [1] stats     graphics  grDevices utils     datasets  methods   base     
#> 
#> loaded via a namespace (and not attached):
#>  [1] compiler_3.6.0  magrittr_1.5    tools_3.6.0     htmltools_0.4.0
#>  [5] yaml_2.2.0      Rcpp_1.0.3      stringi_1.4.3   rmarkdown_1.18 
#>  [9] highr_0.8       knitr_1.26      stringr_1.4.0   xfun_0.11      
#> [13] digest_0.6.23   rlang_0.4.2     evaluate_0.14

Created on 2019-12-02 by the reprex package (v0.3.0)

There is an issue in osmdata that will prevent this in future if fixed: https://github.com/ropensci/osmdata/issues/188 Essentially it's because the polygons that are part of the multipolygon object have names, and those names can be unacceptably long for ggplot2.

library(osmdata)
lake_gva <- 
  getbb("Geneva") %>% 
  opq()%>%
  add_osm_feature(key = "natural", value = "water") %>%
  osmdata_sf()

names(lake_gva$osm_multipolygons)

name_polys = lapply(lake_gva$osm_multipolygons$geometry[[1]], names)
sapply(name_polys, nchar)

lake_poly = sf::st_cast(lake_gva$osm_multipolygons$geometry, "POLYGON")
lake_gva$osm_multipolygons$geometry = lake_poly

names(lake_gva$osm_multipolygons$geometry[[2]][[1]]) = NULL]

lake_gva$osm_multipolygons$geometry = unname(lake_gva$osm_multipolygons$geometry)

library(ggplot2)
ggplot() +
  geom_sf(
    data = lake_gva$osm_multipolygons
  )

names(lake_gva$osm_multipolygons$geometry[[1]][[1]][[1]]) = NULL 
names(lake_gva$osm_multipolygons$geometry[[1]][[1]][[2]]) = "lake" 

That helps show what's going on but doesn't quite solve your problem. See here for a solution on a related issue: https://github.com/rstudio/leaflet/issues/631

This has been remedied in most recent update to the {osmdata} package. You can install it with:

devtools::install_github("ropensci/osmdata")

As @RobinLovelace mentioned, the problem comes from openstreetmap geometries including named features within their geometry specifications, something that {sf} and {tidyverse} cannot handle. The latest version includes a helper function to convert an osmdata-sf into a true {sf} object. This is osmdata::unname_osmdata_sf() . Here is your code, with that function added to allow for use with ggplot:

library(osmdata)
#> Data (c) OpenStreetMap contributors, ODbL 1.0. https://www.openstreetmap.org/copyright
library(tidyverse)
#> Warning: package 'tibble' was built under R version 3.6.2
#> Warning: package 'purrr' was built under R version 3.6.2

lake_gva <- 
  getbb("Geneva") %>% 
  opq()%>%
  add_osm_feature(key = "natural", value = "water") %>%
  osmdata_sf()

lake_gva_sf <- lake_gva %>% 
  unname_osmdata_sf()
#> Loading required namespace: sf

lake_gva_sf$osm_multipolygons %>% 
  ggplot() + geom_sf()

Created on 2020-05-26 by the reprex package (v0.3.0)

In the end I did a workaround by plotting using the sp method rather than the sf method, but hope that a solution is eventually implemented in {osmdata}/{sf} to allow the geom_sf approach:

library(osmdata)
#> Data (c) OpenStreetMap contributors, ODbL 1.0. http://www.openstreetmap.org/copyright
library(tidyverse)

lake_gva <-
  getbb("Geneva") %>% 
  opq()%>%
  add_osm_feature(key = "natural", value = "water") %>%
  osmdata_sp()

lake_gva$osm_multipolygons@data$id <- rownames(lake_gva$osm_multipolygons@data)
df_lake_gva <- 
  fortify(lake_gva$osm_multipolygons, region = "id") %>% 
  merge(lake_gva$osm_multipolygons@data, by = "id")
#> Warning in RGEOSUnaryPredFunc(spgeom, byid, "rgeos_isvalid"): Self-intersection
#> at or near point 6.2434241000000004 46.174487800000001
#> SpP is invalid
#> Warning in rgeos::gUnaryUnion(spgeom = SpP, id = IDs): Invalid objects found;
#> consider using set_RGEOS_CheckValidity(2L)

ggplot() +
  geom_polygon(
    data = df_lake_gva,
    aes(x = long, y = lat, group = group)
  )

Created on 2019-12-03 by the reprex package (v0.3.0)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM