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)
Any suggestion on how I can do this? Thanks
Since the library maptools is no longer available, the original answer provided is obsolete. Here is an update using the sf library:
library(maps)
library(ggplot2)
library(sf); sf::sf_use_s2(FALSE)
#Defining a general CRS
mycrs <- "+proj=longlat +datum=WGS84 +no_defs"
#Downloading world map (maps package) and converting 'map' object to 'sf' object
world <- maps::map("world", fill=TRUE, plot = FALSE)
world <- sf::st_as_sf(world)
world <- sf::st_transform(world, mycrs) #optional
#The resulting map has invalid geometries. Solving issue
if(isFALSE(all(sf::st_is_valid(world)))){
world <- sf::st_make_valid(world)
}
#Dissolving polygon's limits
limitless_world <- sf::st_union(world)
#Plotting. I add theme_void to your code to erase any axis, etc
ggplot() +
geom_sf(data = limitless_world, fill = 'NA', color = 'black', linewidth = 0.2) +
theme_void()
There are two workarounds to your question:
library(maps)
world <- maps::map("world", fill=FALSE, plot=TRUE, interior = FALSE)
Which results in:
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:
Hope it helps