【问题标题】:Setting limits for x and y using coord_sf after updating ggplot2更新 ggplot2 后使用 coord_sf 设置 x 和 y 的限制
【发布时间】:2021-06-24 07:38:16
【问题描述】:

我以前做过很多次,没有任何问题。最近在指定 x 和 y 坐标如coord_sf(xlim = c(-150, 120), ylim = c(-10, 75)) 时一直报以下错误信息。奇怪的是,当我尝试各种数字时,有些数字有效,有些无效,如下例所示。任何建议将不胜感激。

library(rnaturalearth)
library(ggplot2)
library(sf)
#> Linking to GEOS 3.9.0, GDAL 3.2.1, PROJ 7.2.1
map_usa <- ne_states(country = 'united states of america') %>% st_as_sf 
p <- map_usa %>%  
  ggplot() +
  geom_sf(aes(geometry = geometry, fill = name)) +
  theme(legend.position = "none") 
p 

p + coord_sf(xlim = c(-150, 120), ylim = c(-10, 75))
#> Warning in st_cast.GEOMETRYCOLLECTION(X[[i]], ...): only first part of
#> geometrycollection is retained
#> Error in st_cast.POINT(x[[1]], to, ...): cannot create MULTILINESTRING from POINT

p + coord_sf(xlim = c(-150, 120), ylim = c(0, 75))

reprex package (v2.0.0) 于 2021-06-24 创建

