Search code examples
rggplot2gisr-sfrnaturalearth

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


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.


Solution

  • 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.