# Beautiful Mapping with R (Extracting the Kenyan Map from the World Map) — Part 2

From Part 1 of the mapping series, we loaded shapefiles into R and plotted the Kenyan map based on the given county coordinates(the shapefiles GEO folder).

Here, we will extract the Kenyan Map from the world map data. The world map can be obtained as a base map of the world using ggplot2. See below;

`# #Load packages ---------------------------------------------------library(tidyverse)library(sf)library(rgeos)# #Obtain the world map --------------------------------------------world1 <- map_data("world")`

# Plotting the world map;

`# #Plot the world map ----------------------------------------------ggplot(world1, aes(x=long, y=lat, group=group))+  geom_polygon(fill="green",color = "black")`

# Extracting the Kenyan Map;

The function used for the extraction is coord_map. coord_map projects a portion of the earth, which is approximately spherical, onto a flat 2D plane using any projection defined by the mapproj package. Map projections do not, in general, preserve straight lines, so this requires considerable computation. Source; https://ggplot2.tidyverse.org/reference/coord_map.html

`# #Extract the Kenyan Map ------------------------------------------ggplot(world1, aes(x=long, y=lat, group=group))+  geom_polygon(fill="yellow",color = "black")+  coord_map(xlim=c(33,42),ylim=c(-5,5.5))+  xlab("")+  ylab("")`

One can also add a title or subtitle to the map, change the colour of the map and the theme. It’s all up to how you want it to appear.

`# #Adding a title and subtitle -------------------------------------library(ggthemes) #theme_stata requires this packageggplot(world1, aes(x=long, y=lat, group=group))+  geom_polygon(fill="skyblue",color = "black")+  coord_map(xlim=c(33,42),ylim=c(-5,5.5))+  xlab("")+  ylab("")+  labs(title = "Kenyan Map", subtitle = "This is an Extract from the world map")+  theme_stata()`

Wow! Do you like it as much as I do?

Well, there’s so much more in store for mapping such as labelling, merging map data and a data frame for spatial data analysis etc.

Thanks for reading. I will definitely share more in the upcoming articles.

--

--

Data storyteller | Nature Lover | Aspiring photographer