【问题讨论】:

    标签: r ggplot2 sf


    【解决方案1】:

    我认为这与投影有关(¿?)。您是否已将sf 更新为v1?也许值得在ggplot2/sf 上打开一个问题。

    与此同时,重新投影形状是一种选择:

    library(rnaturalearth)
    library(ggplot2)
    library(sf)
    #> Linking to GEOS 3.9.0, GDAL 3.2.1, PROJ 7.2.1
    
    map_usa <- ne_states(country = "united states of america") %>% st_as_sf()
    
    p <- map_usa %>%
      ggplot() +
      geom_sf(aes(geometry = geometry, fill = name)) +
      theme(legend.position = "none")
    p
    

    p + coord_sf(xlim = c(-150, 120), ylim = c(-10, 75))
    #> Warning in st_cast.GEOMETRYCOLLECTION(X[[i]], ...): only first part of
    #> geometrycollection is retained
    #> Error in st_cast.POINT(x[[1]], to, ...): cannot create MULTILINESTRING from POINT
    
    # Using tigris
    
    library(tigris)
    #> To enable 
    #> caching of data, set `options(tigris_use_cache = TRUE)` in your R script or .Rprofile.
    
    map_usa2 <- tigris::states()
    
    st_crs(map_usa2)
    #> Coordinate Reference System:
    #>   User input: NAD83 
    #>   wkt:
    #> GEOGCRS["NAD83",
    #>     DATUM["North American Datum 1983",
    #>         ELLIPSOID["GRS 1980",6378137,298.257222101,
    #>             LENGTHUNIT["metre",1]]],
    #>     PRIMEM["Greenwich",0,
    #>         ANGLEUNIT["degree",0.0174532925199433]],
    #>     CS[ellipsoidal,2],
    #>         AXIS["latitude",north,
    #>             ORDER[1],
    #>             ANGLEUNIT["degree",0.0174532925199433]],
    #>         AXIS["longitude",east,
    #>             ORDER[2],
    #>             ANGLEUNIT["degree",0.0174532925199433]],
    #>     ID["EPSG",4269]]
    
    p2 <- map_usa2 %>%
      ggplot() +
      geom_sf(aes(geometry = geometry, fill = NAME)) +
      theme(legend.position = "none")
    p2
    

    p2 + coord_sf(xlim = c(-150, 120), ylim = c(-10, 75))
    

    p2 + coord_sf(xlim = c(-150, 120), ylim = c(0, 75))
    

    
    # Tigris with epsg 4326
    ggplot(st_transform(map_usa2, 4326)) +
      geom_sf() +
      coord_sf(xlim = c(-150, 120), ylim = c(-10, 75))
    #> Warning in st_cast.GEOMETRYCOLLECTION(X[[i]], ...): only first part of
    #> geometrycollection is retained
    #> Error in st_cast.POINT(x[[1]], to, ...): cannot create MULTILINESTRING from POINT
    
    
    # Rnaturalearth with epsg 4269
    ggplot(st_transform(map_usa, 4269)) +
      geom_sf() +
      coord_sf(xlim = c(-150, 120), ylim = c(-10, 75))
    

    sessionInfo()
    #> R version 4.1.0 (2021-05-18)
    #> Platform: x86_64-w64-mingw32/x64 (64-bit)
    #> Running under: Windows 10 x64 (build 19041)
    #> 
    #> Matrix products: default
    #> 
    #> locale:
    #> [1] LC_COLLATE=Spanish_Spain.1252  LC_CTYPE=Spanish_Spain.1252   
    #> [3] LC_MONETARY=Spanish_Spain.1252 LC_NUMERIC=C                  
    #> [5] LC_TIME=Spanish_Spain.1252    
    #> 
    #> attached base packages:
    #> [1] stats     graphics  grDevices utils     datasets  methods   base     
    #> 
    #> other attached packages:
    #> [1] tigris_1.4          sf_1.0-0            ggplot2_3.3.3      
    #> [4] rnaturalearth_0.1.0
    #> 
    #> loaded via a namespace (and not attached):
    #>  [1] styler_1.4.1             rnaturalearthhires_0.2.0 tidyselect_1.1.1        
    #>  [4] xfun_0.23                purrr_0.3.4              lattice_0.20-44         
    #>  [7] colorspace_2.0-1         vctrs_0.3.8              generics_0.1.0          
    #> [10] htmltools_0.5.1.1        s2_1.0.5                 yaml_2.2.1              
    #> [13] utf8_1.2.1               rlang_0.4.11             e1071_1.7-7             
    #> [16] pillar_1.6.1             foreign_0.8-81           glue_1.4.2              
    #> [19] withr_2.4.2              DBI_1.1.1                rappdirs_0.3.3          
    #> [22] sp_1.4-5                 uuid_0.1-4               wk_0.4.1                
    #> [25] lifecycle_1.0.0          stringr_1.4.0            rgeos_0.5-5             
    #> [28] munsell_0.5.0            gtable_0.3.0             evaluate_0.14           
    #> [31] knitr_1.33               maptools_1.1-1           curl_4.3.1              
    #> [34] class_7.3-19             fansi_0.5.0              highr_0.9               
    #> [37] Rcpp_1.0.6               KernSmooth_2.23-20       backports_1.2.1         
    #> [40] scales_1.1.1             classInt_0.4-3           farver_2.1.0            
    #> [43] fs_1.5.0                 digest_0.6.27            stringi_1.6.2           
    #> [46] dplyr_1.0.6              grid_4.1.0               rgdal_1.5-23            
    #> [49] tools_4.1.0              magrittr_2.0.1           proxy_0.4-26            
    #> [52] tibble_3.1.2             crayon_1.4.1             pkgconfig_2.0.3         
    #> [55] ellipsis_0.3.2           reprex_2.0.0             httr_1.4.2              
    #> [58] assertthat_0.2.1         rmarkdown_2.8            R6_2.5.0                
    #> [61] units_0.7-2              compiler_4.1.0
    

    reprex package (v2.0.0) 于 2021-06-24 创建

    【讨论】:

    • 非常感谢。在最近更新 sfggplot2 之前,它运行良好。
    【解决方案2】:

    我能够通过恢复到旧版本的 sf 包来使其工作。 它适用于 ggplot2 3.3.5 版和 sf 0.9.8 版。

    【讨论】:

      猜你喜欢
      • 2012-04-22
      • 2015-04-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-13
      • 1970-01-01
      相关资源
      最近更新 更多