Zhiqiang Wang
Zhiqiang Wang

Reputation: 6769

Setting limits for x and y using coord_sf after updating ggplot2

I have done this many times before without any problems. Recently, it kept giving the following error message when specifying x and y coordinates such as coord_sf(xlim = c(-150, 120), ylim = c(-10, 75)). The strange thing is when I try various numbers, some numbers work and some do not, as shown in the following example. Any suggestions would be appreciated.

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))

Created on 2021-06-24 by the reprex package (v2.0.0)

Upvotes: 3

Views: 2890

Answers (2)

Michelle
Michelle

Reputation: 1

I was able to make it work by reverting to an older version of sf package. It works for me with ggplot2 version 3.3.5 and sf version 0.9.8.

Upvotes: 0

dieghernan
dieghernan

Reputation: 3402

I think this is related with the projection (¿?). Have you updated sf to v1? Maybe it is worth opening an issue on ggplot2/sf.

In the meantime, reprojecting the shape would be an option:

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

Created on 2021-06-24 by the reprex package (v2.0.0)

Upvotes: 1

Related Questions