Mapmaking with R

Mapping the 2022 election results.

Mission of the project

To equip students with the necessary skills to design and make maps that communicate

as well as the relevant knowledge to manipulate spatial data with code.

Project Procedure

  1. Loading libraries

library('tidyverse')

library('tmap')

library('dplyr')

library('st')

library('sf')

  1. Reading the datasets

#election

data<-read.csv('election2022.csv')

#read shapefile

counties<-st_read('County.shp')

  1. Merging

#merge the two

election<-merge(counties,data,by.x='county_id',by.y='Code')

  1. Decision Making with ifelse statements

election$Winner<-ifelse(election$Odinga>election$Ruto,'Raila','Ruto')

  1. Plotting with tmap

mypal<-c('blue','yellow')

tmap_mode('view')

tm_shape(election)+tm_polygons("Winner",palette=mypal,title="Candidate")+

tm_layout(title="2022 General Election", title.position = c("right","top"))


Questions?

Contact [kevinkiprotich250@gmail.com] to get more information on the project