Suppose I want to create a world map, with borders. I do not need Antarctica in the plot, so I filter it from the map data.
library(dplyr)
library(ggplot2)
library(maps)
world_map <- map_data("world") %>%
filter(region != "Antarctica")
However, if I add borders()
to the plot, it shows up again because the function takes the border data straight from the maps package, including Antarctica.
ggplot()+
geom_polygon(data = world_map, aes(x = long, y = lat, group = group))+
borders()
Is there a way to exclude it, maybe using the regions
argument? I could not find a way, except for playing around with ylim = c(-55, 90)
, as suggested in another post, which works. However, it also seems like a very crude solution that might work in this specific case, but not in most others.
You can use colour =
and fill =
to define your map borders and fill colours:
library(dplyr)
library(ggplot2)
library(maps)
world_map <- map_data("world") %>%
filter(region != "Antarctica")
ggplot() +
geom_polygon(data = world_map,
aes(x = long, y = lat, group = group),
colour = "black",
fill = "grey")
However, note that geom_polygon
is not the best geom to use when working with geospatial data. If you want a map that conforms to geographical norms, the easiest way is to use the rnaturalearth
package. The ne_countries()
world dataset is an sf object, and comes with a defined CRS. As such, you can use geom_sf()
to plot it. It's a much more robust approach and it's simpler too as ggplot2
does a lot of the 'heavy lifting' for you e.g. no need to declare x and y aesthetics:
library(rnaturalearth)
world_map1 <- ne_countries() %>%
filter(sovereignt != "Antarctica")
ggplot() +
geom_sf(data = world_map1,
colour = "black",
fill = "grey")
To ensure the y-axis text is plotted, and the northern and southern-most features aren't touching the edge of the plot (which personally I don't like), you could use something like:
ggplot() +
geom_sf(data = world_map1,
colour = "black",
fill = "grey") +
coord_sf(expand = FALSE) +
scale_y_continuous(limits = c(-58, 90))