Search code examples
rggplot2giswgs84

How can I get equal lan lat grids using WGS84 with ggplot in R


I use am familiar with R and ArcGIS pro. I want to plot maps using WGS84 in R using ggplot.

I get equal lat Lon grids in ArcGIS pro while zooming to, say, Germany. But I am not able to get equal grids in R using ggplot2 using WGS84.

Following is what I am trying:

library("ggplot2")
library("rnaturalearth")
library("rnaturalearthdata")
library("terra")
library("sf")

world <- ne_countries(scale = "medium", returnclass = "sf")
world_projected <- st_transform(world, crs("epsg:4326"))

ggplot(data = world_projected) +
  geom_sf(fill = "transparent") +
  theme(
    panel.grid.major = element_line(colour = alpha("black", 0.1), linewidth=1, linetype = 1)
  ) +
  coord_sf(xlim = c(5, 16), ylim = c(46, 56), default_crs = crs("epsg:4326"))

What I get in ArcGIS Pro

enter image description here

What I get in R

enter image description here

Thanks in advance.

Pallav


Solution

  • It looks like you want to plot an equidistant cylindrical projection. This is straightforward, but you will need to convert your x and y range to the correct projection too:

    library(ggplot2)
    library(rnaturalearth)
    library(sf)
    
    world <- ne_countries(scale = "medium", returnclass = "sf")
    
    coords <- st_sf(a = 1:2, geom = st_sfc(st_point(c(5, 46)), st_point(c(16, 56))), 
                    crs = 4326) |>
                    st_transform(crs = 4087) |>
                    st_coordinates()
    
    ggplot(data = st_transform(world, crs = 4087)) +
      geom_sf(fill = "transparent") +
      coord_sf(xlim = coords[,1], ylim = coords[,2]) +
      theme(panel.grid.major = element_line(colour = alpha("black", 0.1), 
                                            linewidth = 1)
      ) 
    

    enter image description here