aaaaa
aaaaa

Reputation: 185

Remove countries' political borders from ggplot2 map

I need to remove the political borders of the countries from the following ggplot2 map:

library(ggplot2)

world = map_data('world')

plot=ggplot() +
geom_polygon(data=world, aes(x=long, y=lat, group=group), fill='NA', color='black', size=0.2)

print(plot)

enter image description here

Any suggestion on how I can do this? Thanks

Upvotes: 1

Views: 1992

Answers (2)

aboyd
aboyd

Reputation: 13

The rnaturalearth package offers an easier way to make world maps in ggplot2 now.

library(ggplot2)
library(rnaturalearth)

#Mapping for coastlines
coast <- ne_coastline(scale = "small", returnclass = "sf")

ggplot(data = coast) + geom_sf() + theme_classic()

#Optional - apply a fill to the continents
world <- ne_countries(scale = "small", returnclass = "sf")

ggplot(data = world) +
  geom_sf(color = "#E5E5E5", fill = "#E5E5E5") +
  geom_sf(data = coast) + theme_classic()

Upvotes: 1

davidnortes
davidnortes

Reputation: 922

There are two workarounds to your question:

First workaround: Using maps instead of ggplot2

library(maps)    
world <- maps::map("world", fill=FALSE, plot=TRUE, interior = FALSE)

Which results in:

enter image description here

Second workaround: Using maps and ggplot2

library(maps)
library(magrittr)
library(maptools)
library(raster)
library(ggplot2)

#Defining a general CRS
mycrs <- "+proj=longlat +datum=WGS84 +no_defs"

#Using the original maps package, then converting map into SpatialPolygons object
world <- maps::map("world", fill=TRUE) %$% 
  maptools::map2SpatialPolygons(., IDs=names,proj4string=CRS(mycrs))

#The resulting map has self intersection problems so any further operation reports errors; using buffers of width 0 is a fast fix
while(rgeos::gIsValid(world)==FALSE){
  world <- rgeos::gBuffer(world, byid = TRUE, width = 0, quadsegs = 5, capStyle = "ROUND")
}

#Dissolving polygon's limits
world <- raster::aggregate(world)

#Plotting. I add theme_void to your code to erase any axis, etc
ggplot() +
  geom_polygon(data = world, aes(x=long, y=lat, group=group), fill='NA', color='black', size=0.2)+
  theme_void()

The result:

enter image description here

Hope it helps

Upvotes: 2

Related Questions