How to plot a map with south up using the ggplot2 and sf packages?

104 Views Asked by At

I am trying to plot a map with south up using the packages ggplot2 and sf.

I tried the functions coord_flip and scale_y_reverse, but I was not successful.

The base code I am using is:

library(tidyverse)
library(sf)
library(rnaturalearth)
library(rnaturalearthhires)

BRA <- ne_states(country = "Brazil",
                 returnclass = "sf")

ggplot(BRA) +
  geom_sf(fill = "white") +
  coord_sf() 

Thank you in advance.

2

There are 2 best solutions below

3
Allan Cameron On

You could try an inverted Mercator projection:

ggplot(BRA) +
  geom_sf(fill = "white") +
  coord_sf(crs = paste0('+proj=merc +lat_0=0 +lon_0=0 +k=1 +x_0=0 +y_0=0 ',
                         '+axis=wsu +ellps=WGS84 +towgs84=0,0,0,0,0,0,0 ',
                         '+units=m +no_defs'))

enter image description here

0
Matheus BP On

You must modify your data before plotting it.

The first step is to create a function that rotates your data.

rot = function(a) matrix(c(cos(a), sin(a), -sin(a), cos(a)), 2, 2)

Then you apply this to your dataset (with st_geometry()).

a <- BRA |> st_geometry() * rot(pi)

The output is Brazil map rotated 180°:

ggplot(a) +
geom_sf(fill = "white")

enter image description here

And the same can be applied to your datapoints. See https://r-spatial.github.io/sf/articles/sf3.html in Affine Transformations section.