ggplot2 map with a shapefile issue

I desperately need help, I am very much a beginner with R and I need to make maps showing the previous and new distribution of a species. I downloaded a free shapefile from: Marine Regions · Mediterranean Sea (IHO Sea Area) which is supposed to spatially map the mediterranean sea (for me the previous species distribution). When I run my code my images look like this:

I need the red part to cover the mediterranean sea not be weird triangle shapes like it is now.
Here is the code:

# These were just for the basic map of the mediterranean sea
library("ggplot2")                
library("sf")         
library("rnaturalearth")
library("rnaturalearthdata")
library("rgeos")
library("ggspatial")
library("scales")
library("rgdal")

#This is for the map of just the Med without the distribution layer
world <- ne_countries(scale = "medium", returnclass = "sf")


shape <- st_read("~/Desktop/iho/iho.shp")
Medfill <- fortify(shape)

Med <- ggplot(data = world) +
  geom_sf(color = "black", fill = "light grey") +
  annotation_scale(location = "bl", width_hint = 0.5) + 
  geom_polygon(aes(x = longitude, y = latitude), data = shape,
               colour = 'black', fill = 'red', alpha = .4, size = .3) +
  xlab("Longitude") + ylab("Latitude") +
  coord_sf(xlim = c(-17.43, 40.08), ylim = c(28.33, 59.80), expand = FALSE) +
  ggtitle("Mediterranean Sea Distribution") +
  theme(panel.grid.major = element_line(color = gray(.25), linetype = "blank",
            size = 0.2), panel.background = element_rect(fill = "light blue"))

plot_grid(Med)

Hi. It might be because of the mismatch in the crs, but I have transformed world to match though you might want to use a different one. I didn't use fortify, but you weren't using it either (you had data = shape). I have used geom_sf() instead of geom_polygon().

#This is for the map of just the Med without the distribution layer
world <- ne_countries(scale = "medium", returnclass = "sf") %>% 
  st_transform(4326)

shape <- st_read("iho.shp") # whatever your path was
# Medfill <- fortify(shape)

Med <- ggplot(data = world) +
  geom_sf(color = "black", fill = "light grey") +
  annotation_scale(location = "bl", width_hint = 0.5) +
  geom_sf(aes(x = longitude, y = latitude), data = shape,
               colour = 'black', fill = 'red', alpha = .4, size = .3) +
  xlab("Longitude") + ylab("Latitude") +
  coord_sf(xlim = c(-17.43, 40.08), ylim = c(28.33, 59.80), expand = FALSE) +
  ggtitle("Mediterranean Sea Distribution") +
  theme(panel.grid.major = element_line(color = gray(.25), linetype = "blank",
                                        size = 0.2), panel.background = element_rect(fill = "light blue"))

plot_grid(Med)

2 Likes

The polygons are a classic problem with using fortify; the royal road to thematic mapping is bringing shapefiles into an {sf} package data frame like object that can be combined with corresponding data and {ggplot2} then "just works."

1 Like

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.