0

I've created a world map with this code

graph_world <- 
  rnaturalearth::ne_countries(
    type = "countries",
    scale = "large",
    returnclass = "sf") %>% 
  filter(admin != "Antarctica") %>% 
  ggplot() +
  ggplot2::geom_sf(
    fill = "#264653", # Country colour
    size = 0          # Line size
    ) +
  ggplot2::coord_sf(
    expand = TRUE,
    default_crs = sf::st_crs(4326),
    xlim = c(-180, 180),
    ylim = c(-90, 90)) 

graph_world

The graph looks like this!

I would like to:

  1. Remove the white vertical borders on the left and the right. I'd like them to be transparent.
  2. Shift the map horizontally so that the left hand border goes through the Bering Strait rather than cutting Chukotka off from the rest of Russia.

Note that one way of removing the borders is to set coord_sf(expand = F) but this would also remove the empty space either side of the graph which I want to keep.

Quinten
  • 35,235
  • 5
  • 20
  • 53
Cameron
  • 226
  • 1
  • 12

1 Answers1

1

The solution to the rotation is to shift the the projection using st_crs(). We can set the Prime Meridian as 10 degrees east of Greenwich with "+pm=10 " which rotates the map.

# Download map data
df_world <- 
  rnaturalearth::ne_countries(
    type = "countries",
    scale = "large",
    returnclass = "sf") %>%
  st_make_valid()

# Specify projection 
target_crs <- 
  st_crs(
    paste0(
      "+proj=robin ", # Projection
      "+lon_0=0 ",
      "+x_0=0 ",
      "+y_0=0 ",
      "+datum=WGS84 ",
      "+units=m ",
      "+pm=10 ", # Prime meridian 
      "+no_defs"
    )
  )

However, if we graph this it will create 'streaks' across the map where countries that were previously contiguous are now broken across the map edge.

streaky map

We just need to slice our map along a tiny polygon aligned with our new map edge, based on the solution here

# Specify an offset that is 180 - the lon the map is centered on
offset <- 180 - 10 # lon 10

# Create thin polygon to cut the adjusted border 
polygon <- 
  st_polygon(
    x = list(rbind(
      c(-0.0001 - offset, 90),
      c(0 - offset, 90),
      c(0 - offset, -90),
      c(-0.0001 - offset, -90),
      c(-0.0001 - offset, 90)))) %>%
  st_sfc() %>%
  st_set_crs(4326)


# Remove overlapping part of world
df_world <- 
  df_world %>% 
  st_difference(polygon)

# Transform projection
df_world <- 
  df_world %>% 
  st_transform(crs = target_crs)

# Clear environment 
rm(polygon, target_crs)

Then we run the code above to get a rotated earth without smudges.

# Create map 
graph_world <- 
  df_world %>% 
  ggplot() +
  ggplot2::geom_sf(
    fill = "#264653", # Country colour
    colour = "#FFFFFF", # Line colour
    size = 0          # Line size
  ) +
  ggplot2::coord_sf(
    expand = TRUE,
    default_crs = sf::st_crs(4326),
    xlim = c(-180, 180),
    ylim = c(-90, 90))

# Print map
graph_world

enter image description here

Cameron
  • 226
  • 1
  • 12