Difficulty labeling longitude with geom_sf

I'm having trouble plotting a world map with the x-axis (longitude) labeled similarly to the y-axis. The longitude labels don't make sense to me and don't seem to respond to scale_x_continuous().

library(tidyverse)
library(sf)
#> Linking to GEOS 3.9.0, GDAL 3.2.1, PROJ 7.2.1
library(rnaturalearth)

ggplot() +
  geom_sf(data = ne_countries(returnclass = "sf"),
          fill = "gray90", color = NA) +
  geom_sf(data = ne_coastline(returnclass = "sf")) +
  ylab("") +
  xlab("") +
  theme_bw() +
  coord_sf(expand = FALSE) +
  scale_x_continuous(breaks = seq(from = -180, to = 180, by = 60))

ggplot() +
  geom_sf(data = ne_countries(returnclass = "sf"),
          fill = "gray90", color = NA) +
  geom_sf(data = ne_coastline(returnclass = "sf")) +
  ylab("") +
  xlab("") +
  theme_bw() +
  coord_sf(expand = FALSE)

Created on 2021-07-26 by the reprex package (v2.0.0)

Is there a way to format labels along both axes to match the way that the latitude is currently shown?

Both of your examples work fine for me (thanks for the reprex). Maybe look into your ggplot default parameters? reinstall the packages? I noticed that there are no grid lines on your plots either so perhaps you have something set to NULL.

2 Likes

Thanks! I hadn't realized that the grid lines were missing, too. I'll take a closer look and maybe try some re-installs.

So I've tried reinstalling sf, tidyverse, ggplot2 and haven't had any luck. Did a quick check of defaults, and nothing stood out to me. Not sure what else to try!

Your example code works for me as well, but it's interesting as I was having this exact issue the other day. It was after trying different projections using the "crs" option in coord_sf(). Restarting my computer and coming back to it fixed it, will post if I can replicate your issue.

Some session info:

R version 4.0.2 (2020-06-22)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 18363)

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

other attached packages:
 [1] rnaturalearth_0.1.0 sf_0.9-6            forcats_0.5.0      
 [4] stringr_1.4.0       dplyr_1.0.7         purrr_0.3.4        
 [7] readr_1.4.0         tidyr_1.1.2         tibble_3.0.4       
[10] ggplot2_3.3.2       tidyverse_1.3.0    
1 Like

That is interesting! I have gone through a few restarts this week and haven't had any luck. (I also tried it with R 4.0.2 seeing as you are using a different R version than me).

Thanks for sharing!

1 Like

This is a side effect from sf and can be fixed by running sf_use_s2(FALSE). See here for details.

1 Like

Thanks for following up...will bookmark that!
Cheers :smiley_cat:

1 Like

I checked my install and sf_use_s2() is already FALSE (hence why I did not have the problem). I certainly did not do that myself so I wonder what would cause it to be TRUE for some environments and FALSE for others?

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.