Map with facet_wrap (ggplot2)

Hello,

I'm trying to use ggplot2 and facet_wrap to show the evolution of membership in Mercosur. My goal is to have each facet depicting a year (1991, 1996...) and which were the countries member of the trade bloc, with a different colour to indicate if they were full members or associated members.
The countries that were part of the bloc in that year are listed in "region", while the year is "Ano" and their status is in "Categoria"

When I run the following code I receive: Error: stat_sf requires the following missing aesthetics: geometry

ggplot(data = world) +
  geom_sf() +
  geom_sf(colour = "black") +
  geom_sf(data = MSRmap, aes(fill = Categoria)) +
  coord_sf(xlim = c(-90, -30), ylim = c(-60, 15), expand = FALSE) +
  facet_wrap(~Ano) +
  theme_minimal() + 
  labs(fill = "Categoria",
       x = "",
       y = "") 

The reprex is (I'm not sure if you need all the data from all the countries)

MSRmap <- data.frame(
  stringsAsFactors = FALSE,
            region = c("Argentina","Argentina",
                       "Argentina","Argentina","Argentina"),
              long = c(-68.66162109375,
                       -68.66162109375,-68.66162109375,-68.66162109375,-68.66162109375),
               lat = c(-49.9357414245605,
                       -49.9357414245605,-49.9357414245605,-49.9357414245605,
                       -49.9357414245605),
             group = c(20, 20, 20, 20, 20),
             order = c(1686L, 1686L, 1686L, 1686L, 1686L),
         Categoria = c("Estado-Parte","Estado-Parte",
                       "Estado-Parte","Estado-Parte","Estado-Parte"),
               Ano = c(2003, 1996, 2015, 2006, 2016)
)

world <- map_data("world")

I appreciate your help in advance!

On one hand, I managed to plot something with this code:

ggplot(MSRmap, aes(long, lat, group = group))+
  geom_polygon(aes(fill = Categoria), color = "white")+
  facet_wrap(~Ano)

However, it gives these maps that don't show much. Only one country has the proper shape, while the others are crossed by white lines

Reproducible example:

data <- data.frame(
  stringsAsFactors = FALSE,
         Categoria = c("Estado-Parte","Estado-Parte",
                       "Estado-Parte","Estado-Parte","Estado-Parte",
                       "Estado-Associado","Estado-Parte","Estado-Associado",
                       "Estado-Parte","Estado-Parte"),
               Ano = c(1991,1991,1991,1991,1996,
                       1996,1996,1996,1996,1996),
            region = c("Argentina","Brazil",
                       "Paraguay","Uruguay","Argentina","Bolivia","Brazil","Chile",
                       "Paraguay","Uruguay")
)

world <- map_data("world")

MSRmap <- merge (world, data)

The white lines are the colour you've specified to outline the geometries, so you can tinker with that aesthetic to get rid of them.

The reason you're missing parts of the continent is, presumably, because they don't have categories (Categoria) in the data for that year, so there is nothing to plot. You could add a base map, or you could fill in the gaps for those years for which the categories don't exist (e.g. in your example, you have four countries with categories for 1991, but six of them for 1996).

I'm not familiar with the dataset you're using, but, presumably, the strange shapes are coming from there…

Here's what I get when I run your code as a reprex:

suppressPackageStartupMessages(library(tidyverse))
data <- data.frame(
  stringsAsFactors = FALSE,
  Categoria = c("Estado-Parte","Estado-Parte",
                "Estado-Parte","Estado-Parte","Estado-Parte",
                "Estado-Associado","Estado-Parte","Estado-Associado",
                "Estado-Parte","Estado-Parte"),
  Ano = c(1991,1991,1991,1991,1996,
          1996,1996,1996,1996,1996),
  region = c("Argentina","Brazil",
             "Paraguay","Uruguay","Argentina","Bolivia","Brazil","Chile",
             "Paraguay","Uruguay")
)

world <- map_data("world")

MSRmap <- merge(world, data)

ggplot(MSRmap, aes(long, lat, group = group))+
  geom_polygon(aes(fill = Categoria))+
  facet_wrap(~Ano)

Created on 2021-04-05 by the reprex package (v1.0.0)

Also, to maintain the shape, you'll need to lock in an aspect ratio for the image.

There's distortion of the polygons that can be adressed with an arrange

suppressPackageStartupMessages(library(tidyverse))
data <- data.frame(
  stringsAsFactors = FALSE,
  Categoria = c("Estado-Parte","Estado-Parte",
                "Estado-Parte","Estado-Parte","Estado-Parte",
                "Estado-Associado","Estado-Parte","Estado-Associado",
                "Estado-Parte","Estado-Parte"),
  Ano = c(1991,1991,1991,1991,1996,
          1996,1996,1996,1996,1996),
  region = c("Argentina","Brazil",
             "Paraguay","Uruguay","Argentina","Bolivia","Brazil","Chile",
             "Paraguay","Uruguay")
) 
world <- map_data("world")

MSRmap <- merge(world, data) %>%
  arrange(region,order)

ggplot(MSRmap, aes(long, lat,
                   group=group,
                   fill=Categoria))+
  geom_polygon()+
  facet_wrap(~Ano) 

2 Likes

Thank you!
I've found out that the problem was in the order of the points, caused by merge(). Thus, I've used left_join() that bypassed this issue.

About the categories, the idea is precisely that in 1991 some countries were not members of the bloc, while they entered in 1996.

I share below the code I've written and it's working well.

#Countries
world <- map_data("world")

#Load data

#Merge datasets
MSRmap<-left_join(data, world)

##MAP
ggplot(MSRmap, aes(long, lat, group = group))+
  geom_polygon(aes(fill = Categoria), color = "black")+
  scale_fill_manual(values=colours)+
  coord_sf(xlim = c(-90, -30), ylim = c(-60, 15), expand = FALSE) +
  facet_wrap(~Ano, nrow=2) +
  theme_bw() +
  ggtitle("Mercosul", subtitle = "Estados-Partes e Estados-Associados, 1991 a 2021") +
  theme(plot.title = element_text(size = 15, face="bold")) +
  theme(plot.subtitle = element_text(size = 12)) +
  labs(fill = "Categoria",
     x = "",
     y = "")

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.