1

My code for creating some maps stopped working after I updated R and libraries recently. geom_sf() is throwing an error which id did not have before.

library("sf")
library("ggplot2")
library("maps")

usa = st_as_sf(maps::map('state',region = c("WA", "OR"), plot = FALSE, fill = TRUE))


base <-
  ggplot() +
  geom_sf(data = usa)
base +
  coord_sf(
    xlim = c(-123.4,-121.8),
    ylim = c(48.99, 47.6),
    expand = FALSE
  )
Error in st_normalize.sfc(x, c(x_range[1], y_range[1], x_range[2], y_range[2])) : 
  domain must have a positive range

A similar question was posted here but there was a problem with reproducibility and no real answer.

My session info:

Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 18363)

Matrix products: default

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252   
[3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    

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

other attached packages:
[1] maps_3.3.0    ggplot2_3.3.3 sf_0.9-7     

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.6         rstudioapi_0.13    magrittr_2.0.1     units_0.6-7        tidyselect_1.1.0  
 [6] munsell_0.5.0      ggspatial_1.1.5    colorspace_2.0-0   here_1.0.1         R6_2.5.0          
[11] rlang_0.4.10       dplyr_1.0.3        tools_4.0.3        grid_4.0.3         gtable_0.3.0      
[16] KernSmooth_2.23-18 e1071_1.7-4        DBI_1.1.1          withr_2.4.0        class_7.3-17      
[21] ellipsis_0.3.1     yaml_2.2.1         rprojroot_2.0.2    assertthat_0.2.1   tibble_3.0.5      
[26] lifecycle_0.2.0    crayon_1.3.4       farver_2.0.3       purrr_0.3.4        vctrs_0.3.6       
[31] glue_1.4.2         compiler_4.0.3     pillar_1.4.7       generics_0.1.0     scales_1.1.1      
[36] classInt_0.4-3     pkgconfig_2.0.3  
m_c
  • 496
  • 2
  • 19

1 Answers1

3

Because you inverted the latitudes' values. This should work:

base +
  coord_sf(
    xlim = c(-123.4,-121.8),
    ylim = c(47.6, 48.99),
    #ylim = c(48.99, 47.6), ## here was the error
    expand = FALSE
  )
Nicolás Velasquez
  • 5,623
  • 11
  • 22