Skip to content

Coord_sf does not preserve the latitude lines #5133

Closed
@larry77

Description

@larry77

Please look at the reprex at the end of the post. I plot a world map, but when I change the coordinates, the grid is at least partially lost (the latitude lines are gone). Can you do anything about it? I discussed this online and it seems that the problem stems from ggplot2 rather than rnaturalearth or sf.

The suggestion given here

#4571 (comment)

does not work in my case.

Thanks!

rm(list=ls())

library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(ggplot2)
library(rnaturalearth)
library(sf)
#> Linking to GEOS 3.9.0, GDAL 3.2.2, PROJ 7.2.1; sf_use_s2() is TRUE

sf_use_s2(FALSE)
#> Spherical geometry (s2) switched off

ww_ini <- ne_countries(scale = "medium",
                       type = 'map_units',
                       returnclass = "sf")


gpl1 <- ggplot(data = ww_ini) +
  geom_sf(  col = "black", lwd = 0.3 )+
  xlab(NULL) + ylab(NULL) +
    ggtitle("Test title")+
      theme(plot.background = element_rect(fill = "white"),
        panel.background = element_rect(fill = 'white', colour = 'black'),
        panel.grid.major = element_line(colour = "grey"),
        legend.position="top",
        plot.title = element_text(lineheight=.8, size=24, face="bold",
                                  vjust=1),
        legend.text = element_text(vjust=.4,lineheight=1,size = 14),
        legend.title = element_text(vjust=1,lineheight=1, size=14,
                                    face="bold" ))

## this is fine

gpl1

sf_use_s2(FALSE)


gpl2 <- ggplot(data = ww_ini) +
  geom_sf(  col = "black", lwd = 0.3 )+
  xlab(NULL) + ylab(NULL) +
    ggtitle("Test title")+
    coord_sf( crs = "+proj=eqearth +wktext" )+
      theme(plot.background = element_rect(fill = "white"),
        panel.background = element_rect(fill = 'white', colour = 'black'),
        panel.grid.major = element_line(colour = "grey"),
        legend.position="top",
        plot.title = element_text(lineheight=.8, size=24, face="bold",
                                  vjust=1),
        legend.text = element_text(vjust=.4,lineheight=1,size = 14),
        legend.title = element_text(vjust=1,lineheight=1, size=14,
                                    face="bold" ))

## but this is not fine. Part of the grid has disappeared

gpl2 

sessionInfo()
#> R version 4.2.2 (2022-10-31)
#> Platform: x86_64-pc-linux-gnu (64-bit)
#> Running under: Debian GNU/Linux 11 (bullseye)
#> 
#> Matrix products: default
#> BLAS:   /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.9.0
#> LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.9.0
#> 
#> locale:
#>  [1] LC_CTYPE=en_GB.UTF-8       LC_NUMERIC=C              
#>  [3] LC_TIME=en_GB.UTF-8        LC_COLLATE=en_GB.UTF-8    
#>  [5] LC_MONETARY=en_GB.UTF-8    LC_MESSAGES=en_GB.UTF-8   
#>  [7] LC_PAPER=en_GB.UTF-8       LC_NAME=C                 
#>  [9] LC_ADDRESS=C               LC_TELEPHONE=C            
#> [11] LC_MEASUREMENT=en_GB.UTF-8 LC_IDENTIFICATION=C       
#> 
#> attached base packages:
#> [1] stats     graphics  grDevices utils     datasets  methods   base     
#> 
#> other attached packages:
#> [1] sf_1.0-9            rnaturalearth_0.1.0 ggplot2_3.4.0      
#> [4] dplyr_1.0.10       
#> 
#> loaded via a namespace (and not attached):
#>  [1] Rcpp_1.0.9              pillar_1.8.1            compiler_4.2.2         
#>  [4] highr_0.10              class_7.3-20            tools_4.2.2            
#>  [7] digest_0.6.31           lattice_0.20-45         evaluate_0.19          
#> [10] lifecycle_1.0.3         tibble_3.1.8            gtable_0.3.1           
#> [13] pkgconfig_2.0.3         rlang_1.0.6             reprex_2.0.2           
#> [16] cli_3.5.0               DBI_1.1.3               yaml_2.3.6             
#> [19] xfun_0.36               fastmap_1.1.0           rnaturalearthdata_0.1.0
#> [22] e1071_1.7-12            withr_2.5.0             stringr_1.5.0          
#> [25] knitr_1.41              generics_0.1.3          fs_1.5.2               
#> [28] vctrs_0.5.1             classInt_0.4-8          grid_4.2.2             
#> [31] tidyselect_1.2.0        glue_1.6.2              R6_2.5.1               
#> [34] fansi_1.0.3             rmarkdown_2.19          sp_1.5-1               
#> [37] farver_2.1.1            magrittr_2.0.3          units_0.8-1            
#> [40] scales_1.2.1            htmltools_0.5.4         assertthat_0.2.1       
#> [43] colorspace_2.0-3        KernSmooth_2.23-20      utf8_1.2.2             
#> [46] proxy_0.4-27            stringi_1.7.8           munsell_0.5.0

Created on 2023-01-06 with reprex v2.0.2

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions