The level refers to the administrative level, so level 0 will give you the country outline and level 1 will give you the regional boundaries.

The countries are identified by their ISO Country Code, a list of these can be found here:

http://www.nationsonline.org/oneworld/country_code_list.htm

library(maptools)
library(raster)
library(plyr)
library(ggplot2)
library(rgdal)

plot(Kenya)

plot(Kenya1)

If you want to map a very localised area it may be best to use a UTM grid, (http://www.dmap.co.uk/utmworld.htm) the world is split up into UTM grids and each have their own projection. You can ignore the letters on the UTM map, you just need to know the grid number and your grid square is north or south.

Once you have found which UTM square you want to use (I am using UTM 37S for Kenya), you need to find the EPSG code for it, you can do this on http://spatialreference.org/

Reprojecting might not make a huge amount of difference to countries around the equator, but it is good practice to set an appropriate projection for your work.

Kenya1_UTM<-spTransform(Kenya1, CRS("+init=EPSG:32737"))  

We can look at the names of the regions and pick out some we might be interested in:

Kenya1_UTM@data$NAME_1 ## [1] "Baringo" "Bomet" "Bungoma" ## [4] "Busia" "Elgeyo-Marakwet" "Embu" ## [7] "Garissa" "Homa Bay" "Isiolo" ## [10] "Kajiado" "Kakamega" "Kericho" ## [13] "Kiambu" "Kilifi" "Kirinyaga" ## [16] "Kisii" "Kisumu" "Kitui" ## [19] "Kwale" "Laikipia" "Lamu" ## [22] "Machakos" "Makueni" "Mandera" ## [25] "Marsabit" "Meru" "Migori" ## [28] "Mombasa" "Murang'a" "Nairobi" ## [31] "Nakuru" "Nandi" "Narok" ## [34] "Nyamira" "Nyandarua" "Nyeri" ## [37] "Samburu" "Siaya" "Taita Taveta" ## [40] "Tana River" "Tharaka-Nithi" "Trans Nzoia" ## [43] "Turkana" "Uasin Gishu" "Vihiga" ## [46] "Wajir" "West Pokot" Laikipia<-Kenya1_UTM[Kenya1_UTM@data$NAME_1 == "Laikipia",]
Laikipia_df<-fortify(Laikipia)

And we can join some count data to the admin areas so that we can visualise it later on.

You can use the code below to join data from a csv to a shapefile, based on a common spatial value, here it is administrative boundaries but it could be anything.

NAME_1<-Kenya1_UTM@data$NAME_1 count<-sample(1:1000,47) #or any other data you can associate with admin level here count_df<-data.frame(NAME_1, count) Kenya1_UTM@data$id <- rownames(Kenya1_UTM@data)
Kenya1_UTM@data <- join(Kenya1_UTM@data, count_df, by="NAME_1")
Kenya1_df <- fortify(Kenya1_UTM)
Kenya1_df <- join(Kenya1_df,Kenya1_UTM@data, by="id")

Set general theme options for the ggplot. This includes things like the background colour, axis titles and tick marks.

For maps you might want most of these to be blank, but for things like graphs this would be different.

theme_opts<-list(theme(panel.grid.minor = element_blank(),
panel.grid.major = element_blank(),
panel.background = element_blank(),
plot.background = element_blank(),
axis.line = element_blank(),
axis.text.x = element_blank(),
axis.text.y = element_blank(),
axis.ticks = element_blank(),
axis.title.x = element_blank(),
axis.title.y = element_blank(),
plot.title = element_blank()))

Here we can create a map just highlighting Laikipia

ggplot() +
geom_polygon(data=Kenya1_df, aes(long,lat,group=group), fill="whitesmoke")+
geom_path(data=Kenya1_df, aes(long,lat, group=group), color="grey",
size=0.1) +
geom_polygon(data=Laikipia_df, aes(long,lat,group=group), fill="red")+
theme(aspect.ratio=1)+
theme_opts

Let’s try visualising the count data by changing the ‘fill’ argument

ggplot() +
geom_polygon(data = Kenya1_df, aes(x = long, y = lat, group = group, fill =
count), color = "black", size = 0.25) +
theme(aspect.ratio=1)

Now we can try and make it prettier

#install.packages("ggmap")
#install.packages("scales")

library(ggmap)
library(scales)
theme_opts<-list(theme(panel.grid.minor = element_blank(),
panel.grid.major = element_blank(),
panel.background = element_blank(),
plot.background = element_blank(),
axis.line = element_blank(),
axis.text.x = element_blank(),
axis.text.y = element_blank(),
axis.ticks = element_blank(),
axis.title.x = element_blank(),
axis.title.y = element_blank(),
plot.title = element_blank()))
ggplot() +
geom_polygon(data = Kenya1_df, aes(x = long, y = lat, group = group, fill =
count), color = "black", size = 0.25) +
theme(aspect.ratio=1)+
scale_fill_distiller(name="Count", palette = "YlGn", breaks = pretty_breaks(n = 5))+
labs(title="Nice Map")

This tutorial is adapted from the following blogs: http://prabhasp.com/wp/how-to-make-choropleths-in-r/ http://www.r-bloggers.com/mapping-with-ggplot-create-a-nice-choropleth-map-in-r/