ropensci / rnaturalearth

An R package to hold and facilitate interaction with natural earth map data :earth_africa:
http://ropensci.github.io/rnaturalearth/
Other
214 stars 24 forks source link

coastline problem when using world projection #21

Closed Rekyt closed 6 years ago

Rekyt commented 6 years ago

Hi @AndySouth, thank you for building rnaturalearth it's a great tool to draw great maps quickly.

I'm currently having a issue using worldwide coastline with certain projections. It may not be due to the package, but you maybe aware of a solution to this problem. When plotting world coastline with projections I get a weird line between each part of the world because of Russian coastline: Here's a reprex:

library("ggplot2")

# No problem
ggplot() +
    geom_sf(data = rnaturalearth::ne_coastline(returnclass = "sf"))


# Problem with Russian coastline
ggplot() +
    geom_sf(data = rnaturalearth::ne_coastline(returnclass = "sf")) +
    coord_sf(crs = sf::st_crs(3395))

Session info ``` r devtools::session_info() #> Session info ------------------------------------------------------------- #> setting value #> version R version 3.5.0 (2018-04-23) #> system x86_64, mingw32 #> ui RTerm #> language (EN) #> collate French_France.1252 #> tz Europe/Paris #> date 2018-05-18 #> Packages ----------------------------------------------------------------- #> package * version date #> backports 1.1.2 2017-12-13 #> base * 3.5.0 2018-04-23 #> class 7.3-14 2015-08-30 #> classInt 0.2-3 2018-04-16 #> colorspace 1.3-2 2016-12-14 #> compiler 3.5.0 2018-04-23 #> curl 3.2 2018-03-28 #> datasets * 3.5.0 2018-04-23 #> DBI 1.0.0 2018-05-02 #> devtools 1.13.5 2018-02-18 #> digest 0.6.15 2018-01-28 #> e1071 1.6-8 2017-02-02 #> evaluate 0.10.1 2017-06-24 #> ggplot2 * 2.2.1.9000 2018-05-15 #> graphics * 3.5.0 2018-04-23 #> grDevices * 3.5.0 2018-04-23 #> grid 3.5.0 2018-04-23 #> gtable 0.2.0 2016-02-26 #> htmltools 0.3.6 2017-04-28 #> httr 1.3.1 2017-08-20 #> knitr 1.20 2018-02-20 #> lattice 0.20-35 2017-03-25 #> lazyeval 0.2.1 2017-10-29 #> magrittr 1.5 2014-11-22 #> memoise 1.1.0 2017-04-21 #> methods * 3.5.0 2018-04-23 #> mime 0.5 2016-07-07 #> munsell 0.4.3 2016-02-13 #> pillar 1.2.2 2018-04-26 #> plyr 1.8.4 2016-06-08 #> R6 2.2.2 2017-06-17 #> Rcpp 0.12.16 2018-03-13 #> rlang 0.2.0 2018-02-20 #> rmarkdown 1.9 2018-03-01 #> rnaturalearth 0.1.0 2017-03-21 #> rnaturalearthdata 0.1.0 2017-02-21 #> rprojroot 1.3-2 2018-01-03 #> scales 0.5.0.9000 2018-05-03 #> sf 0.6-3 2018-05-17 #> sp 1.2-7 2018-01-19 #> spData 0.2.8.3 2018-03-25 #> stats * 3.5.0 2018-04-23 #> stringi 1.2.2 2018-05-02 #> stringr 1.3.1 2018-05-10 #> tibble 1.4.2 2018-01-22 #> tools 3.5.0 2018-04-23 #> udunits2 0.13 2016-11-17 #> units 0.5-1 2018-01-08 #> utils * 3.5.0 2018-04-23 #> withr 2.1.2 2018-05-03 #> xml2 1.2.0 2018-01-24 #> yaml 2.1.19 2018-05-01 #> source #> CRAN (R 3.5.0) #> local #> CRAN (R 3.5.0) #> CRAN (R 3.5.0) #> CRAN (R 3.5.0) #> local #> CRAN (R 3.5.0) #> local #> CRAN (R 3.5.0) #> CRAN (R 3.5.0) #> CRAN (R 3.5.0) #> CRAN (R 3.5.0) #> CRAN (R 3.5.0) #> Github (tidyverse/ggplot2@daba7ab) #> local #> local #> local #> CRAN (R 3.5.0) #> CRAN (R 3.5.0) #> CRAN (R 3.5.0) #> CRAN (R 3.5.0) #> CRAN (R 3.5.0) #> CRAN (R 3.5.0) #> CRAN (R 3.5.0) #> CRAN (R 3.5.0) #> local #> CRAN (R 3.5.0) #> CRAN (R 3.5.0) #> CRAN (R 3.5.0) #> CRAN (R 3.5.0) #> CRAN (R 3.5.0) #> CRAN (R 3.5.0) #> CRAN (R 3.5.0) #> CRAN (R 3.5.0) #> CRAN (R 3.5.0) #> CRAN (R 3.5.0) #> CRAN (R 3.5.0) #> Github (hadley/scales@d767915) #> CRAN (R 3.5.0) #> CRAN (R 3.5.0) #> CRAN (R 3.5.0) #> local #> CRAN (R 3.5.0) #> CRAN (R 3.5.0) #> CRAN (R 3.5.0) #> local #> CRAN (R 3.5.0) #> CRAN (R 3.5.0) #> local #> Github (jimhester/withr@79d7b0d) #> CRAN (R 3.5.0) #> CRAN (R 3.5.0) ```
mdsumner commented 6 years ago

Hi there, this is actually a generic and pervasive problem that's not got anything to do with rnaturalearth - the key issue is that the longitude -180/180 is ambiguous in terms of east or west here, and low down in the transformations that ambiguity puts a coordinate on that meridian to the "wrong side".

You can workaround it by slightly cropping the lines, it works to do this on the eastern side for this data.

x <- rnaturalearth::ne_coastline(returnclass = "sf")
library(sf)
ggplot() +
  geom_sf(data = st_crop(x, c(xmin = -180, xmax = 179.9999, ymin = -90, ymax = 90))) +
  coord_sf(crs = sf::st_crs(3395))

image

(st_crop is a relatively new helper function that makes it easier to specify a bounding box to crop to).

There's often some subtle workaround trick like this that can be used, but unfortunately this is unlikely to be a generally useful approach, it depends on particular data and situations.

Rekyt commented 6 years ago

Thanks for the quick answer @mdsumner, I didn't know the st_crop() function.

I found another workaround by getting higher resolution data:

library("ggplot2")

ggplot() +
    geom_sf(data = rnaturalearth::ne_coastline("medium", returnclass = "sf")) +
    coord_sf(crs = sf::st_crs(3395))