gpt4 book ai didi

R:使用 ggplot、geom_sf 和 openstreetmap 绘制多面体时出现异常错误

转载 作者:行者123 更新时间:2023-12-03 16:03:25 26 4
gpt4 key购买 nike

我正在尝试使用 {ggplot2}、{osmdata} 和 {sf} 绘制日内瓦湖/莱曼湖。每当我尝试绘制湖泊多面体(其中有 5 个)时,都会出现错误。我在其他地方找不到任何提到这个错误的地方。

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

这是我的 session 信息:

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

创建于 2019-12-02 由 reprex package (v0.3.0)

最佳答案

osmdata 中存在一个问题,如果修复,将来会阻止此问题:https://github.com/ropensci/osmdata/issues/188本质上,这是因为作为多多边形对象一部分的多边形具有名称,而这些名称对于 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"

这有助于显示正在发生的事情,但并不能完全解决您的问题。有关相关问题的解决方案,请参见此处: https://github.com/rstudio/leaflet/issues/631

关于R:使用 ggplot、geom_sf 和 openstreetmap 绘制多面体时出现异常错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59144491/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com