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

50 views Asked by At

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

2

There are 2 answers

2
Allan Cameron On BEST ANSWER

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

1
Wimpel On

an other option can be to drop the crs all together.. this will autimatically create a square (numeric) grid to plot on

ggplot(data = world %>% sf::st_set_crs(NA)) +
  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"))

enter image description here