Excluding specific country labels with ggplot and "world" dataset

Hello, I am struggling to get rid of country names that I don't need in a custom-made map of the Middle East and North Africa. I want to exclude the country labels for non-MENA countries such as Chad and Burkina Faso, but I am uncertain which function to use and where to insert it. Code below. All help much appreciated.
Best regards,
Jacob.

library(rnaturalearthdata)
library(sf)
library(tidyverse)

world <- ne_countries(scale = "medium", returnclass = "sf")

# Plotting map:
ggplot(data = world) +
  geom_sf() +
  # choose colour: geom_sf(color = "black", fill = "lightgreen")
  xlab("Longitude") + ylab("Latitude") +
  ggtitle("World map", subtitle = paste0("(", length(unique(world$name)), " countries)"))

# Zoom in on MENA area, retaining all the data in the "world" df. Coordinates 
# from https://www.mapsofworld.com
ggplot(data = world) +
  geom_sf() +
  coord_sf(xlim = c(-18.0, 72.0), ylim = c(11.5, 45.0), expand = FALSE) +
  xlab("lengdegrader") + ylab("breddegrader") +
  ggtitle("Midtøsten og Nord-Afrika")

# Switching off  spherical geometry  because of updates in sf, see link:
# https://github.com/r-spatial/sf/issues/1902

sf_use_s2(FALSE)
world <- st_make_valid(world)
world_points <- st_centroid(world)
world_points <- cbind(world, st_coordinates(st_centroid(world$geometry)))

# This is the custom-made map that I want to rid of irrelevant country names
ggplot(data = world) +
  geom_sf(fill= "antiquewhite") +
  geom_text(data = world_points, aes(x=X, y=Y, label=name),
            color = "darkblue", fontface = "bold", check_overlap = TRUE) +
  annotate(geom = "text", x = 20, y = 35, label = "MØNA", 
           fontface = "italic", color = "grey22", size = 6) +
  coord_sf(xlim = c(-18.0, 72.0), ylim = c(11.5, 45.0), expand = FALSE) +
  xlab("lengdegrader") + ylab("breddegrader") +
  ggtitle("Midtøsten og Nord-Afrika") +
  theme(panel.background = element_rect(fill = "aliceblue"))

Is there a field that you can use for those countries? Otherwise you could use this (instead of what you have for that section):

  geom_text(data = world_points %>% 
              filter(!admin %in% c("Chad", "Burkina Faso")), aes(x=X, y=Y, label=name),
            color = "darkblue", fontface = "bold", check_overlap = TRUE) +

Thank you very much. This worked.

1 Like

This topic was automatically closed 21 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.